0

I would like to change if/else statement to loops in a function that receives a string and return to a new string which is changed the vowel letters to the next letter after it.

function changeVowels(str) {
        var alphabet = 'abefijopuvABEFIJOPUV';
        var strNew = ""
        for(var i = 0; i < str.length; i++){
           for(var j = 0; j < alphabet.length; j++){
             if(str[i] === alphabet[j] && j%2 === 0){
               strNew += alphabet[j+1]
            }
          }
          if(str[i]==='a' || str[i]==='i' || str[i]==='u'||str[i]==='e' || str[i]==='o'){}
          else if(str[i]==='A' || str[i]==='I' || str[i]==='U'|| str[i]==='E' || str[i]==='O'){}
          else{
           strNew+=str[i]
          }
        }
        return strNew
      }

console.log(changeVowels('Car'))

how to change if/else statement below to loops ?

if(str[i]==='a' || str[i]==='i' || str[i]==='u'||str[i]==='e' || str[i]==='o'){}
else if(str[i]==='A' || str[i]==='I' || str[i]==='U'|| str[i]==='E' || str[i]==='O'){}
else{strNew+=str[i]}

or i don't mind you change it besides loop, or make it shorter

3
  • 1
    not sure how you can turn ANY if/else to a loop - the two things are two different programming constructs Commented Sep 19, 2019 at 6:23
  • And a loop is really not the way to handle it Commented Sep 19, 2019 at 6:24
  • return str.split('').map(l => 'aeiou'.includes(l.toLowerCase()) ? String.fromCharCode(l.charCodeAt(0) + 1): l).join(''); Commented Sep 19, 2019 at 6:27

5 Answers 5

2

You could check if the character is a vowel and then take the next letter from a given string or the actual letter.

function changeVowels(str) {
  var alphabet = 'abefijopuvABEFIJOPUV';
  var strNew = ""
  for (var i = 0; i < str.length; i++) {
    if ('aeiou'.includes(str[i].toLowerCase())) {
      strNew += alphabet[alphabet.indexOf(str[i]) + 1];
    } else {
      strNew += str[i];
    }
  }
  return strNew;
}

console.log(changeVowels('Car'))

Sign up to request clarification or add additional context in comments.

Comments

1

Here's another fun way to do it

function changeVowels(str) {
    var x = {a:'b', e:'f', i:'j', o: 'p', u: 'v', A: 'B', E: 'F', I: 'J', O: 'P', U: 'V'};
    return str.split('').map(l => x[l] || l).join('');
}
console.log(changeVowels('Car'))

Or if you really want that alphabet var

function changeVowels(str) {
    var alphabet = 'abefijopuvABEFIJOPUV';
    var x = Object.fromEntries(alphabet.match(/../g).map(kv => kv.split('')));
    return str.split('').map(l => x[l] || l).join('');
}
console.log(changeVowels('Car'))

Explanation of var x = Object.fromEntries(alphabet.match(/../g).map(kv => kv.split('')));

alphabet.match(/../g) produces an array ['ab', 'ef', 'ij', ... etc]

.map(kv => kv.split('')) maps the above to [['a', 'b'], ['e', 'f'], ... etc

Object.fromEntries([['a', 'b'], ['e', 'f'] .... etc]) produces

{
    a: 'b',
    e: 'f',
    i: 'j',
    o: 'p',
    u: 'v',
    A: 'B',
    E: 'F',
    I: 'J',
    O: 'P',
    U: 'V'
};

same as the first snippet

Comments

0

You don't need loops to simplify this code

Both str[i]==='a' || str[i]==='i' || str[i]==='u'||str[i]==='e' || str[i]==='o' and str[i]==='A' || str[i]==='I' || str[i]==='U'|| str[i]==='E' || str[i]==='O' are about the same letters but with different capitalisation, so you can reduce your code in half by only considering one case (upper or lower) and converting anything to that. If you extract str[i] as a variable, you can further reduce the repetition:

var char = str[i].toUpperCase();
/* ... */
if (char==='A' || char==='I' || char==='U'|| char==='E' || char==='O') {}

you can further reduce the multiple ORs if you use an array and Array#includes alternatively String#includes will do the same but I'll use an array since it's a bit more universal for values, even if a string would be shorter.

if (['A', 'I', 'U', 'E', 'O'].includes(char)) {
  /* code */
} else {
  strNew+=str[i];
}

and if you want to do nothing in the first case, you can further just eliminate the else by inverting the condition:

if (!['A', 'I', 'U', 'E', 'O'].includes(char)) {
//  ^ invert the result
  strNew+=str[i];
}

2 Comments

strings also have 'includes' method
@JaromandaX yeah, I just added a note for why I used an array. basically, more universal, you can also use it for numbers, for example.
0

I will make it simpler for you

function changeVowels(str) {
        var alphabet = 'abefijopuvABEFIJOPUV';
        for(var i = 0; i < alphabet.length; i++){
           str = str.replace(new RegExp(alphabet[i], 'g'), alphabet[i+1]);
           i++
        }
        return str
}

console.log(changeVowels('Facebook'))

Comments

-1

separate your string and check if that exist in your alphabet variable. if it present means u get the next character or else return the same character and so on

var alphabet = 'abefijopuvABEFIJOPUV';

function changeVowels(str) {
const newArray = str.split('').map(element => {
  let index = alphabet.indexOf(element);
  if(index > -1) {
return alphabet.charAt(index + 1);
  } else {
return element;
  }
})
return newArray.join('');
}

console.log('final ---  ', changeVowels('Car'))

Comments

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.