简体   繁体   中英

just change the vowel to the next letter in javascript

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

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'));

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);

 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

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'

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM