Sunday, 14 June 2015

Node.js - Reverse of words in a string.

Prerequisites:
Node.js and NPM is already installed, click below link for installing Node.js and NPM.
http://automation-home.blogspot.in/2015/05/installing-nodejs-and-npm-on-windows.html

Procedure-1:
FileName: script1.js
var str = "Welcome to Automation-Home";
var result = "";
var wordArr = str.split(" ");
for(var i=wordArr.length-1; i>=0;i--){
   result = result+wordArr[i];
   if(i>0) result = result + " ";
}
console.log(result);

Procedure-2:
FileName: script2.js
var str = "Automation-Home welcomes you";
var result = str.split(" ").reverse().join(" ");
console.log(result);

Procedure-3:
FileName: script3.js
var rev = function(str){
  return str.split(" ").reverse().join(" ");
}
console.log(rev("NodeJS javascript"));

Watch Demo

1 comment: