...

/

Solution Review: Make a Sentence

Solution Review: Make a Sentence

This lesson will explain the solution to the problem in the previous lesson.

Solution #

Press + to interact
const sentence = (conjunction, ...otherWords) => {
const commasJoiningWords = otherWords.slice(0,-1).join(", ");
const lastWord = otherWords.pop();
return `${commasJoiningWords} ${conjunction} ${lastWord}`;
}
const partialFnction = (func, conjunction) => {
return (...otherWords) => {
return func(conjunction, ...otherWords);
}
}
function test(conjunction, ...otherWords){
const partialSentence = partialFnction(sentence, conjunction);
const ans = partialSentence(...otherWords);
return ans
}
console.log(test("and", "apple", "mango", "peach"))
console.log(test("or" , "bike" , "car" , "train"))
console.log(test("but" , "fish" , "potatoes" , "spicy"))

Explanation #

In this challenge, you were given the implementation of the sentence function. Let’s understand it first.

Press + to interact
const sentence = (conjunction, ...otherWords) => {
const commasJoiningWords = otherWords.slice(0,-1).join(", ");
const lastWord = otherWords.pop();
return `${commasJoiningWords} ${conjunction} ${lastWord}`;
}

It takes two parameters: conjunction, the first word, and otherWords, all of the remaining words ...

Access this course and 1400+ top-rated courses and projects.