ds-algo-study
Version:
Just experimenting with publishing a package
39 lines (28 loc) • 1.26 kB
JavaScript
/***********************************************************************
Write a function named: interrupter(interruptingWord). The interrupter function will
accept a word and return a function. When the function returned by interrupter
is invoked with a string the string will be returned with "interruptions".
Look below to see how this function is invoked:
let rudePerson = interrupter("what"); // => returns a function
console.log(rudePerson("how are you")); // prints "how what are what you"
console.log(rudePerson("I like pie")); // prints "I what like what pie"
Invoking the interrupter function again:
let rudePerson2 = interrupter("yo"); // => returns a function
console.log(rudePerson2("I love dogs")); // prints "I yo love yo dogs"
***********************************************************************/
function interrupter(interruptingWord) {
return function (sentence) {
let words = sentence.split(" ");
let newString = "";
for (let index = 0; index < words.length; index++) {
let word = words[index];
if (index === words.length - 1) {
newString += word;
} else {
newString += word + " " + interruptingWord + " ";
}
}
return newString;
};
}
module.exports = interrupter;