diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/golf-code/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/golf-code/index.md index 37835a780b..69911ff01c 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/golf-code/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/golf-code/index.md @@ -115,6 +115,22 @@ golfScore(5, 4); ## Code explanation Since we already have an array defined in the variable `names` we can take advantage of it and use it for our return statements using indexes (eg: `names[0] is the first one`). That way, if you ever need to change a specific result you wouldn't need to look for it inside the function, it'd be at the beginning, in your array. +## ![:sunflower:](https://forum.freecodecamp.com/images/emoji/emoji_one/sunflower.png?v=3 ":sunflower:") Intermediate Code Solution: +(Using Multiple Conditional (Ternary) Operators) +```js + function golfScore(par, strokes) { + return (strokes == 1) ? names[0] : + (strokes <= par - 2) ? names[1] : + (strokes == par - 1) ? names[2] : + (strokes == par) ? names[3] : + (strokes == par + 1) ? names[4] : + (strokes == par + 2) ? names[5] : + (strokes >= par + 3) ? names[6] : + "Change Me"; + } +``` +![:rocket:](https://forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=3 ":rocket:") Run Code + ### Resources * Golf @@ -122,3 +138,4 @@ Since we already have an array defined in the variable `names` we can take advan * Challenge: Comparison with the Greater Than Equal To Operator * Challenge: Comparison with the Less Than Equal To Operator * ["Array" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) +* Use Multiple Conditional (Ternary) Operators