<body> <h1> Select an Input File</h1> <p> <input type="file" id="fileInput"> <p> </body>
Select an Input File
< script type="text/javascript"> </script>
< script type="text/javascript"> "use strict"
let fileSelector = document.getElementById('fileInput');
let fileSelector = document.getElementById('fileInput'); fileSelector.addEventListener('change', MyFileReader);
fileInput
button changes, call the function MyFileReader
.
function MyFileReader(theEvent) { console.log(theEvent); }
function MyFileReader(theEvent) { let file = theEvent.target.files[0]; console.log("The file is ", file.name); }
<script type="text/javascript"> "use strict" const reader = new FileReader(); reader.addEventListener("loadend", SortThem);
MyFileReader
I added
function MyFileReader(theEvent) { let file = theEvent.target.files[0]; console.log("The file is ", file.name); reader.readAsText(file); }
SortThem
function
function SortThem() { console.log(reader.result); }
split
.
parseInt
to turn it into an integer.
function SortThem() { //console.log(reader.result); let i; let rawData = reader.result.split(" "); let data = []; for(i = 0; i < rawData.length; i++) { data.push(parseInt(rawData[i])); } }
data
and rawData
are arrays
document.write
for(i =0; i < data.length; i++) { document.write(i + ": ", data[i], "<p>"); } }
function Sort(data) { let smallest, i , j; for(i = 0; i < data.length; i++) { smallest = i; for(j = i+1; j < data.length; j++) { if (data[j] < data[smallest]) { smallest = j; } } if (smallest != i) { let tmp = data[i]; data[i] = data[smallest]; data[smallest] = tmp; } } }