//! Challenge /** * You've just finished writing the last chapter for your novel when a virus suddenly * infects your document. It has swapped the i's and e's in ei words and capitalized * random letters. In today's challenge, you will write a function which will: * a) Remove the spelling errors in 'ei' words. (Examples of 'ei' words: their, caffein, deceive, weight) * b) Only capitalize the first letter of each sentence. * Make sure the rest of the sentence is in lowercase ** Example: * 'he haD iEght ShOTs of cAffIene.' ==> 'He had eight shots of caffeine.' * Good luck and happy coding~! */ // Long Version function reformat_sentence(sentence) { // Capitalize first letter of string const capitalize_first_letter = (word) => word.replace(/^\w/, firstLetter => firstLetter.toUpperCase()); // convert sentence to array of words, // switch 'ie' to 'ei' in each word, // convert each word lowercase sentence = sentence.split(' ').map(word => { word = word.toLowerCase(); return word.replace('ie', 'ei') }) // Capitalize the first letter of the first word in the sentence array sentence[0] = capitalize_first_letter(sentence[0]); // Convert sentence back to string and return return sentence.join(' '); } // Short Version const reformatSentence = sentence => sentence .split(' ') .map(word => word.toLowerCase().replace('ie', 'ei')) .join(' ') .replace(/^\w/, firstLetter => firstLetter.toUpperCase()); console.log("Long Version:", reformat_sentence('he haD iEght ShOTs of cAffIene.')) console.log("Short Version:", reformatSentence('he haD iEght ShOTs of cAffIene.')) // Output // Long Version: He had eight shots of caffeine. // Short Version: He had eight shots of caffeine.