freeCodeCamp Challenge Guide: Return Early Pattern for Functions

freeCodeCamp Challenge Guide: Return Early Pattern for Functions
0

#1

When a return statement is reached, the execution of the current function stops and control returns to the calling location.

Example

function myFun() {
  console.log("Hello");
  return "World";
  console.log("byebye")
}
myFun();

The above outputs “Hello” to the console, returns “World”, but "byebye" is never output, because the function exits at the return statement.


#2

Horrible “Hint” It is the EXACT same thing on the other page!! Come on now.


#3

HINT: Use if statement to check if the condition fails aka variable 1 less than 0 or variable 2 less than 0 then return undefined else success return


#4

I tried that and when I get to the else statement the return statement
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2)); does not match up with the scope of the else. When I try to add another return statement into the else statement the output is nothing.


#5

All you need is an if statement.
for example
if x === z || y ===z{
return;
}


#7

Too bad they never explained this, took me quite some time to figure this1 out. The gitter chat helped met out eventually :sweat_smile:


#8

// 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));
}


#9

// 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);

I have this and it is still not working?


#10

Hey, in case you haven’t solved it yet, return undefined; should go without quotes :wink:


#11

Correct code is,
if(a < 0 || b < 0){
return undefined;
}
Above code is defined return value when condition found true, undefined is JavaScript’s primitive types which does not required string sign to execute.


#12

Here is the correct answer:
if(a < 0 || b < 0){
return undefined;
}


#13

not sure which is correct - what do you guys think? (both solve the exercise):

  • return undefined;
    OR
  • return;

I guess it’s the second one - just return;


#14

After trying for a while, I finally got it to work.
FIRST i had:
IF (a<0 || b< 0){
return;
}
This didn’t worked, but then I did this:
IF (a<=0 || b< =0){
return;
}
Then it worked, the only difference being <=.


#15

what is immediately exit with a value of undefined.
i clear the quiz but it is not returning anything when a or b is less than 0


#16

The below also works using && instead of ||

// Setup
function abTest(a, b) {
// Only change code below this line

if(a > 0 && b> 0) {}
else {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);


#17

Feeling sorry for myself not figuring this out without using the Hint button :joy:


#18

for me using && did not work, and going to the detail the instructions say “Modify the function abTest so that if a or b are less than 0 the function will immediately exit with a value of undefined.”

So I would suggest to use || instead of &&.

if (a < 0 || b < 0)
return undefined;


#19

Even though && is saying “and” rather than “or” it accomplishes the same goal/means the same thing at the end of the day. Think of it this way: In order for (a > 0 && b> 0) to evaluate as true, it means BOTH a and b must be greater than zero – thus if one of those is less than zero, then the function will immediately exit with a value of undefined. You can almost always create and && that will produce the same result as an || and vica versa.


#20

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


#21

look at the given hint what it say!.undefined is a KEYWORD!.