Nonzero Numbers explainer

Aim of the puzzle: Introduce the concept of Truthy and Falsy
Walk through of solution: The test of an if statement is always either true or false. For example:

let x = 3
if (x === 3) {
    console.log('true!')
}

This code will print 'true!'because x is equal to 3.

However, what if we write it like this?

let x = 3
if (x) {
    console.log('true!')
}

This code will also print 'true!'. Weird!

This code works because every value in JavaScript is either “truthy” or “falsy”.

It seems confusing, but it’s a bit simpler than it sounds.

There are only 6 falsy values in JavaScript. Everything else is truthy. The 6 falsy values are:

  1. false: the boolean value false
  2. 0: the number 0
  3. '': An empty string
  4. NaN: which means “Not a Number”, and is typically caused by math errors
  5. undefined: a variable’s value before it has been assigned a value
  6. null: a blank value that can be assigned to a variable

In this puzzle, a classic for loop is set up with i initially set at 0 - 3 (this is a workaround for most mobile phone keyboards not allowing users to input negative numbers). It will loop as long as i < 4, and i will increment by 1 with each iteration.

Inside the for loop, an if statement tests i. If i has a truthy value, the console.log() will print i. If i has a falsy value, it won’t be printed.

When the code runs, every number from -3 to 3 except 0 will be printed to the console.
Sample code solution:

for (let i = 0 - 3; i < 4; i++) {
    if (i) {
        console.log(i);
    }
}

Javascript Concepts: Truthy and Falsy, If Statements, Booleans, console.log(), For Loops

1 Like

I need help!!! I dont know what i am doing wrong

Happy to help! Post a screenshot of your code and I can take a look.

1 Like

Im so lost

1 Like

Hey there, try putting an if statement inside the for loop like this:

for (let i = 0 - 3; i < 4; i++) {
  if (i) {
    console.log(i);
  }
}

If statements test if something is true or false. In this puzzle, we’re learning about truthiness and falsiness, and showing that 0 is one of the six falsy values.

Hope this helps! Let me know if you have any questions.
Ben

3 Likes


It says the console.log() statement is outside the if statement, but it’s not.

1 Like

There are 2 console.log() statements and the first one is outside the if statement’s {} brackets

Your code:

console.log(); <----- (outside)

if ( i ) { console.log( i ) };

The first console.log statement is not needed as it prints out 0 and thats not the aim of the puzzle.
The aim of the puzzle is to exclude 0 by running the if statement. As the first console.log statement is not running through the if statement’s test it prints 0.

So to complete the puzzle, remove the 1st console.log statement

Oh. Thank you so much.

So to my understanding I need to somehow get rid of this bracket that is circle to get this code to work? Because right now the console.log has its own brakets and it needs to be in the if statements

No, remove the console.log statement and put it inside the if statement

You know at one point I also was trying to do that, however the app will not let me remove the console.Log statement or edit any if the pre-written code

This is probably a stupid question, maybe I’ve forgotten a lesson, but please can you explain to me why zero is still printed if you put console.log outside the if statement?

Don’t worry, there are no stupid questions! It’s all part of learning.

If you put code inside of an if statement, that code will only run if the test of the if statement is true. If code is not in an if statement, it will always run. For example:

let x = 5;
if (x === 5) { // this test is true, because x is 5
    console.log('x is five'); // this line then runs
}
console.log('hello'); // this line will always run,
// because it is not in an if statement

Hope this clears things up! Let me know if you have any additional questions.
Ben