javascript - For loop is stopping at the first letter in string? -


this rovarspraket function. loop not iterating on other letters in phrase. stops @ 't' first letter in string. function returning 'tot' instead of full string of 'tothohisos isos fofunon'. i've been staring @ long. suggestions help. i'm novice javascript way.

function translate (phrase) {   var splitphrase = phrase.split().join();   var letter = splitphrase[i];   var vowels =  ['a', 'e', 'i', 'o', 'u'];   var output = '';    (i = 0; < phrase.length; i++) {     if(vowels.indexof(letter) === -1) {       output = letter + 'o' + letter;    } else {    }     return output } };  undefined  translate('this fun')  "tot" 

there handful of mistakes or things may have overlooked in order reach final desired result of "tothohisos isos fofunon":

  1. you aren't appending data output, you're overwriting it.
  2. you return within loop, stopping loop after first letter
  3. you using index i before exists.
  4. unnecessary splitting , joining
  5. not handling case of when letter is vowel
  6. not handling spaces

here's corrected version:

function translate (phrase) {        var vowels =  ['a', 'e', 'i', 'o', 'u', ' ']; // handle spaces      var output = '';        (var = 0; < phrase.length; i++) { // use var declare index variable         var letter = phrase[i]; // array notation accesses character in position         if (vowels.indexof(letter) === -1) {             output += letter + 'o' + letter; // += appends         } else {             output += letter; // handle vowels & spaces         }      }        return output; // return outside loop  };    alert(translate('this fun'));


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -