JS Using filter function

Hello!
I am trying to master the filter function and it doesn’t want to cooperate.
Maybe someone can tell me what is wrong here. As it sits now, I only get .
Here is the code:

const citiesIhavevisited = ["San Francisco, CA", "Chicago, Ill", "NYC, NY", "Boston, MA", "Paducah, KY", "Miami Beach, FLA", "Bangkok, Thailand", "Hanoi, Vietnam", "Saigon, Vietnam"];

const visted = citiesIhavevisited;

visited.forEach(vist => {
    console.log(vist);
});

//note: So far the code works!

let filtervst = visted.filter{(country => country.Vietnam);
console.log(filtervst});

No OUTPUT!!! Please Comment. Code works until the last filter.

const citiesIhavevisited =["SF, CA", "Chicago, Ill", "NYC, NY", "Boston, MA", "Paducah, KY", "Miami Beach, FLA", "Bangkok, Thailand", "Hanoi, Vietnam", "Saigon, Vietnam"];

const visted = citiesIhavevisited; 

visited.forEach(vist => {
     console.log(vist);
});

//NOTE: WORKS FINE UNTIL THIS NEXT PART. NOT SURE WHAT TO DO TO FILTER OUT THE COUNTRY IN TERMS OF SYNTAX. 



let filtervst = visited.filter(country => country.Vietnam);
console.log(filtervst);

(https://www.codecademy.com/courses/introduction-to-javascript/lessons/javascript-iterators/exercises/iterators-review)

Hello, @bangkok221.

When I first tried to run your code, I received this error:

visited.forEach(vist => {
^

ReferenceError: visited is not defined

Not sure why you need visited, but after correcting the spelling, the output was:

SF, CA
Chicago, Ill
NYC, NY
Boston, MA
Paducah, KY
Miami Beach, FLA
Bangkok, Thailand
Hanoi, Vietnam
Saigon, Vietnam
[]

The empty array is filtervst. In your attempt to filter elements that include Vietnam, you are using the variable, country as the placeholder for each element of your visited array. Those elements are string literals. They do not have a Vietnam property. The dot notation you are using, country.Vietnam is undefined, so none of the elements match, and you end up with filtervst assigned to the empty array. You may want to look at the String.prototype.includes() method.

Also, I edited your posts to remove the skeleton text provided for your first several posts, and to format your code properly, so it can be more easily viewed and copied/pasted for testing. Please do format your code in future posts. See How to ask good questions (and get good answers), and scroll down to Format Your Code.

1 Like

Wow! Thanks for helping me to think!

1 Like

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