JavaScript Function to Reverse Words with 5 or More Letters
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/mBTO 著作权归作者所有。请勿转载和采集!