Reputation: 11
First I want to prove whether there is a vowel in the sentence, if there is a vowel is replaced by the next letter. Letters that are not vowels are not replaced but remain the same in the output.
The results of the coding that I made became inappropriate.
function changeVocals(str){
var split = str.split('')
var vocal = 'aiueo'
var change = 'bjvfp'
var a = ''
for (var i = 0; i < split.length; i++) {
for (var j = 0; j < vocal.length; j++) {
if (vocal[j] === split[i]) {
a = a + change
} else {
a = a + split[i]
}
}
}
return a
}
console.log(changeVocals('Alexa')); //'Blfxb'
I expect the output of 'Alexa' to be 'Blfxb', but the actual output is the sentence appears to be repetitive.
Actual Output: AAAAAllllleeebjvfpexxxxxbjvfpaaaa
Upvotes: 1
Views: 1128
Reputation: 2111
function convertot(string) {
const vowels = ['a', 'e', 'i', 'o', 'u'];
let str = '';
for (let i = 0; i < string.length; i += 1) {
const element = string[i];
const isPresent = vowels.findIndex((f) => { return f === element.toLowerCase(); });
if (isPresent !== -1) {
str += String.fromCharCode(element.charCodeAt() + 1);
} else {
str += element;
}
}
return str;
}
const text = 'Alexa';
const modified = convertot(text);
console.log(text);
console.log(modified);
Upvotes: 0
Reputation: 118271
There are couple of mistakes I found in your attempt. I tried to fix that to make it working as you see below. But your attempt was really close. I hope my comments will help you to understand that 2 area where you made the mistakes.
function changeVocals(str) {
var split = str.split('');
var vocal = 'aiueo';
var change = 'bjvfp';
var a = ''
for (var i = 0; i < split.length; i++) {
for (var j = 0; j < vocal.length; j++) {
// When there is a match we will not continue
// so break out of the loop.
if (vocal[j] === split[i]) {
// this test if the match is lowercase to lowercase
a = a + change[j];
break;
} else if (vocal[j].toUpperCase() === split[i]) {
// this test if the match is uppercase to uppercase
a = a + change[j].toUpperCase();
break;
}
}
// When there is no match found we just copy the input
// character to the resulting string.
// We figured there is no match found when we see
// that the resulting string length is less than the current
// value of i
if (a.length < i + 1) a = a + split[i];
}
return a;
}
console.log(changeVocals('Alexa'));
//'Blfxb'
Upvotes: 0
Reputation: 2146
function changeVocals(str){
return [...str].map(letter => "aeiouAEIOU".contains(letter) ? String.fromCharCode(letter.charCodeAt(0)+1) : letter).join('');
}
changeVocals("Alexa");
But somehow, code snippet does not recognize contains
function
Upvotes: 0
Reputation: 2564
It can be done with map
;
var vowels = ['a', 'e', 'i', 'o', 'u'];
var text = 'Alexa';
var result = text.split('').map(x => vowels.indexOf(x.toLowerCase())>=0 ? String.fromCharCode(x.charCodeAt()+1) : x).join('');
console.log(result);
Upvotes: 0
Reputation: 37755
You can use an object for mapping the values, and replace the matching value based on the case
const mapper = {
a: 'b', e: 'f', i: 'j', o: 'p', u: 'v',
A: 'B', E: 'F', I: 'J', O: 'P', U: 'V'
}
const changeVocals = (string) => {
return string.replace(/[aeiou]/gi, match => mapper[match])
}
console.log(changeVocals('Alexa'));
Upvotes: 1