Adds number to each word in a sentence


/ Published in: JavaScript
Save to your folder(s)

This function adds number to each word in a sentence.


Copy this code and paste it in your HTML
  1. function splitWordNumero(bar){
  2. var foo = bar;
  3. //var foo = "The quick brown fox jumps over the lazy dog";
  4. var temp = [];
  5. var res = [];
  6.  
  7. temp = foo.split(' ');
  8. var j = temp.length;
  9.  
  10. console.log(j);
  11.  
  12. for(i=0; i<j ; i++) {
  13. res.push(temp[i]+(i+1));
  14. }
  15.  
  16. var ans = res.join(' ');
  17.  
  18. //console.log(res.toString());
  19. console.log (ans);
  20. document.getElementById('hello').innerHTML = ans;
  21.  
  22. }
  23. //pasws value here
  24. splitWordNumero("The quick brown fox jumps over the lazy dog");

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.