I am currently working on a program to sort a parallel set of arrays. The program is suppose to sort the first array with the myArray.sort()
method/function. the second arrays values should stay in line with the first arrays sort when the program prints both of them out. I think i am very close to solving it but cant quite wrap my head around it.The sortArrays()
function is the only logic that needs work the rest works perfectly. Any help will be greatly appreciated.
Javascript
var employeeName = [];
var employeeSal = [];
var tmpArrayName = [];
var tmpArraySal = [];
var arraySize = 0;
function getInfo() {
arraySize = parseInt(prompt("How many employee records will you be adding today?"));
while (isNaN(arraySize)) {
arraySize = parseInt(prompt("Error: please enter a valid positive number for question (How many employee records will you be adding today?)"));
}
for (var l = 0; l < arraySize; l++) {
employeeName[l] = prompt("Please enter the employee's last name and first name (ex: Wayne John)");
employeeSal[l] = parseInt(prompt("Please enter the employee's yearly salary (ex: 62000)"));
while (isNaN(employeeSal[l])) {
employeeSal[l] = parseInt(prompt("Please enter the employee's yearly salary (ex: 62000)"));
}
}
tmpArrayName = employeeName;
tmpArraySal = employeeSal;
employeeName.sort();
sortArrays();
printResult();
}
function sortArrays() {
var index = 0;
for (var i = 0; i < employeeName.length; i++) {
index = employeeName.indexOf(tmpArrayName[i]);
employeeSal[index] = tmpArraySal[i];
}
}
function printResult() {
for (var k = 0; k < employeeName.length; k++) {
document.write('' + employeeName[k] + ' ' + employeeSal[k] + '<br/>');
}
}
HTML
<body onload="getInfo()">
<div id ="content">
</div>
</body>