0

I'm having trouble with Coderbyte's challenge

Using the JavaScript language, have the function LetterChanges(str) take the str parameter being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string.

Here's my code:

function LetterChanges(str) {
str = str.toLowerCase();
var al = "abcdefghijklmnopqrstuvwxyz";
var vo = "aeiou";
var newStr = "";
for (var i = 0;i < str.length;i++) {
    if (al.charAt(al.indexOf(str.charAt(i))) == "z") {
        newStr += "A";
    }
    else if (str.charAt(i) == " "){
        newStr += " ";
    }
    else {
        if (al.charAt(al.indexOf(str.charAt(i))+1) == vo.charAt(vo.indexOf(str.charAt(i)))) {
            newStr += vo.charAt(vo.indexOf(str.charAt(i))+1).toUpperCase();
        }
        else {
            newStr += al.charAt(al.indexOf(str.charAt(i))+1)
        }
    }
}
   console.log(newStr);
}
LetterChanges("Argument goes here")

This is returning the following into the console: bshvnfou hpft ifsf

but what I need to be returned is: bshvnfOU hpft Ifsf

I can't figure out why my .toUpperCase() isn't working. Any help that you can provide is very much appreciated!

4

3 回答 3

1

Your problem isn't that toUpperCase() isn't working, it's that your code never gets to that point.

Your

if (al.charAt(al.indexOf(str.charAt(i))+1) == vo.charAt(vo.indexOf(str.charAt(i))))

never passes, because of that +1. There's no need for that there, and if you remove it, uppercase vowels will appear. There are other problems with your code beyond that point, but I'll let you solve them as part of the challenge :-)

于 2013-11-07T23:42:37.643 回答
1

Wow, it's crazy looking at the code I wrote a year ago! Reviewing this question, here's what I came up with (disclaimer: still getting comfortable with ES6):

function convertText (str) {
    const al = 'abcdefghijklmnopqrstuvwxyz';
    return str.toLowerCase().split('').map(letter => {
        if (letter.match(/\W/)) return letter;
        const index = al.indexOf(letter),
            newLetter = (al[index + 1] || al[0]);
        return newLetter.match(/[aeiou]/) ? newLetter.toUpperCase() : newLetter;
    }).join('');
}

And by using character codes:

function letterChanges (str) {
    const v = {d: 69, h: 73, n: 79, t: 85, z: 65};
    return str.toLowerCase().split('').map(l => 
        (l.match(/\W/) ? l : String.fromCharCode(v[l] || l.charCodeAt(0) + 1))
    ).join('');
}

There're probably better ways, but still a fun question! Thanks again for your help, @MattDiamant

于 2015-11-05T00:39:27.997 回答
0

Here's my solution. It does not satisfy the z becomes a requirement but it passes Coderbyte's test as seen in the screenshot below.

function LetterChanges(str) {
    return str
        .replace(/[a-z]/gi, c => String.fromCharCode(c.charCodeAt(0) + 1))
        .replace(/[aeiou]/gi, c => c.toUpperCase());
}

// keep this function call here
LetterChanges(readline());

Letter Changes Javascript Solution

于 2019-12-16T22:39:32.897 回答