用js实现Write a function that takes in a string of one or more words and returns the same string but with all five or more letter words reversed Just like the name of this Kata Strings passed in will con
function reverseLongWords(str) { // Split the string into an array of words const words = str.split(' ');
// Loop through each word and reverse if it's length is 5 or greater for (let i = 0; i < words.length; i++) { if (words[i].length >= 5) { words[i] = words[i].split('').reverse().join(''); } }
// Join the words back into a string and return it return words.join(' '); }
// Example usage: reverseLongWords('Hello world'); // returns 'Hello world' reverseLongWords('This is a test'); // returns 'sihT is a tset' reverseLongWords('The quick brown fox jumps over the lazy dog'); // returns 'The quick nworb xof spmuj revo eht yzal god'
原文地址: https://www.cveoy.top/t/topic/bbzj 著作权归作者所有。请勿转载和采集!