From 892232b70e00177d8d4d822186eff0628e19483f Mon Sep 17 00:00:00 2001 From: greggubarev Date: Tue, 16 Oct 2018 07:46:00 +0400 Subject: [PATCH] Javascript: add hint to Return Early Pattern for Functions (#19118) Add hint to Return Early Pattern for Functions (https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/return-early-pattern-for-functions/ and https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-javascript/return-early-pattern-for-functions/) --- .../index.md | 58 ++++++++++++++++--- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/client/src/pages/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/return-early-pattern-for-functions/index.md b/client/src/pages/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/return-early-pattern-for-functions/index.md index 0ada89f034..8a797c7633 100644 --- a/client/src/pages/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/return-early-pattern-for-functions/index.md +++ b/client/src/pages/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/return-early-pattern-for-functions/index.md @@ -1,10 +1,54 @@ ---- -title: Return Early Pattern for Functions ---- + ## Return Early Pattern for Functions -This is a stub. Help our community expand it. - -This quick style guide will help ensure your pull request gets accepted. - + +Here’s a setup: + +```javascript +// Setup +function abTest(a, b) { + // Only change code below this line + + // 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); +``` + +We need 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```. + +We add in body of function simple ```if``` statement, which, under the conditions "if ```a``` or ```b``` are less than ```0``` - immediately exit with a value of ```undefined```": + +```javascript + if (a < 0 || b < 0) { + return undefined; + } +``` + +Now, if ```a``` or ```b``` are less than ```0``` - function exit with a value of ```undefined```, in other cases - + +```javascript + return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2)); + } +``` + + Here’s a full solution: + + ```javascript + // 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); + ```