So i have a exercise that i can´t solver about arrays i have done some part of it bt i am not able to finish.
they are asking for: Using this characters array, print to the console every character which name begins with ‘M’. Don’t use any kind of loop, yet. We’ll get to that in a second.
If there asking you not to use a loop i assume that they might want you to just hard code this and and have 4 separate console.logs and index into the names, but that’s just me guessing. The only other guess i would have is they don’t want you to use a type of for loop, but a method? even though those would also technically be a loop as well.
I’m not sure you can really get the names without any loop other then hard coding the indexes, unless someone here knows something i don’t.
I would say using traditional for…loop to iterate over an array is not a good choice. Instead you should consider array helper methods, such as : yourarrayname.forEach(), yourarrayname.find(). Each of the helper methods expects a callback function as an argument. For example:
const myArray = [["John", "Bob"], ["Alice", "Michael"]];
myArray.forEach((elem) => {
let name = elem.find((name) => name.startsWith('M'));
if (name) {
console.log(name); // Michael
console.log(...name.split("")); // M i c h a e l
}
});
I don’t know if it’s an issue with copy pasting, but here you have lots of ‘ and ’ being used, but those are not valid quote characters, you need to use the straight one '
@ILM is saying that there are different types of quote marks. When coding, the expected version are the straight ones. Yours are slanted, like one might see when using a Word Processor like Microsoft Word. You can easily test this by copying your code and paste it into NotePad. Then copy and paste it back into your code.
I assume the array was given to you and is in the starting code?
Seems more like a simple test to see if you understand how to access nested array elements (directly). But we don’t really have enough information to be sure of that.