1

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] + '&nbsp;&nbsp;' + employeeSal[k] + '<br/>');
    }
}

HTML

<body onload="getInfo()">
    <div id ="content">

    </div>
</body>
Andreas
  • 21,535
  • 7
  • 47
  • 56
  • 1
    Woah, copy and paste malfunction going on. – Hayley Guillou Jul 08 '15 at 05:00
  • Your code is unreadable, and we should not have to do the cleanup job. – Tim Biegeleisen Jul 08 '15 at 05:00
  • You couple the employees name with the salary right? So why do you use 2 arrays? You could instead use an object with key/value therefore you only have to sort once. Take a look here: http://stackoverflow.com/questions/1168807/how-can-i-add-a-key-value-pair-to-a-javascript-object-literal – greenhoorn Jul 08 '15 at 05:29
  • I have to use the functionality we have learned and that's parallel arrays, sort(), indexOf(),and a few more basic ones. the objective was to use two arrays in parallel. It was my idea to sort just employeeName[] and change arrays employeeSal index's to match using a function. it looks solid but i am missing something. copying the arrays into temp arrays while i made the switches of the indexes seemed like the way to go. – Chris Becker Jul 08 '15 at 06:16

2 Answers2

1

The problem was that you were not making a copy of your arrays, but referencing the original ones. To make a copy, use slice:

tmpArrayName = employeeName;
tmpArraySal = employeeSal;

should be

tmpArrayName = employeeName.slice(0);
tmpArraySal = employeeSal.slice(0);
Caleb An
  • 366
  • 1
  • 10
  • i haven't used slice before i am still learning. i added some print statements through out the code to do error checking and it seemed like i had the old arrays stored in the tmp arrays. i am still very green with the programming. I can not use the functions/object types you guys are mentioning tho we haven't learned those yet. I pretty much have to write the function to sort them instead of using one we haven't been introduced to yet. any ideas not using slice? – Chris Becker Jul 08 '15 at 06:08
  • You're solution was perfect. This did fix the problem but i can not use it. Figured i would at least let people know that yours did work. – Chris Becker Jul 08 '15 at 06:39
  • if it works, please check my solution off and upvote so it gets a green checkmark. Thanks! – Caleb An Jul 08 '15 at 22:21
0

Ok, here is the final solution. @caleb-an your solution works great but i solved it by instead using a for loop to set the tmpArrays values to the original arrays.

//this code works fine but can not use
        //tmpArrayName = employeeName.slice(0);
        //tmpArraySal = employeeSal.slice(0);

       //this code does the same exact thing and works
       for (var e = 0; e < employeeName.length; e++)
       {
           tmpArrayName[e] = employeeName[e];
           tmpArraySal[e] = employeeSal[e];
       }