// Setup
function abTest(a, b) {
// Only change code below this line
switch(a, b < 0){
case a:
case b:
console.log("undefined")
return "undefined";
}
// Only change code above this line
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
}
// Change values below to test your code
abTest(2,2);
Modify the function abTest so that if a or b are less than 0 the function will immediately exit with a value of undefined.
Are you familiar with how to chain together expressions in javascript yet? There are a few things to remember:
To express if (this is true OR that is true) we would use if (this == true || that == true);
to express if (this is true AND that is true), we would use if(this==true && that==true)
So how would you show if a < zero or b < zero, do you think?
And be careful – are you being told to return a string that says “undefined”, or are you told to return undefined? They are two very very different things.
The challenge is to return undefined if a or b are negative
A switch statement is not a good choice because it uses strict comparison, and can’t be used if you want to say that something must be less than something else to do something
So the example I was giving yesterday may have been too “theoretical.” Let me make a more concrete example.
Suppose I wished to check whether my userName is snowmonkey and my age is over 40. That would be an AND condition, as both things must be true, so it would look something like:
if (userName == "snowmonkey" && userAge > 40){ ... }
And that’s great, but that’s not really what you need. You are looking for EITHER thing to fail. So that would be more like “if my userName is snowmonkey OR my userAge is over 40”, and that might look more like:
In that one, if either one or both conditions are met, then we can go ahead. What are the two conditions you have to meet? Can you see a way this might apply?
Modify the function abTest so that if a or b are less than 0 the function will immediately exit with a value of undefined.
// Setup
function abTest(a, b) {
// Only change code below this line
if( a || b <0){
return "undefined";
}
// Only change code above this line
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
}
// Change values below to test your code
abTest(2,2);
// Setup
function abTest(a, b) {
// Only change code below this line
if( a && b <0){
return "undefined";
}
// Only change code above this line
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
}
// Change values below to test your code
abTest(2,2);
// running tests
abTest(-2,2) should return undefined
abTest(2,-2) should return undefined
// tests completed
You were closer with the last post. It is an or operation, not an and, but the thing is, you are thinking you can check “if a or b are less than zero”, but you can’t. Just can’t be done.
You must check “if a is less than zero OR b is less than zero”. Each side of that is a totally separate thing. There is no connection between them, and there is no shortcut.
So use the || instead of the &&. Now tell me:
How would you code "a is less than zero"?
How would you code "b is less than zero"?
When you have each piece, separately, then you can use the || to join them.
And that’s exactly right. But now, go back to the very first response I gave you in this thread, and read the last two lines. You’re returning “undefined” – is that what it asks you to return?
undefined is a reserved keyword in javascript, it actually represents something. This is taken from the Mozilla Developer docs (MDN) about the keyword undefined:
A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value.
So you’re returning “undefined”, the string, while the test wants to see undefined, the reserved keyword. How would you get there?
Actually thats where i am also stuck and i am required to return undefined and my codes so far
// Setup
function abTest(a, b) {
// Only change code below this line
if(a<0 || b<0){
return "";
}
// Only change code above this line
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
}
// Change values below to test your code
abTest(2,2);
and my error
abTest(-2,2) should return undefined
abTest(2,-2) should return undefined