## Recommended Posts

my function, max, takes 3 arguments. they are numbers. i want to return the largest number, but it keeps returning the 2nd largest, 27, when i want 41 to be returned. please what am i doing wrong? <!DOCTYPE html><html><head><script type="text/javascript">function max(a,b,c){ if( a > b && c ){ return a; }else if (b > a && c){ return b; }else{ return c; }}</script></head><body><script type="text/javascript">alert(max(27,13,41));</script></body></html>

##### Share on other sites

Because since that is the first if in the if else if else conditionals, 'a' which is 27 is greater than 'b' which is 13, which is true and you're returning a. JS has a built in max function: http://www.w3schools.com/jsref/jsref_max.asp

##### Share on other sites

This is the problem:if( a > b && c ){ What that says is:if a is greater than b, and c is true (or non-false data. Non-false data is pretty much everything except '', 0, undefined, or null)So as long as a is larger than b and c is anything but '', 0, undefined, or null the function will return a. What you meant was this:if( a > b && a > c ){ EDIT: The same thing applies to your else if statement as well.

##### Share on other sites

oh i see. thank you guys

##### Share on other sites

You could also do something like this: var max = a;if (b > max) max = b;if (c > max) max = c;return max; You could turn that into a loop to allow you to pass as many parameters as you want instead of only 3 and it will return the max, or you could pass an array of numbers to get the max from.

• 1
##### Share on other sites

You could also do something like this: var max = a;if (b > max) max = b;if (c > max) max = c;return max; You could turn that into a loop to allow you to pass as many parameters as you want instead of only 3 and it will return the max, or you could pass an array of numbers to get the max from.
ah thats very cool. please can you show me an example of how it would work with the array
##### Share on other sites

Something like this:

`function max(numbers) {  var max = numbers[0];  for (var x=1, y=numbers.length; x<y; x++) {    if (numbers[x] > max) max = numbers[x];  }  return max;}`

##### Share on other sites

thanks guys. i really appreciate all the help

##### Share on other sites

hi guys i keep failing at the simpliest JS tasks. rather than making new threads each time for simple JS, i figured ill just ask here. heres what im trying to do: Write a function that takes a character (i.e. a string of length 1) and returns true if it is a vowel, false otherwise. this is the best i could come up with. it doesnt work, any tips? <!DOCTYPE html><html><head><script type="text/javascript">function vowel(i)if (i == "a" || i == "e" || i == "i" || i == "o" || i == "u") {return true;} else {return false;}</script></head><body><script type="text/javascript">alert("p" == vowel(i) ? "true" : "false");</script></body></html> lame attempt i know. will solving new tasks ever get easier?

Edited by SnakesBite101
##### Share on other sites

You forgot to wrap the function in braces: function vowel(i) {}

##### Share on other sites

the code still doesnt seem to work... no response

##### Share on other sites

When you call the function, you should do it like this:

`alert( vowel("p") ? "true" : "false" );`

##### Share on other sites

Edited by CodeName
##### Share on other sites

I believe he's trying to find out if "p" is a vowel. And that expression won't do it. That expression evaluates to true because "p" is equivalent to true.
##### Share on other sites

thanks for the help guys. it works !

##### Share on other sites

I believe he's trying to find out if "p" is a vowel. And that expression won't do it...
Yes bro, I was just pointing out the error and not the result. Edited by CodeName
##### Share on other sites

That expression evaluates to true because "p" is equivalent to true.
If he tried it, it would have evaluate to false, because "p", ll never be equivalent to any value (true or false) returned from that function.
##### Share on other sites

If he tried it, it would have evaluate to false, because "p", ll never be equivalent to any value (true or false) returned from that function.
In loose comparison (==) "p" is equal to true. In strict comparison (===) it is not. A loose comparison evaluates any value that is not false, an empty string, 0, null or undefined as true.
##### Share on other sites

Yes, thats true!

If the above statement is executed the way it is, what will be the value, passed into the alert() method? Thats what am talking about.
##### Share on other sites

Yes, thats true! If the above statement is executed the way it is, what will be the value, passed into the alert() method? Thats what am talking about.
Alright. Let's break it down. First, evaluate the condition for the ternary statement:alert("p"==vowel("i")?"true":"false") We're using loose comparison, so we'll convert each value to boolean. "p" converts to boolean true. "i" is a vowel, so the function will return true. We get:true == trueObviously, that's true. So the ternary will return the first of the two possible results: "true"This means the alert will look like this:alert("true");
##### Share on other sites

Well, it seems you geting things mixed up here. No value is being converted!

`'p'==true`

'p' is not converted to boolean true. We are just comparing two values here ok!

##### Share on other sites

In a loose comparison, each side is converted, or evaluated, as a boolean and then compared. Loose comparison does not care what type of data it's comparing. Only whether it is true-ish or false-ish. The following values are false-ish:0'' (an empty string)nullundefinedfalse Any of those values, when used in a loose comparison, will evaluate to boolean false.Anything else, will evaluate to boolean true. EDIT: Though, after a quick test, it appears that null and undefined are special. null is (loosely) equal to undefined, but null and undefined are not (loosely) equal to false, 0 or ''. EDIT2: Per the MDN reference on the == operator:

If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison.
##### Share on other sites

You telling me that if i do:

`'a'=='b'`

, i ll get: true?

##### Share on other sites

No, because they are the same type (ie, they are both strings). Only when they are different types do they get converted. I guess I never said that, but the line I quoted from the reference did. FWIW, here's a good reference table for loose comparisons:http://jsfiddle.net/rodneyrehm/fBKbT/

##### Share on other sites

`"a"==1`

You get: true? Lol, this is funny...Lol

Edited by CodeName