Jump to content

Response ASAP thanks!


Dr_Jae
 Share

Recommended Posts

I need a script that will prompt the user for 10 numbers and store them in a array. then compare them to print the largest number. I wish was wasn't so lame in Javascript.Thanks for quick response guys!

Dj_Jae, have you tried to accomplish this for yourself? We are here to give you guidance etc not to write entire scripts.Try it first and if you get stuck post your code.cheers :)
Link to comment
Share on other sites

Dj_Jae, have you tried to accomplish this for yourself?  We are here to give you guidance etc not to write entire scripts.Try it first and if you get stuck post your code.cheers :)

Ok, here is what u got so far, I am unsure how to compare the numbers. Thanks if you can help.
    <script type="text/javascript">var studentGrade=new Array();for (var i=1; i<=10; i++){var Grade = prompt("Please enter Grade#"+i+" :","");studentGrade[i]=new Array(Grade);}for (var i=1; i<=1; i++){document.write("The highest Grade is #"+i+" : " + studentGrade[i][0] + "<br>");}    </script>

Link to comment
Share on other sites

You will have to use an if statement in your second loop to compare the array and also set the loops limit to array.lengh

<script type="text/javascript">var studentGrade=new Array();for (var i=1; i<=10; i++){    var Grade = prompt("Please enter Grade#"+i+" :","");    studentGrade[i]=new Array(Grade);}highestGrade=studentGrade[1];student=1;for (var i=2; i<=studentGrade.length-1; i++){         studentGrade[i]=parseFloat(studentGrade[i]);   if (studentGrade[i]>highestGrade)		    {	        highestGrade=studentGrade[i];	        student=i;    } }document.write("The highest Grade is #" +student +": "+ highestGrade + "<br>");</script>

Edited by scott100
Link to comment
Share on other sites

If you want to keep it simple use this:

<script type="text/javascript">var studentGrade=new Array();for (var i=1; i<=10; i++){var Grade = prompt("Please enter Grade#"+i+" :","");studentGrade[i]=new Array(Grade);}var highval = ''var highgrade = ''for (var i=1; i<=10; i++){if(studentGrade[i]>highval){highval = studentGrade[i]highgrade = i}}document.write("The highest Grade is #"+highgrade+" : " + highval + "<br>");</script>

Link to comment
Share on other sites

The info stored in the array seems to be a string not a int, maybe cause its collected from the prompt() :) I initially done the same as you but noticed when i done some testing, i added parseFloat() to the array result before it is tested in the if statement and it works finestudentGrade=parseFloat(studentGrade); :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...