I already know the answer (the lower most code snippet), but I’m struggling to understand why my logic fails using checkIsland(). Here’s my code:
const myCountry = {
country: "Mexico",
capital: "Mexico City",
language: "Spanish",
population: 120,
neighbours: ["United States", "Guatemala", "Belize"],
describe() {
console.log(
`${this.country} has ${this.population} million ${this.language}-speaking people, ${this.neighbours.length} neighouting countries and a capital city called ${this.capital}`
);
},
checkIsland() {
this.neighbours.length === 0 // if myCountry's neighbour array is empty
? this.isIsland = true // create a new property called isIsland(), and set it to true
: this.isIsland = false; //if not, create a new property called isIsland(), and set it to false
};
};
The correct code is this: checkIsland () { this.isIsland = this.neighbours.length === 0 ? true : false;
The only issue I see is that you have a semicolon that you shouldn’t on the second-to-last line. Once I remove that it works fine. see it working on repl.it here
Stylistically, it isn’t the way you want to do it, but it works.
A ternary’s job is to return a value which can be assigned to a variable. Assigning a value to a variable inside the ternary is not best practices and will often fail. So, the reassignment must be declared first, followed by the condition, then finally the value… IE let x = true === true ? “It’s true!” : “It’s false!”.