Jump to content

please help on this simple function


SnakesBite101

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>

Link to comment
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.

Edited by ShadowMage
Link to comment
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.

  • Like 1
Link to comment
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
Link to comment
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
Link to comment
Share on other sites

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

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

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

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.
Edited by ShadowMage
Link to comment
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/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...