This is my wonky solution. I didn’t use the recommended methods as I didn’t understand them. I’m sure this is probably a roundabout way to solve it but it works.
function fearNotLetter(str) {
var letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var letterRange = letters.slice(letters.indexOf(str[0]),letters.indexOf(str.slice(-1)));
var missingLetter = "";
for (var i = 0; i < str.length; i++){
if(str[i] !== letterRange[i]){
missingLetter = letterRange[i];
return missingLetter;
}
else if(str.slice(-1) === str[i]) {
return undefined;
}
}
return letterRange;
}
fearNotLetter("abcdefghjklmno");
function fearNotLetter(str) {
var ascii = [];
var sum = 0;
var total = 0;
var i = 0;
for(i = 0; i < str.length; i++) {
ascii[i] = str.charCodeAt(i);
sum = sum + ascii[i];
}
ascii = ascii.sort(function(a, b){
return a - b;
});
var length = ascii.length - 1;
for(var j = ascii[0]; j <= ascii[length]; j++) {
total = total + j;
}
total = total - sum;
if(total === 0) {
return;
}
return String.fromCharCode(total);
}
function fearNotLetter(str) {
var start = str.charCodeAt(0); //strcodes loop start
var end = str.charCodeAt(str.length - 1); // strcodes loop end
var strCodes = []; // there i will push every charCode from the start to the end of the string
for(var i = start; i < end; i++){
strCodes.push(i);
}
for(var b = 0; b < strCodes.length; b++){
if(String.fromCharCode(str.charCodeAt(b)) != String.fromCharCode(strCodes[b])){
return String.fromCharCode(strCodes[b]);
}
}
return undefined;
}
//97-122
fearNotLetter("bcd");
In my solution I use a loop to test first if the following char (i+1) would not be larger than the length of the str itself, because otherwise it means there was no gap and everything is in order and the outer return statement (outside of the loop) returns undefined. Within the loop I test if the following char (i+1) minus the current char would be anything else but 1, because a larger gap than 1 would mean that its not in line and something is missing. In this case it returns from the current char position the next in line.
function fearNotLetter(str) {
for (var i = 0; i < str.length; i++) {
if(i+1 < str.length && str.charCodeAt(i+1) - str.charCodeAt(i) !== 1) {
return String.fromCharCode(str.charCodeAt(i)+1);
}
}
return undefined;
}
fearNotLetter("abce");
I feel like my functions are always so convoluted compared to others but I can’t seem to get out of thinking this way, it works but after looking at others I think I’ll take another crack at it
‘’'
function fearNotLetter(str) {
var alphabet = “abcdefghijklmnopqrstuvwxyz”;
var missingLetter;
if (alphabet.indexOf(str) === -1) {
missingLetter = undefined;
}
var firstLet = alphabet.indexOf(str[0]);
var alphaStr = alphabet.substr(firstLet, str.length).split(’’);
for (var i = 0 ; i < str.length ; i ++) {
if (str[i] !== alphaStr[i]) {
missingLetter = alphaStr[i]; break;
}
doesnt actually check for a range of letters… More tests should be implemented before the challenge can be completed to check for this kind of thing? i.e. fearNotLetter(“fgjkl”)
function fearNotLetter(str) {
var last;
for (var i = 0; i < str.length; i++) {
if (last && (str.charCodeAt(i) - last > 1)) {
return String.fromCharCode(str.charCodeAt(i) - 1);
}
last = str.charCodeAt(i);
}
}
// Test
fearNotLetter("abce"); // should return "d".
Said this works. Not sure if I believe it. It only seems to work if there is only one missing letter. Any thoughts?
function fearNotLetter(str) {
var a;
var num = [];
var missing = [];
var answer;
for (var i = 0; i<str.length; i++) {
num.push(str[i].charCodeAt());
}
var len = num[num.length-1];
for (var j = num[0]; j<len; j++) {
if (num.indexOf(j) == -1){
missing.push(j);
answer = String.fromCharCode(missing);
}
}
in addition to all of the solutions for this challenge. Here is my work:
[code]function fearNotLetter(str) {
var int = str.charCodeAt(0);
var x = 0;
while ( x < str.length) {
x++;
if (str.indexOf(String.fromCharCode(int++)) === -1) return String.fromCharCode(–int);
} return undefined;
}
Here’s my hair-brained and convoluted solution to this challenge:
function fearNotLetter(str) {
var encoded = [],
missing = [];
for(var i = 0; i < str.length; i++){
encoded.push(str.split("")[i].charCodeAt());
}
for(var j = 0; j < encoded.length; j++) {
var counter = encoded[j] - encoded[j-1],
ref = 1;
if(counter != 1)
while(ref < counter) {
missing.push(encoded[j-1]+ref);
ref++;
}
}
//-------------------------------number of missing elements----------------------------------
function consecNumbers(encoded) {
return encoded[encoded.length - 1] - encoded[0] - encoded.length + 1;
} // function that returns the number of elements missing in an array of consecutive numbers
//-------------------------------------------------------------------------------------------
if(consecNumbers(encoded) === 0){
return undefined;
}
var found = String.fromCharCode(missing[0]);
return found;
}
fearNotLetter("abce");
In my way to understanding the problem, I’ve introduced a new consecNumbers function that helped me to find out if from a given array of consecutive numbers, an element or more are missing. So because that was helpful for me and proved useful I incorporated it in the solution.
Hope I’ve added a new perspective for those interested.
Tried this exercise with codePointAt and fromCharCode methods
function fearNotLetter(str) {
var newStr = []; // String Array to push Unicode of the given string
var missingLetter;
for (var i=0; i<str.length; i++) {
newStr.push(str.codePointAt(i));
// Checks the different between consequent indices,returns missing letter or
undefined accordingly
for (var j=0; j<newStr.length; j++) {
if (newStr[j+1] - newStr[j] > 1) {
missingLetter = newStr[j]+1;
return String.fromCharCode(missingLetter);
} else {
str.codePointAt(missingLetter);
}
}
}
}
fearNotLetter("abd");