You will create a program that will find the missing letter from a string and return it. If there is no missing letter, the program should return undefined. There is currently no test case for the string missing more than one letter, but if there was one, recursion would be used. Also, the letters are always provided in order so there is no need to sort them.
You will need to convert from character to ASCII code using the two methods provided in the description.
Hint 2
You will have to check for the difference in ASCII code as they are in order. Using a chart would be very helpful.
Hint 3
You will need to figure out where the missing letter is, along with handling the case that there is not missing letter as it needs an specific return value.
Solutions
Solution 1 (Click to Show/Hide)
function fearNotLetter(str) {
for (let i = 0; i < str.length; i++) {
/* code of current character */
const charCode = str.charCodeAt(i);
/* if code of current character is not equal to first character + no of iteration
then a letter was skipped */
if (charCode !== str.charCodeAt(0) + i) {
/* if current character skipped past a character find previous character and return */
return String.fromCharCode(charCode - 1);
}
}
return undefined;
}
// test here
fearNotLetter("abce");
Code Explanation
This solutions makes use of a for loop.
Code of encountered character is stored in code.
It is checked if code of current character is the expected one (no characters are skipped) by using the logic - code of current character = code of first character + number of iterations.
If a character is missing, the missing character is found and the final string is returned.
undefined is returned if there is no missing character in the string.
Iâve got the feeling that more often than not, the basic solution is more straightforward and much more readable. Sometimes, it feels like the more advanced solutions are akin to killing a fly using a bazooka! I used to feel bad having my solution look more basic than advanced, but I think that simplicity and readability on projects at scale can be much more beneficial than using higher-end functions and techniques.
Whatâs your take on this?
Thank you @P1xt for the thoughtful and useful answer (as Iâve already come of expect of you). Youâre a very generous contributor to this forum, thank you!
So what advice would you give to reach the right balance between simplicity, readability and efficiency, and in particular in regard to FCCâs challenges? I want to make sure I learn in a way that would be most useful for me in the future.
I also have a quick question concerning the benchmarks. Whatâs the fastest way to run them? Do you use benchmark.js with node.js installed on your own computer? Is there an easy way to run these benchmarks online?
Here is my super lazy algorithm, sans UTF-16 or regex. I think I need to go to bed as I completely missed the charCodeAt() hint LOL.
function fearNotLetter(str) {
var alphabet = 'abcdefghijklmnopqrstuvwxyz';
var len = str.length;
var start = alphabet.indexOf(str[0]);
for(var i = start; i < start + len; i++){
if(!str.includes(alphabet[i])){
return alphabet[i];
}
}
return undefined;
}
function fearNotLetter(str) {
for (var i = str.charCodeAt(0); i < str.charCodeAt(str.length - 1); i++) {
if (str.indexOf(String.fromCharCode(i)) == -1) {return String.fromCharCode(i);}
}
}
fearNotLetter("abce");
Is it really bad? And if it is - please explain why?
(Sorry for my bad English)
function fearNotLetter(str) {
var missing;
var first = str.charCodeAt(0);
var last = str.charCodeAt(str.length - 1);
for(var i = first; i < last; i++){
if(str.indexOf(String.fromCharCode(i)) < 0){
missing = String.fromCharCode(i);
}
}
return missing;
}
allChars[allChars.length-1] !== str[str.length-1] until the last chars are equal, I get it. I think however that the syntax of a While loop will easier to understand and more suitable.
Can u explain new RegExp('[^'+str+']','g'); ? From my understanding `[^str]â means searching out of the parentheses. Why is it necessary, there is no paretheses, so why do we need to mention it?
yeah, following the more advanced examples are great practice, but I hate to use code until after I understand it well enough to explain it in my commentsâŚ
Hi guys check out my implementation of the Missing letters algorithm.
function fearNotLetter(str) {
var startStrCharCode = str.charCodeAt(0);
var endStrCharCode = str.charCodeAt(str.length - 1);
var sumOfCompleteCharCode =
(((endStrCharCode + 1) - startStrCharCode) * (endStrCharCode + startStrCharCode))/2;
var actualCount = str.split('').reduce(function(acc, value){
acc += value.charCodeAt(0);
return acc;
}, 0);
var missingChar = sumOfCompleteCharCode - actualCount;
if(missingChar === 0){
return undefined;
}
return String.fromCharCode(missingChar);
}
It uses the sum of the first n integer algorithm SUM(N) = n(n+1)/2. if n does not start from 1 the but starts from r and ends at n formula becomes
SUM(of integers starting from r to n) = [(n + 1) - r]( n + r ) / 2
function fearNotLetter(str) {
var min = Math.min(str.charCodeAt(0), str.charCodeAt(str.length-1));
var max = Math.max(str.charCodeAt(0), str.charCodeAt(str.length-1));
var codeArr = [];
for(var i = min; i <= max; i++) {
codeArr.push(String.fromCharCode(i));
}
for (var j=0; j<codeArr.length; j++) {
if (str.indexOf(codeArr[j]) === -1) {
return codeArr[j];
}
}
}
function fearNotLetter(str) {
var ar = [];
var p = "";
for (var e in str){
ar.push(str.charCodeAt(e));
}
for (var x=0; x<ar.length-1; x++){
if (ar[x]+1 !== ar[x+1]){
p = ar[x]+1;
return String.fromCharCode(p);
}
}
return undefined;
}