I made an array of the string, and translated it into one of Unicode codes. A loop start value matches the lowest code value of the string and continues until the last code number. Every letter code could be compared to the loop variable with a stop and return of the first letter code that is not available named as the loop variable it could be. Strings with codes matching loop variables would return undefined.
The while loop marks the beginning of issues. Though this is a finite array I chose a for loop to practice using them. Nothing is produced. I am not sure where to place return undefined so that a loop can be made through the whole array.
function fearNotLetter(str) {
let array = str.split("");
let coded = array.map(letter => letter.charCodeAt());
let start = Math.min(...coded);
let i = start;
let missingMember = 0;
while (i < coded.length) {
console.log(coded[i]);
if (coded[i] === i) {
i++;
}
return undefined;
}
missingMember = coded[i];
return missingMember;
}
fearNotLetter("abce");
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.63.
Return statements are proving a challenge. I tried moving statements around and thinking about the loops. The post above passes tests with missing values and fails tests with complete strings, then this version does the reverse in failing tests of missing values and passing those with complete strings.
function fearNotLetter(str) {
let array = str.split("");
let coded = array.map(letter => letter.charCodeAt());
let start = Math.min(...coded);
let end = Math.max(...coded);
let missingMember = 0;
//console.log(end);
for (let i = 0; i < end; i++) {
//console.log(coded[i], start + i);
if (coded[i] !== start + i) {
missingMember = coded[i] - 1;
return String.fromCharCode(missingMember);
}
else {
return undefined;
}
}
}
fearNotLetter("abcde");
you take the max and min values of the array, but then try to loop through the array using end as max index
your end is around 60-100
the coded array has at max 26 elements
so once you reach coded[26] that is undefined, and your return statement triggers anyway, returning, sometimes weird stuff, also note that coded[i] can’t be the missing member as it is in the array
and if for some reason it manages to move after the loop, there is a return 8 for some reason
My bad. The code that I posted the last two times were miscopied. return 8 was the replacement for undefined which does not show up on my console. The last code with the loop you critiqued was the incorrect item on the clipboard.
The patient responses gave me the response I needed to to help me solve this myself.
function fearNotLetter(str) {
let array = str.split("");
let coded = array.map(letter => letter.charCodeAt());
let start = Math.min(...coded);
let end = Math.max(...coded);
let missingMember = 0;
//console.log(end);
for (let i = 0; i < coded.length; i++) {
//console.log(coded[i], start + i);
if (coded[i] !== start + i) {
missingMember = coded[i] - 1;
return String.fromCharCode(missingMember);
}
}
return "undefined";
}
fearNotLetter("abcde");
I wanted to make a while loop. It is tricky to figure out how to fit an if statement inside of one, so I changed the base case from the initial trial.
function fearNotLetter(str) {
let array = str.split("");
let coded = array.map(letter => letter.charCodeAt());
let start = Math.min(...coded);
let i = 0;
let missingMember = 0;
while (coded[i] == i + start) {
i++;
if (coded[i] !== i + start) {
missingMember = coded[i] - 1;
return String.fromCharCode(missingMember);
}
return undefined;
}
}
fearNotLetter("abcde");