Skip to content

Instantly share code, notes, and snippets.

@Nostromos
Created August 15, 2023 13:42
Show Gist options
  • Save Nostromos/ce2235822c5040dab6aef618a8553588 to your computer and use it in GitHub Desktop.
Save Nostromos/ce2235822c5040dab6aef618a8553588 to your computer and use it in GitHub Desktop.
TStark Planets
let ourPlanet = 'moon';
// For the below, when JS is 'reading' (aka parsing) this line, it reads it like this:
// if ourPlanet equals 'Moon' OR if there's a string 'moon' that exists.
// What you really want to tell it is- if our planet equals "Moon" OR if our planet equals "moon" THEN... do something.
if (ourPlanet === "Moon" || "moon") console.log('No, that\'s not a planet! Try again!'); // You need to wrap the 'then' statement in curly brackets. if (something) ->{do something}<- else if (something) {do something}.
else if (ourPlanet === "Sun" && /* ourPlanet === */ "sun") console.log('No, that\'s not a planet, but it\'s also called a star! Try again!');
else if (ourPlanet === "Venus" && "venus") console.log('Nope,you\'re wrong! Try again!');
else if (ourPlanet === "Mars" && "mars") console.log('No, that\'s not the correct answer! Guess again!');
else if (ourPlanet === "Pluto" && "pluto") console.log('Incorrect answer! It\'s a Dwarf Planet Guess again!');
else if (ourPlanet === "Uranus" && "uranus") console.log('Nope, this planet is the 7th one from the sum! Almost there!');
else if (ourPlanet === "Saturn" && "saturn") console.log('No! Guess again!');
else if (ourPlanet === "Earth" && "earth") console.log('Yes, that\'s not the correct answer! We are all on earth, living and breathing!');
else console.log('Research the planets!');
@Nostromos
Copy link
Author

Nostromos commented Aug 15, 2023

my question was why does this not work:
if (ourPlanet === "Moon" && "moon") console.log("No, that\'s not a planet! Try again!")

but this does work:
if (ourPlanet === "Moon" || "moon") console.log("No, that\'s not a planet! Try again!")

Technically, neither is correct because you didn't wrap the 'then' statement (you're writing if/then statements) in curly brackets.

Here's what I would have written:
if (ourPlanet === 'Moon' || ourPlanet === 'moon') {console.log('No, that\s not a planet! Try again!')};

You'll see that for the if the statement, I broke it out into two expressions. JS doesn't 'understand' what you're comparing when you do it your way. Technically your if statement was: 'if ourplanet equals 'Moon' or if there's a string that exists called 'moon''

You need to tell it that its looking at the ourPlanet variable to see if it equals "Moon" or if it equals "moon". Get it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment