Programming Questions

  • Newest
  • Popular Tags
  • Ask Question
  • SecondGreatLow JavaScript Code Not Working
    Hi. I made the following program below in the JavaScript environment over at It works there to retrieve the Second Highest and Lowest Numbers in an Array. When I type "input.sort(function(a,b)...", strangely, it indents everything else in the block remaining to the middle of the page. If I cut and paste all of my code anyway, it still seems not to work. It only returns me a blank box; no values inside of it. Please help? Why isn't this code working on Coderbyte too? function SecondGreatestLow(arr) { input.sort(function(a,b){return (a-b);}); var input = arr; var hashObject = {} for (var i = input.length - 1; i >= 0; i--) { var currentItem = input[i]; //WE LOOP THROUGH OUR NUMBER LIST if (hashObject[currentItem] === true) { input.splice(i, 1); //IF CURRENTITEM IS ALREADY IN HASHOBJECT, THEN DELETE THE COPY FOUND IN ARRAY AND MOVE ON TO NEXT NUMBER } hashObject[currentItem] = true; //IF CURRENTITEM IS NOT IN HASHOBJECT, ADD IT! THEN MOVE ON TO NEXT NUMBER. } //THE ARRAY HAS NOW BEEN "DE-DUPED": //NEXT WE OUTPUT THE MAX AND MIN VALUES ACCORDING TO ARRAY SIZE if (arr.length >= 4) { //If 4+ chars, then remove the first two values to access the second by default arr.pop(); arr.shift(); var min = Math.min.apply(null,input), max = Math.max.apply(null,input); return (max + " " + min); } else { var min2 = Math.min.apply(null,input), max2 = Math.max.apply(null,input); return max2 + " " + min2; } }
    nytek posted this question on 5/26/14 | javascript, array
  • +
  • 1
  • -
  • 1) It looks like you removed this line: // keep this function call here // to see how to enter arguments in JavaScript scroll down SecondGreatLow(readline()); 2) You changed the name of the function. You need to either change it back to SecondGreatLow or change the function call to SecondGreatestLow(readline());. 3) Your first line tries to sort an array that doesn't yet exist: input.sort(function(a,b){return (a-b);}); var input = arr; Once you solve those problems, you'll start seeing output and be able to debug the rest of it.
    Log in to write an answer.