用javascript实现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
function reverseWords(str) { // Split the string into an array of words let words = str.split(' ');
// Loop through each word in the array for (let i = 0; i < words.length; i++) { // If the word is 5 or more letters long, reverse it if (words[i].length >= 5) { words[i] = words[i].split('').reverse().join(''); } }
// Join the array of words back into a string and return it return words.join(' '); }
// Example usage: console.log(reverseWords('This is a test')); // Output: This is a tset console.log(reverseWords('Hello world')); // Output: olleH dlrow console.log(reverseWords('Welcome to the jungle')); // Output: emocleW to the elgnuj
原文地址: https://www.cveoy.top/t/topic/bbzq 著作权归作者所有。请勿转载和采集!