Missing Letters Algorithem

Tell us what’s happening:
This is the Missing Letters problem from the Intermediate Algorithms Scripting section of the Javascript Data Structures and Algorithms course.

Could I get a hint as to why my function doesn’t return ‘undefined’, but returns ‘{’ instead?

**Your code so far**

function fearNotLetter(str) {
for (let i = 0; i < str.length; i++) {
    let x = str.charCodeAt(i);
    let y = str.charCodeAt(i + 1);
    if (x + 1 != y) {
        return String.fromCharCode(x + 1);
    }
}
return undefined;
}
fearNotLetter("abce");
**Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0

Challenge: Missing letters

Link to the challenge:

This seems to solve the issue.

function fearNotLetter(str) {

    if (str.length == 26) {

        return undefined;

    }

    for (let i = 0; i < str.length; i++) {

        let x = str.charCodeAt(i);

        let y = str.charCodeAt(i + 1);

        if (x + 1 != y) {

            return String.fromCharCode(x + 1);

        }

    }

}

What’s the character after z?

I’m not sure exactly what is happening, but I am concerned about running your loop too for.

I added spoiler tags to your code.

I wouldn’t make a special case like that. I’d fix your loop bound instead.

I hope this clears things up.

We loop through each character of our input string, assigning the character code of the ith element to x and that of it’s successor to y.

Note that the character code between any two elements of our string differs at most by 1 if there is no missing letter of the alphabet.

In light of this, we proceed by checking if this is the case for each element of our string.
If it is the case, then the ‘if’ condition evaluates to false, and so the for loop runs again, this time incrementing the value of i by 1.
If it’s not the case, then that means we do indeed have a missing character.
Recall that there is at most a single missing character, otherwise we will have to resort to some form of recursion to account for multiple missing characters.
This means that the character code of the missing letter must be 1 less than that of the i + 1th element, which is same as 1 greater than that of the ith element.
Hence, the character code of the missing letter is the character code of the ith element plus 1, namely, x + 1.

function fearNotLetter(str) {
    for (let i = 0; i < str.length; i++) {
        let x = str.charCodeAt(i);
        let y = str.charCodeAt(i + 1);
        if (x + 1 != y) {
            return String.fromCharCode(x + 1);
        }
    }
// I tried to implement a catch-all clause that will run if the function has not returned anything after the 'for' loop has finished running.
    return undefined;
}

This

and this

can’t go together

Thanks! It now works.

function fearNotLetter(str) {
// fixed the upper bound to str.length - 1.
    for (let i = 0; i < str.length - 1; i++) {
        let x = str.charCodeAt(i);
        let y = str.charCodeAt(i + 1);
        if (x + 1 != y) {
            return String.fromCharCode(x + 1);
        }
    }
    return undefined;
}
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.