Javascript function for boolean returns

42,866

Solution 1

In JavaScript, the constants true and false are not numbers; they're a separate type.

Furthermore, you're comparing with === and that will explicitly prevent type conversion during the comparison.

Note that n / 4 is going to be true (non-zero) for all values of "n" except 0 (edit you probably meant to use %). And in general, any construction of the form:

if (expression) {
  return true;
}
else {
  return false;
}

can be replaced by:

return !!(expression);

or, alternatively,

return Boolean(expression);

Solution 2

Change your if statement to be consistent, since 1 != true. The triple equals will not allow it.

if (quarter(4) == true) {

Your function is also incorrect, I think you want something more like this, to return if it's divisible by 4:

var quarter = function(n) {
  if (n % 4 == 0){
    return true;
  } else {
    return false;
  }
};

This can be shortened to this:

var quarter = function(n) {
   return n % 4 == 0;
}

Solution 3

Using === means you're asking for a "strict equals"--not truthy or falsey. This means you can't check for a number, because a number is not strictly true. See this SO question for more details.

The calling code should either check for === true, or just skip the explicit value compare, and just be if (quarter(4)) { ....

Solution 4

Couldn't find an answer on here that worked, so I assume I just missed it. I also find that Googling solutions make things worse for me.

I tend to find that I overthink the solutions in Javascript, and this obviously leads to an inability to solve the solutions.

You also don't necessarily need the return true, but I added it anyways.

Just remember that logically ordered simplicity is key.


    var quarter = function(number) {
        var number = n / 4;
        var n = 48;
    if (quarter(number) % 3 === 0 ) {
        console.log("The statement is true");
        return true;
    } else {
        console.log("The statement is false");
        }
    }

I hope that works for you.

I'll use my lack of JavaScript skill and knowledge to explain this to beginners like myself, and you. And anybody who has an extreme knowledge of JavaScript will probably disagree, but it's also why most programming employers will take a Communication major over a programmer anyday:

  1. Define a function named quarter, meaning you are defining a function-- quarter -- but you must first define what quarter-- var.
    var quarter = function(number) 
  2. The function returns a value that equals to
    n / 4
    The function is being defined as
    quarter(number)
    The value is number.
    You then introduce a new var that defines the value with
    number = n / 4
  3. To return true and begin your if statement,

    function(number or n)
    is now quarter, the function you're defining.

  4. The basis of providing a correct equation relies on it returning true in the form of

    console.log("The statement is true!")
    You do this by then defining n with a number that will be divided 4, and then that number being able to be divided by 3 with no remainder.Since number
    has been defined as
    n / 4
    , With n being defined as 48 in this example, both:
    quarter(number)
    and
    quarter(n)
    will work for the if / else statement.
    To bring a remainder of 0 after dividing by 3, use
    % 3 === 0
    .

    1. So
      if (quarter(number) % === 3 {
      is now the defined function quarter.
      number
      is a parameter, and you assign the value of the parameter with variables,
      var number = n / 4
      and
      var n = 48
      .

On that note, as I'm typing this, this would, in theory, also work and being shorter:


    var quarter = function(number) { 
              if (quarter(48) % 3 === 0) {
                 console.log("This statement is true!");
                 return number = number / 4;
              } else { 
                  console.log("This statement is false.");
            };
         };

Share:
42,866
Matt Zelenak
Author by

Matt Zelenak

Updated on August 26, 2020

Comments

  • Matt Zelenak
    Matt Zelenak almost 4 years

    I'm having some issues getting a proper boolean return on section 4.1 of Codecademy's Javascript tutorial. Here is the code:

    // Define quarter here.
    var quarter = function(n) {
        if (n / 4 ){
            return true;
        } else {
            return false;
        }
    };
    
    if (quarter(4) === 1) {
      console.log("The statement is true.");
    } else {
      console.log("The statement is false.");
    }
    

    From what I can see, I am passing the newly defined quarter variable a function with a parameter of 'n' that I then divide by 4 to see if it returns 1 for true, or 0 (else) for false. I then am using the 'quarter' function in an if loop to check for equality of 1 of the number '4' passed as 'n'.

    I'm assuming this is some basic logic that I am just not used to using (as a front end developer looking to get into JavaScript programming) but I would definitely appreciate some thoughts and guidance.