Array exercise-

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.

The code i already got is:

var filmCharacters = [
[‘Vito’, ‘Michael’, ‘Sonny’, ‘Freddo’],
[‘Mia’, ‘Vincent’, ‘Jules’, ‘Butch’],
[‘Bella’, ‘Edward’, ‘Jacob’, ‘Carlisle’],
[‘James’, ‘M’, ‘Moneypenny’, ‘Felix’]
];
for (var i = 0; i < filmCharacters.length; i++){
for(j = 0; j < filmCharacters [i].length; j++){
if(filmCharacters [i][j] .startsWith(“M”)){
console.log(filmCharacters[i] [j] )
}
}
}

and i get in response answser that says

code is incorrect. make sure not to miss any names and print each one seperatly

Michael

Mia

M

Moneypenny

( the names that start with M are all there but i can´t figure out how to separate them)

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
   }
});
1 Like

i have tried to apply what u said to my problem still with no success, might be some mistake of my part my where is the code i tried to apply

var filmCharacters = [
[‘Vito’, ‘Michael’, ‘Sonny’, ‘Freddo’],
[‘Mia’, ‘Vincent’, ‘Jules’, ‘Butch’],
[‘Bella’, ‘Edward’, ‘Jacob’, ‘Carlisle’],
[‘James’, ‘M’, ‘Moneypenny’, ‘Felix’]
];
filmCharacters.forEach ((elem) => {
let name = elem.find((name) => name.startsWith(“M”));
if (name){
console.log(name);
console.log(…name.split(“”));
}
});

Code is incorrect

Make sure not to miss any names and print each one separately.

Michael

M i c h a e l

Mia

M i a

M

M

can you use an example of what you are saying pls

btw i forgot to say that this is about nested arrays

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 '

i don´t understand what you mean

@ilenia 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.

You can’t use and for strings, you need to use '

As already said, it doesn’t sound like they want you to loop or do any conditional code. Just log out the 4 elements.

console.log(filmCharacters[someIndex][someIndex]);
console.log(filmCharacters[someIndex][someIndex]);
console.log(filmCharacters[someIndex][someIndex]);
console.log(filmCharacters[someIndex][someIndex]);

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.

yes the array came with the code at the start