Basic JavaScript: Return Early Pattern for unctions

Tell us what’s happening:

this is the answer on the video and in the hint…whats wrong. please help

Your code so far


/*-// 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
console.log(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/80.0.3987.116 Safari/537.36.

Challenge: Return Early Pattern for Functions

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

What do the failing tests say? What errors are you getting? What is behaving differently than you expect? What else have you tried.

1 | /*-// Setup
| ^
2 | function abTest(a, b) {
3 | // Only change code below this line
4 |

Why did you add those extra characters to the first line?

// running tests

abTest(-2,2)

should return

undefined
abTest(2,-2)

should return

undefined

// tests completed // console output

thats what it says now. I fixed the first line

Have you changed anything else in your code? If you fixed the comment, then your code should pass.

// running tests

abTest(-2,2)

should return

undefined
abTest(2,-2)

should return

undefined

// tests completed // console output

this is my current code. it will not pass

please show your current code in the editor

// 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
console.log(abTest(2,2));

The code you had in your initial post should have been correct. Have you tried resetting the challenge and only putting in the if statement?

undefined should not be in quotes since you are wanting the keyword, not the string “undefined”.

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums