Advertisement
Guest User

OdwracanieTekstu

a guest
Jun 24th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function reverse(text) {
  2.   console.log(text);
  3.   var wyraz = "";
  4.   var odwroconyTekst = "";
  5.  
  6.   for(var i=0; i<text.length; i++) {
  7.     //jesli nie napotkamy spacji dodajemy znaki do wyrazu
  8.     if (text[i] != " " && i != text.length-1) {
  9.       wyraz += text[i]
  10.     }
  11.    
  12.     //ostatni znak wyrazu
  13.     else if(i == text.length-1) {
  14.       wyraz += text[i]
  15.       for(var j=wyraz.length-1; j>=0; j--) {
  16.         odwroconyTekst += wyraz[j];
  17.       }
  18.       wyraz = "";
  19.     }
  20.    
  21.     //jesli napotkamy spacje
  22.     else {
  23.       for(var j=wyraz.length-1; j>=0; j--) {
  24.         odwroconyTekst += wyraz[j];
  25.       }
  26.       odwroconyTekst+=" ";
  27.       wyraz = "";
  28.     }
  29.   }
  30.   return odwroconyTekst;
  31. }
  32.  
  33. var text = "Tekst do przetestowania";
  34. var textTwo = "Hello World"
  35. console.log(reverse(text));
  36. console.log(reverse(textTwo));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement