Image

I'm having a problem executing a while loop. I want to find the maximum and the second largest number that a user enters out of 10 numbers. I can get the maximum, but I haven't had any luck with the coding for finding the second largest number that a user inputs. Any suggestions would be great.

My code so far is behind the cut.

<SCRIPT LANGUAGE= "JavaScript">



  var   counter,  //number of integers entered

    number,    //current digit entered into the program

    largest,  //maximum value so far

    secondLargest,  //second largest number entered

    numValue;  //the number the user typed in as an integer





  // save the maximum and minimum to display later

  number = window.prompt( "Enter an integer:", "0");

  numValue = parseInt( number );

  largest = numValue

  secondLargest = numValue



  //Initialization

  counter = 1;  //prepare to loop





  //Processing



  while ( counter <= 9 ) {  //loop an additional 9 times



    //prompt user for an integer

    number = window.prompt( "Enter an integer:", "0") ;



    //convert number from a string to an integer

    numValue = parseInt( number ) ;



    //check each number entered for the maximum

    if(numValue > largest)

      largest = numValue;







    //check each number entered for the second largest

    if(numValue > secondLargest && largest > numValue)

      secondLargest = numValue;





    // add 1 to Counter

    counter = counter + 1 ;

  }



  //Termination





  // display the maximum numbers typed in

  document.writeln( "<H3>The maximum is " + largest + ".</H3>" );





  // display the second largest number typed in

  document.writeln( "<H3>The second largest is " + secondLargest + ".</H3>" );





</SCRIPT>