Please explain what's wrong w this syntax

function testGreaterThan(val) {
  if (val) { console.log(val < 100); // Change this line
    return "Over 100";
  }
  
  if (val) { console.log(val < 10); // Change this line
    return "Over 10";
  }

  return "10 or Under";
}

// Change this value to test
testGreaterThan(10);

It always returns “Over 100” even when I put in lower numbers unless I put in 0 then it returns “Under 10”
https://www.freecodecamp.org/challenges/comparison-with-the-greater-than-operator

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums


Both of your `if` statements are just checking if `val` is truthy. Your comparison is only inside a `console.log` statement.

Please explain what result you want from this code,

The assignment calls for different inputs to check the code…

testGreaterThan(0) should return "10 or Under"
testGreaterThan(10) should return "10 or Under"
testGreaterThan(11) should return "Over 10"
testGreaterThan(99) should return "Over 10"
testGreaterThan(100) should return "Over 10"
testGreaterThan(101) should return "Over 100"
testGreaterThan(150) should return “Over 100”

@shadrachtuck You need to understand that right now the code that you have posted does not check for the condition in the if statement. The if statement only contains (val) that means it checks that if there is a value in val the condition is going to be true and it returns above 100 and leaves the function. You need to add the conditions to be checked in the if statement and not in the console.log().

1 Like

What it is currently doing is:

testGreaterThan(50)

It hits the first if statement. That checks if something is true or not. 50 is not 0, so it’s true.
In that block, console.log literally logs something to the console, it doesn’t do anything else, it’s just there so you can inspect what values are at a given point in the program. And it logs whether 50 < 100, so that’s true.
Then you return ""Over 100".

So regardless of what number you put in, that block of code will always run. Except if you do:

testGreaterThan(0).

0 is a falsey value. if statements run their block of code if the condition is true. 0 evaluates to false, so both of the blocks of code in the ifs are skipped, and it ends up returning "10 or Under".


The code in the console log statements is code that evaluates to true of false depending on the value: those two expressions are the conditions you want

1 Like