Created
September 3, 2021 09:48
-
-
Save AshishKapoor/d73cbb2640cfc4082079cd3319e2a9e1 to your computer and use it in GitHub Desktop.
Merge words solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a function that will allow you to pass in a string, with the ability to add to this with more function calls. When it is finally passed an empty argument return the full concatinated string of all arguments pased previously. | |
// For example: mergeWords("Hello")("World!")("how")("are")("you?")(); | |
// This will return the following: "Hello World! how are you?" | |
// Solution 1 | |
function mergeWords(string) { | |
return function(nextString) { | |
if (nextString === undefined) { | |
return string; | |
} else { | |
return mergeWords(string + ' ' + nextString); | |
} | |
} | |
} | |
console.log(mergeWords('There')('is')('no')('spoon.')()); | |
// Solution 2 | |
const mergeWords2 = string => nextString => | |
nextString === undefined ? | |
string : | |
mergeWords(`${string} ${nextString}`); | |
console.log(mergeWords2('There')('is')('no')('spoon.')()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment