Intermediate Algorithm Scripting - Missing letters

As (unless I’ve missed it) charAt() hasn’t yet been covered in this course, I used more basic loops previously learnt. Can anyone tell me if the code’s ok?

I’m aware I’ve created global i and j Variables. Would this lead to problems later on in more complex code when I try and re use them in future loops? should I use more specific variables?

Also should I be looking elsewhere as I learn, or checking the hints before attempting the challenges? . As stated unless I missed them I don’t remember the lessons covering charAt() but the hints (that I always check after completing) state I should have used it?

Appreciate this course cant cover everything, and google is an important part of coding, but I’m always wary of using google for the ‘challenges’ as its easy to find a short(but complicated) solutions that I don’t really understand.

Thanks in advance.

appreciate any feedback.

function fearNotLetter(str) {

// set up an alphabet array and a start point

  const alphabet =["abcdefghijklmnopqrstuvwxyz"];
  let start = 0; 

// create a loop to find a start point

  for (let i = 0; i < alphabet[0].length; i++) {
    if (alphabet[0][i] === str.charAt(0)) {
      start = i;
    }
  } 

// create a while loop to loop through and find the first time the str doesnt match the alphabet from the start point

  let i = start;
  let j = 0;
  while (j < str.length){
    if (str[j] !== alphabet[0][i]) {
      return alphabet[0][i];
    }
  i++
  j++
  }
  return undefined;
}

console.log(fearNotLetter("bcdf"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Missing letters

Link to the challenge:

But you havent’t? All of your variables are inside of your function.

Why a string in an array?

You don’t need this. You can use bracket notation to index into a string.

The intention is wonky, but this basic idea is fine. Its the heart of what fancier built in methods will do.

I’d only look at other answers after you have yours.

Writing simple, elegant code is really hard. I would start with code that works. Your code will get simpler with practice.

Funnily enough, while you didn’t need to use charAt(), charCodeAt() could be the making of a much simpler function.

1 Like

Thanks so much for the reply, Funnily enough the charCodeAt() was the reason I posted, as after I’d completed it and checked the other solutions this was what they’d all used and yet it had not been covered in this course yet.

ahh well keep on keepin on I guess.

Thanks again

Thanks so much for the reply.

Just to clarify (for myself as much as anything.)

  1. Thanks yes contained within a function so not global. Easy to forget as the function is setup for you.
  2. The array was left over from a previouse attempt to push the alphabet into the array using a loop. Soon gave up with that idea but obviously left the array and just added the string inside it.
  3. charAt() I found after completing the challenge and checking the hints and solutions. They were all completed using charAt() and charCodeAt(). As I’d not come across these before I tried charAt() instead of str[0] just to see if would produce the same result. charCodeAt() I had to read further about elsewhere to try and understand,
  4. keep on keepin on I guess.

Thanks again. Its the community that makes this all seem almost possible to learn for people like me with no experience.

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