Dr_Jae Posted March 10, 2006 Share Posted March 10, 2006 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! Link to comment Share on other sites More sharing options...
scott100 Posted March 10, 2006 Share Posted March 10, 2006 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!<{POST_SNAPBACK}> 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 More sharing options...
Dr_Jae Posted March 10, 2006 Author Share Posted March 10, 2006 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 <{POST_SNAPBACK}> 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 More sharing options...
scott100 Posted March 10, 2006 Share Posted March 10, 2006 (edited) 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 March 10, 2006 by scott100 Link to comment Share on other sites More sharing options...
Verminox Posted March 10, 2006 Share Posted March 10, 2006 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 More sharing options...
scott100 Posted March 10, 2006 Share Posted March 10, 2006 If you want to keep it simple use this:<{POST_SNAPBACK}> Keep it simple? there almost identical :)Plus your code is incorrect, it treats 9 as being higher than 77 etc Link to comment Share on other sites More sharing options...
Verminox Posted March 10, 2006 Share Posted March 10, 2006 How come :)The highval is an int not a string right? Shouldnt it make 77 higher????Oh and I said its simple as in simple to make on your own (assuming the topic starter is a learner). Link to comment Share on other sites More sharing options...
scott100 Posted March 10, 2006 Share Posted March 10, 2006 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 More sharing options...
Verminox Posted March 11, 2006 Share Posted March 11, 2006 What exactly does parseFloat do? Link to comment Share on other sites More sharing options...
scott100 Posted March 11, 2006 Share Posted March 11, 2006 What exactly does parseFloat do?<{POST_SNAPBACK}> parseFloat() converts a string to an floating point numberparseInt() converts as string to an integerHave a read at this page which explaines in more detail: http://www.javascripter.net/faq/convert2.htm Link to comment Share on other sites More sharing options...
Verminox Posted March 11, 2006 Share Posted March 11, 2006 Thanks a lot mate Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now