PDA

View Full Version : Unchangable variable?

CrackTheAir
06-02-2009, 08:19 PM
Hi all.

I used the function Math.max() to find the maximum number in a series of numbers. However, after it finds that number, I want it to say a specific thing if it is a certain number. Here's the code:

var h = Math.max(a,b,c,d,e,f,g)
if (h==2){
h=="Two"
}

However, when the maximum does turn out to be 2, it alerts ("2") instead of ("Two"). I've also tried this:

var h = Math.max(a,b,c,d,e,f,g)
var l = "string"
if (h==2){
l=="Two"
}

But on the alert, it comes back as "string". What's wrong here and what can I do to make this work?

Mike_O
06-02-2009, 08:28 PM
Hey,

Put
l="Two"
l=="Two"

Mike

CrackTheAir
06-02-2009, 08:38 PM
Thanks

rnd me
06-03-2009, 07:08 AM
might want to consider using a look-up table instead of forks in a situation like this:

var h = Math.max(a,b,c,d,e,f,g);
var labels="Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten".split(",");

Philip M
06-03-2009, 08:10 AM
might want to consider using a look-up table instead of forks in a situation like this:

var h = Math.max(a,b,c,d,e,f,g);
var labels="Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten".split(",");

That does not work! labels[h] is the maximum value of the list, (say 99), not the index of that value.

Suggest:-

<script type = "text/javascript">

var a = [5,26,4,96,7,24,45,38,52];
var index;
var max = a[0];
var maxIndex = 0;
for (index = 1; index < a.length; index++) {
if (a[index] > max) {
max = a[index];
maxIndex = index;
}
}

alert ("Maximum value is " + max + " at array index " + maxIndex);
var labels="Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten".split(",");