Log in

View Full Version : Javascript Homework Help - Part 2



Jeff
04-08-2008, 03:13 AM
I am trying to write a function that will return the max value from a variable amount of numbers. The script is written to ask the user to enter the number of number they wish to compare. Then, I have the for loop setup to define the array values. When this particular code runs, I get an infinite prompt loop. There must be a better way of doing this. I don't think is it possible to use Math.max on an array, or for more than two numbers.


<html>
<head>
<title>getMax</title>
<script language="JavaScript" type="text/javascript">
function getAvg() {
var numberOf = Number(prompt("How many values do you wish to enter?",""))
var num = new Array(numberOf)
var highest = 0
for (i=0;i<num.length;i++) {
num[i] = Number(window.prompt("Please enter the number", ""))
if (num[i--] > num[i])
{
highest=num[i--]
}
else
{
highest=num[i]
}

}

document.write(highest)

}
</script>
</head>
<body>
<script language="JavaScript" type="text/javascript">
getAvg();
</script>
</body>
</html>

AMDave
04-08-2008, 11:07 AM
There is nothing wrong with the structure of your algorithm.
It is just a wee problem with you array pointer.

You have to remember what i++ and i-- are actually doing.
They are the equivalent to i = i + 1 and i = i - 1
That means i will stay that way.

When you check the value of the previous number in the array (or vector), you have used num[i--]
Sure it looks at the previous number, but it also sets the pointer back to that position in the array because it changes the value of i by minus one permanently.

So when you say is num[i--] > num[i] the answer will always be no because they are now both looking at the same array block because the second i value is now the same as the one on the left (because the i-- set i to that value).

this is also why you go into a recursive loop, because the for-loop add 1 to i, and your value test takes 1 away from it again.
So it is not storing any more numbers in the array, just overwriting the same 1st value in the array.

This will fix your problem:
change to: num[i - 1] > num[i]
That way the value of i does not get changed permanently by the part on the left.
AND
change to: highest = num[i - 1]
That way the value of i does not get changed permanently by the part on the right.


Tested and proven.
Best of luck.

Jeff
04-13-2008, 03:29 AM
Hey Dave,

Your fix did infact clear up the loop. However, my syntax is no good.

For example, if I enter 4 numbers in this order: 1000, 56, 76, 6

it returns 76 instead of 1000


Any ideas?

AMDave
04-13-2008, 04:46 AM
Oh, yes. Half-assed AMDave strikes again.

There is an oops in the algorithm.

You are checking if the previous number is greater than the current number, and if it is then making that the value of highest, regardless of what higher values there might have been in the array. In your example above 76 is higher than 6 so that gets assigned to "highest" even though it previously had the value 1000.

What you really want to do is compare the current value of num[i] against the value of highest, and if it is greater than "highest" THEN update the value of highest.
This means that your IF statement no longer requires the "ELSE" clause

if (num[i] > highest)
{
highest=num[i]
}

You have already initialised highest to zero, so any positive number will be higher and updating will commence.
Important thought...what if all of the numbers you enter are negative? highest would remain zero. Perhaps you could initialize it as a long negative value?
eg:
var highest = -9E1000

Beerknurd
04-13-2008, 05:07 AM
Dave, you are a nerd. And I mean that in a positive way. :)

AMDave
04-13-2008, 05:14 AM
mm hm. You are positive that I am a nerd. :D

Actually I can back that up:
I just got the 32-bit Citrix Server client working on the RHEL5 x86_64 Xen kernel with 64-bit Firefox 1.5.x and 64-bit Seamonkey 1.1.9 without the 32-bit libraries so I am feeling quite chuffed with myself :rolleyes:

Beerknurd
04-13-2008, 05:21 AM
I wish I could be a code jockey.... I just don't have the patience... Looking at lines of code just gives me a headache. My nerdiness is in networking... Give me thousands of computers, and I'll get them all sharing information securely. But give me the simplest lines of code and I look like a deer caught in headlights... :lol:

Frederic Brillouet
04-13-2008, 07:20 AM
So later when I need to put 15 puters in my new house, Beer's gonna do the network, Dave's gonna do something with code (no doubt setting all those puters under his account :icon_lol:). Is there anything else I forgot?

Frederic Brillouet
04-13-2008, 08:11 AM
Ya.
Nflight will install the power plant :icon_mrgreen:
Ototero will monitor the overall progress of the project.
NeoGen will get us competing to finish each implementation stage fastest.
Empty_5oul will add flash and make it look presentable.
Vaughan and spjeff69 will provide guidance as the steering committee.
Every one else will be invited along for moral support and a BBQ ;-)
That's a deal :icon_mrgreen: That means I need to add enough rooms for you all to sleep. Hmm, maybe change 15 puters into 20-25 and a server heheh.