Skip to content

Instantly share code, notes, and snippets.

@prophen
Last active June 21, 2024 23:13
Show Gist options
  • Save prophen/f277b6ae169f0ed9160d982e6b702c63 to your computer and use it in GitHub Desktop.
Save prophen/f277b6ae169f0ed9160d982e6b702c63 to your computer and use it in GitHub Desktop.

https://www.rithmschool.com/courses/javascript/introduction-to-javascript-boolean-exercises

{ Boolean Logic Exercises. }

Part I

Write down what the following statements will return. Try to figure this out before putting the commands in the chrome console.

  1. 2 == "2";

true

  1. 2 === 2;

true

  1. 10 % 3;

1

  1. 10 % 3 === 1;

true

  1. true && false;

false

  1. false || true;

true

  1. true || false;

true

Part II

Answer the following questions about this code block:

var isLearning = true;
if(isLearning){
    console.log("Keep it up!");
} else {
    console.log("Pretty sure you are learning....");
}
  1. What should the above code console.log?

"Keep it up!"

  1. Why do we not need to specify if(isLearning === true)? Why does if(isLearning) work on its own?

They mean the same thing. isLearning was defined as true above the conditional statement.

var firstVariable;
var secondVariable = "";
var thirdVariable = 1;
var secretMessage = "Shh!";

if(firstVariable){
    console.log("first");
} else if(firstVariable || secondVariable){
    console.log("second");
} else if(firstVariable || thirdVariable){
    console.log("third");
} else {
    console.log("fourth");
}
  1. What should the above code console.log? Why?

"third" because firstVariable and secondVariable are falsey values.

  1. What is the value of firstVariable when it is initialized?

undefined

  1. Is the value of firstVariable a "truthy" value? Why?

No, undefined is a falsey value.

  1. Is the value of secondVariable a "truthy" value? Why?

No, an empty string is a falsey value.

  1. Is the value of thirdVariable a "truthy" value? Why?

Yes, the number 1 is truthy.

Part III

  1. Research Math.random here and write an if statement that console.log's "Over 0.5" if Math.random returns a number greater than 0.5. Otherwise console.log "Under 0.5".
if (Math.random() > 0.5) {
  console.log("Over 0.5") 
} else {
  console.log("Under 0.5")
}
  1. What is a falsey value? List all the falsey values in JavaScript.

Falsey values evaluate to false in a conditional statement. undefined, null, NaN, 0, "", and false are the six falsey values in JavaScript.

@SerenityInAllThings
Copy link

Very good, thanks.

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