Return Early Pattern for Functions Can't figure it out

Return Early Pattern for Functions Can't figure it out
0

Tell us what’s happening:
Having trouble understanding/making this lesson :3 can someone give a hint our explanation what to do?

Your code so far


// Setup
function abTest(a, b) {
  // Only change code below this line
  
if (a <= 0) {
  return = "undefined";
}
else (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 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.2 Safari/605.1.15.

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

You’ll find the hint on the page worthwhile reading.

1 Like

Modify the function abTest so that if a or b are less than 0 the function will immediately exit with a value of undefined .

Your conditions are wrong (a <= 0, b <= 0), read the highlighted text carefully

1 Like

You meant the hint that it is a keyword right? but all i can seem to find is this: https://www.w3schools.com/jsref/jsref_undefined.asp
which looks more like a string. could u elaborate onto what you meant?

The task is asking you to return undefined if either number is less than zero, but you have a <= 0, which is not saying a is less than zero.

1 Like

It’s just this:

return undefined;
1 Like

Just in general @KittyKora, be careful with mixing up <= and < – always have a think about exactly what you’re trying to compare each time. It’s really easy to make the mistake, and once you’ve made it anf you’ve been looking at the code for a while, it’s often quite hard to debug (everything looks correct at first glance)

1 Like

that is a good tip. I better get a good screenshot of them and keep them onto my desk :3

it doesn’t seem to work thou…

if (a <- 0) {

return undefined;

}

else (b <- 0) {

return undefined;

}

He means the difference between <= and <

<- is not valid

I tried using the < without the - which should be lesser then. That did’t work either…

The syntax of your else statement was wrong too in the first set of code that you posted.

1 Like

Sorry, I can see why you did that, the – was just punctuation in the sentence I wrote: it’s just < 0 (and else if not else, else on its own doesn’t take a condition)

1 Like

an other thing you missed is that there is a big difference between undefined, which is a reserved word with a specific value, and "undefined" which is just a normal string. You need to return undefined, not "undefined"

Second thing, remember you don’t assign to the return keyword, instead you put to the right of the word what shoukd be returned from the function
if you need to return for example1234 you write return 1234;

2 Likes

then after i tried changing it without the “” and no <- changing the syntax as well it still doesn’t pass

if(a < 0) {

}

else(b < 0) {

return undefined;

}

your first if statement doesn’t do nothing as there is nothing inside the {}, you need to add something in there too

you can also make it a single if statement if you use the OR operator to join the two conditions

condition one OR condition two can be written as condition one || condition two

alright like this then?

if(a < 0 || 0 < b)

return undefined;

No, the syntax is correct but you’ve decided to swap the conditions round for some reason, you did have b < 0 but you’ve decided to make it 0 < b which is the complete opposite.

And it’s

if (condition) {
  return something
} else if (condition) {
  return something else
}

If you wanted to put them in two blocks as you were trying to do originally

1 Like