...

View Full Version : Unchangable variable?



CrackTheAir
06-02-2009, 09: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"
alert(h)
}


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"
alert(l)
}


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, 09:28 PM
Hey,

Put
l="Two"
instead of
l=="Two"

Mike

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

rnd me
06-03-2009, 08: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(",");

alert(labels[h]);

Philip M
06-03-2009, 09: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(",");

alert(labels[h]);

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(",");
alert (labels[maxIndex]);

</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum