0

Trying a fun problem of replacing vowels in a string with the next vowel in line aka a->e, e->i, i->o, o->u, not accounting for "u". Starting with an array instead of a string. My second loop (to iterate over vowel array elements) is ignoring my "j

var vowelChange = function(vowelArray, stringToChange) {
for (var i = 0; i<stringToChange.length; i++) {
    for (var j = 0; j<vowelArray.length; j++) {
      if (stringToChange[i]===vowelArray[j]) {
        var newCharacter = vowelArray[j+1]
          stringToChange[i] = newCharacter
          i++
      }
    }
}
return stringToChange
};

I'm using node-debug to set breakpoints in a browser, and j is looping to 5 before starting over at 0. I get the correct output, but j should stop at 4...

EDIT

Can somebody explain how I'm using join incorrectly, because I can't get my function to output a string instead of just an array.

var vowelChange = function(vowelArray, stringToChange) {
for (var i = 0; i<stringToChange.length; i++) {
    for (var j = 0; j<vowelArray.length-1; j++) {
      if (stringToChange[i]===vowelArray[j]) {
          stringToChange[i] = vowelArray[j+1]
          break
      }
    }
  }
  stringToChange = stringToChange.join('')
  return stringToChange
};


var vowels = ['a','e','i','o','u']
var firstName = ['t', 'e', 's', 't']

vowelChange(vowels, firstName)
console.log(firstName)
5
  • 1
    why are you doing i++ in the j loop? Commented Apr 26, 2016 at 17:27
  • If I don't, then when I iterate back over it, the loop will recognize that the "e" was replaced with an "i", will go back over that (now changed) "i", and will change it to "o". The same thing will happen again, and it will replace the "o" with a "u". Ultimately, it'll change it to "undefined" since if j=4, j+1 is undefined in the vowels array Commented Apr 26, 2016 at 17:29
  • yeah - j will go to 5 and fail the compare 5 < 5 Commented Apr 26, 2016 at 17:29
  • 2
    Just put a 'break' inside your if to break out of the j loop and continue i Commented Apr 26, 2016 at 17:30
  • @n8wrl put a break in there instead of incrementing i. I do see now that it fails the comparison when j hits 5, I'm coming from the Xcode debugger where it won't even show j hitting 5, if I were writing this in C or Objective-C Commented Apr 26, 2016 at 17:35

2 Answers 2

1

Assuming vowelArray is 0-indexed...

var vowelChange = function(vowelArray, stringToChange) {
    for (var i = 0; i<stringToChange.length; i++) {
        for (var j = 0; j<vowelArray.length - 1; j++) {
            if (stringToChange[i]===vowelArray[j]) {
                stringToChange[i] = vowelArray[j+1];
                break;
            }
        }
    }
    return stringToChange
};
Sign up to request clarification or add additional context in comments.

Comments

0

In JavaScript, strings are immutable objects, which means that the characters within them may not be changed and that any operations on strings actually create new strings.

So,if you try to change any index of the string, the original string won't change

node
> str = "hello this is dummy string";
'hello this is dummy string'
> str[0] = "w";
'w'
> str
'hello this is dummy string'

So, stringToChange[i] = vowelArray[j+1]; won't work

Could split the string and then join

var vowelChange = function(vowelArray, stringToChange) {
    stringToChange = stringToChange.split('');
    for(var i=0; i<stringToChange.length;i++){
        for(var j=0;j<vowelArray.length-1;j++){
            if(stringToChange[i] == vowelArray[j]){
                stringToChange[i] = vowelArray[j+1];
                break;
            }
        }
    }
    stringToChange = stringToChange.join('');
    return stringToChange;
};

Example

4 Comments

I actually passed an array to my function instead of a string. The next step would have been to pass a string, split it, and join it before returning it.
Check out the edit I've made in my post and tell me what you think
Nothing wrong with the join, just assign the output of your function to a var and print it.
You are changing the reference of stringToChange that is all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.