Return statement placement of if statement loop

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.

Challenge: Missing letters

Link to the challenge:

I realize that the resul of the function is not a number but the letter.

this return statement will stop the loop at first iteration and just return that

one easy thing to do is to invert your logic, keep in consideration that if the function does not return anything, it returns undefined by default

what is your loop doing, when does it find the missing letter?

1 Like

The loop is a for loop now. With the return statement outside of the loop, the loop would be able to iterate.

  for (let i = start; i < coded.length;      i++)  {
    if (coded[i] !== i)  {
missingMember = coded[i];
return missingMember;
    }
  }
    return undefined;

It still has no return value.

you may want to double check if this makes sense - what’s the value of start? and coded.length?

I want to get 100 as the missing number. coded[i -1] does not return 100’s letter of d. Do you know what might be going on?

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];
return String.fromCharCode(missingMember);
    }
  }
    return 8;
}
fearNotLetter("abce");```

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");

your issue is here

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");

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.