Returning undefined

Tell us what’s happening:
I am supposed to, “Modify the function abTest so that if a or b are less than 0 the function will immediately exit with a value of undefined.” but when I do the code one way it returns two values undefined and when I switch it around then everything runs but not the undefined. Not sure what I am missing. Help?!

Your code so far


// Setup
function abTest(a, b) {
  // Only change code below this line
  console.log("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);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/return-early-pattern-for-functions

1 Like

Well, this is an example of what’s known as “the early return pattern”. If we have tested for an error condition and entered that error condition, then we return early with an error (or in this case, with an undefined).

The issue with your code is, a console.log is not a check of anything. You want to check for two conditions:

if a less than zero OR b less than zero {
  return undefined;
}
// if we get to this point, then both a and b are zero or greater, and any normal
//   processing can happen.

Note that the above code won’t run. It is not valid javascript, it is just “pseudocode”. It shows the intent behind the code. How would you write it?

Another way to accomplish this challenge (in pseudo code) is:

if a is greater or equal to 0 AND b is greater or equal to 0 {
  return Math.round( .......................... ,2));
}

One interesting thing about JavaScript functions is if you do not explicitly return a value, then the value of undefined is returned by the function. For example, the following function would return undefined if x is not equal 5.

function myFunction (x) {
  if (x === 5) {
    return "X is equal to 5";
  }
}

myFunction(5); // returns 'X is equal to 5'
myFunction(7); // returns undefined
1 Like

My only issue with that is… you’d have to edit the code below the // Only change code above this line line. Otherwise, you’re absolutely right.

if again you are not done then:
Add if (a is less than or equal to zero and b less than or equal to zero) Enclose the { return undefined } means you are done :star_struck:

that was sort of @RandellDawson’s point: if you don’t explicitly provide a return value, the default behavior of a function is to return undefined.

Every function returns something. It may not be something you want or need, it may not be something you consume, it may not even be something you think about… but every function returns something. Even if, by default, it returns undefined.

So with his example:

he is only specifying a return value for those cases where a AND b are both not negative. If that if statement doesn’t pass, the function will continue on and the function will return… its default return value. Which is undefined.

Still isn’t passing every area… Here is what I have at this point and it is only passing for the numbers above 0 but not showing undefined for the numbers less than 0. Any ideas?


// Setup

function abTest(a, b) {

// Only change code below this line

if (a<=0 && 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);

what you’re saying in your if statement is “If a less than zero AND b less than zero…”

Is that what you want? I might suggest you look at the OR operator instead ( || ).

Right now, you’re stating “if BOTH these conditions are true, then do this thing.” In this case, though, you want to be saying “if ANY of these conditions are true, then do this thing.” It’s called a logical or.

Great suggestion! I just tried it and it didn’t work…still passing for positive numbers but not for the negatives.

1 Like

You’re very very close. Look at what you’re returning. You are saying “return the string ‘undefined’”, when the challenge is saying "return the value undefined".

undefined is a thing in javascript, totally separate from "undefined". Get rid of the quote marks around it, I think it might be good.

YES! This was the key! Thanks so much! Finally passed thanks to your suggestions :slight_smile:

1 Like