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
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.
Thanks yes contained within a function so not global. Easy to forget as the function is setup for you.
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.
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,
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.