curriculum/challenges/english/blocks/daily-coding-challenges-javascript/68d2ba1468508398389487ce.md
Given a sentence, return a version of it that sounds like advice from a wise teacher using the following rules:
"have", "must", "are", "will", "can".For example, given "You must speak wisely." return "Speak wisely, you must."
wiseSpeak("You must speak wisely.") should return "Speak wisely, you must."
assert.equal(wiseSpeak("You must speak wisely."), "Speak wisely, you must.");
wiseSpeak("You can do it!") should return "Do it, you can!"
assert.equal(wiseSpeak("You can do it!"), "Do it, you can!");
wiseSpeak("Do you think you will complete this?") should return "Complete this, do you think you will?"
assert.equal(wiseSpeak("Do you think you will complete this?"), "Complete this, do you think you will?");
wiseSpeak("All your base are belong to us.") should return "Belong to us, all your base are."
assert.equal(wiseSpeak("All your base are belong to us."), "Belong to us, all your base are.");
wiseSpeak("You have much to learn.") should return "Much to learn, you have."
assert.equal(wiseSpeak("You have much to learn."), "Much to learn, you have.");
function wiseSpeak(sentence) {
return sentence;
}
function wiseSpeak(sentence) {
const triggers = ["have", "must", "are", "will", "can"];
const punctuation = sentence[sentence.length - 1];
const words = sentence.split(" ");
const triggerIndex = words.findIndex(w => triggers.includes(w));
const toMove = words.slice(0, triggerIndex + 1).map(w => w.toLowerCase());
const remaining = words.slice(triggerIndex + 1);
const newStart = remaining.map((w, i) => {
return i === 0 ? w[0].toUpperCase() + w.slice(1) :
i === remaining.length - 1 ? w.slice(0, w.length - 1) : w;
})
return newStart.join(" ") + ", " + toMove.join(" ") + punctuation;
}