The challenge was to find the missing letter in an array in alphabetical order.
Find the missing letter in the passed letter range and return it.
If all letters are present in the range, return undefined.
My solution was this:
function fearNotLetter(str) {
var letters = str.split('');
var codes = [];
var missing = 0;
var start = 0 , next = 0 , c = 0;
for(var i= 0; i < letters.length; i++){
var charcode = letters[i].toString();
codes.push(charcode.charCodeAt());
}
for(var j = 1; j < codes.length; j++){
start = codes[j] -1;
if(codes.indexOf(start) === -1){
missing += start;
}
}
if(missing === 0){
return undefined
}else {
return String.fromCharCode(missing);
}
}
I am wondering whether I have followed some bad practices or used dry code which might cause problems in a later use. Thanks in advance.
fromCharCode
at the end, you assume there is only one missing letter. If that's the case, you can justbreak
out of the secondfor
instead of addingstart
tomissing
and proceeding to process the rest ofcodes
. \$\endgroup\$