Continuing the discussion from freeCodeCamp Algorithm Challenge Guide: Missing Letters :
// Adding this solution for the sake of avoiding using ‘for’ and ‘while’ loops. // See the explanation for reference as to why. It’s worth the effort. function fearNotLetter(str) { var compare = str.charCodeAt(0), missing; str.split(‘’).map(function(letter,index) { if (str.charCodeAt(index) == compare) { ++compare; } else { missing = String.fromCharCode(compare); } }); return missing; } // test here fearNotLetter(“abce”);
I have a question about this solution.
I cannot understand below line.
Variation “Compare” has two values? why there is comma(,) ?
var compare = str.charCodeAt(0), missing;
It’s a shorthand for this:
var compare = str.charCodeAt(0);
var missing;
1 Like
function fearNotLetter(str) {
let alpha = "abcdefghijklmnopqrstuvwxyz".split('');
let missing = '';
let i = alpha.indexOf(str[0]);
let j = 0;
for(; i < alpha.length; i++){
if(alpha[i] !== str[j]){
missing = alpha[i];
return missing;
}
j++;
}
return;
}
fearNotLetter("abce");
my solution, not really optimal but easy to understand.
Here is my solution for the “Missing Letters” problem:
function fearNotLetter(str) {
const letters = "abcdefghijklmnopqrstuvwxyz";
const searchStr = letters.substring(letters.indexOf(str[0]), letters.indexOf(str[str.length - 1]));
const result = searchStr.split('').filter(l => !str.includes(l));
return result.length ? result.join() : undefined;
}
fearNotLetter("abce");
You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
I have blurred the spoiler solutions and set this thread to locked. Thanks for your understanding.
1 Like