Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a JavaScript function reverseWordsInString(str) that accepts as a parameter a string and reverses
- the characters of every word in the string but leaves the words in the same order. Words are considered
- to be sequences of characters separated by spaces. Write a JavaScript program reverseWords.js that prints
- on the console the output. */
- "use strict";
- function reverseWordsInString(str) {
- var text = str.split(/\s+/g).filter(Boolean);
- for (var i = 0; i < text.length; i += 1) {
- var reversedStr = '';
- for (var j = text[i].length - 1; j >= 0; j -= 1) {
- reversedStr = reversedStr.concat(text[i].charAt(j))
- }
- text[i] = reversedStr;
- }
- return text.join(' ');
- }
- console.log(reverseWordsInString('Hello, how are you.'));
- console.log(reverseWordsInString('Life is pretty good, isnβt it?'));
- console.log(reverseWordsInString('JavaScript is a dynamic computer programming language.'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement