SnakesBite101 Posted June 20, 2012 Share Posted June 20, 2012 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> Link to comment Share on other sites More sharing options...
Don E Posted June 20, 2012 Share Posted June 20, 2012 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 Link to comment Share on other sites More sharing options...
ShadowMage Posted June 20, 2012 Share Posted June 20, 2012 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. Link to comment Share on other sites More sharing options...
SnakesBite101 Posted June 20, 2012 Author Share Posted June 20, 2012 oh i see. thank you guys Link to comment Share on other sites More sharing options...
justsomeguy Posted June 21, 2012 Share Posted June 21, 2012 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. Link to comment Share on other sites More sharing options...
SnakesBite101 Posted June 21, 2012 Author Share Posted June 21, 2012 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 Link to comment Share on other sites More sharing options...
ShadowMage Posted June 21, 2012 Share Posted June 21, 2012 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;} Link to comment Share on other sites More sharing options...
SnakesBite101 Posted June 21, 2012 Author Share Posted June 21, 2012 thanks guys. i really appreciate all the help Link to comment Share on other sites More sharing options...
SnakesBite101 Posted June 22, 2012 Author Share Posted June 22, 2012 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? Link to comment Share on other sites More sharing options...
Ingolme Posted June 22, 2012 Share Posted June 22, 2012 You forgot to wrap the function in braces: function vowel(i) {} Link to comment Share on other sites More sharing options...
SnakesBite101 Posted June 22, 2012 Author Share Posted June 22, 2012 the code still doesnt seem to work... no response Link to comment Share on other sites More sharing options...
Ingolme Posted June 22, 2012 Share Posted June 22, 2012 When you call the function, you should do it like this: alert( vowel("p") ? "true" : "false" ); Link to comment Share on other sites More sharing options...
eTianbun Posted June 22, 2012 Share Posted June 22, 2012 alert("p"==vowel("i")?"true":"false") Link to comment Share on other sites More sharing options...
Ingolme Posted June 22, 2012 Share Posted June 22, 2012 alert("p"==vowel("i")?"true":"false") 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. Link to comment Share on other sites More sharing options...
SnakesBite101 Posted June 22, 2012 Author Share Posted June 22, 2012 thanks for the help guys. it works ! Link to comment Share on other sites More sharing options...
eTianbun Posted June 22, 2012 Share Posted June 22, 2012 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. Link to comment Share on other sites More sharing options...
eTianbun Posted June 22, 2012 Share Posted June 22, 2012 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. Link to comment Share on other sites More sharing options...
ShadowMage Posted June 22, 2012 Share Posted June 22, 2012 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. Link to comment Share on other sites More sharing options...
eTianbun Posted June 22, 2012 Share Posted June 22, 2012 Yes, thats true! alert("p"==vowel("i")?"true":"false") 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. Link to comment Share on other sites More sharing options...
ShadowMage Posted June 22, 2012 Share Posted June 22, 2012 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"); Link to comment Share on other sites More sharing options...
eTianbun Posted June 22, 2012 Share Posted June 22, 2012 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! Link to comment Share on other sites More sharing options...
ShadowMage Posted June 22, 2012 Share Posted June 22, 2012 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. Link to comment Share on other sites More sharing options...
eTianbun Posted June 22, 2012 Share Posted June 22, 2012 You telling me that if i do: 'a'=='b' , i ll get: true? Link to comment Share on other sites More sharing options...
ShadowMage Posted June 22, 2012 Share Posted June 22, 2012 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/ Link to comment Share on other sites More sharing options...
eTianbun Posted June 22, 2012 Share Posted June 22, 2012 Have you tried it yourself?EDIT:Ok, what about this: "a"==1 You get: true? Lol, this is funny...Lol Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.