-4

I am having some trouble with this current lesson on control flow with JavaScript...

The question states:

In this exercise, you will be given a variable, it will be called value. You will also be given another variable, it will be called greaterThanFive. Using an 'if statement' check to see if the value is greater than 5. If it is, re-assign the boolean true.

code with stars next to it is the code I was given.

**let greaterThan5 = false;**
if (value > 5 ) {
    console.log("That is true");
}
**return greaterThanFive;**

I have tried a number of different ways on how to write the correct code but it obviously is not right. I tried assigning var value = 10;and then finishing the code as above but it says value has already been assigned. I have tried changing the boolean to let greaterThanFive = true; The hint only tells me that "should return boolean value equal to 10" and "expected true to be false"

Please help, I have been working on this simple code it may seem for a week and do not want to move on to the next lesson without fully understanding this question.

Thank You!

3 Answers3

2

You have two different variables; greaterThan5 and greaterThanFive.
You also have a return statement, which will only work inside of a function.

I believe what you're looking for is something like the following, which passes a value into the function, then checks whether the value is greater than five or not, setting the variable to true inside of the if conditional if it is. The function then returns the greaterThan5 variable's truthiness:

function greater(value) {
  let greaterThan5 = false;
  if (value > 5) {
    greaterThan5 = true;
  }
  return greaterThan5;
}

console.log(greater(10));
console.log(greater(3));

Which can be further simplified to a simple one-line return statement:

function greater(value) {
  return value > 5;
}

console.log(greater(10));
console.log(greater(3));
Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
2

So, the first clue in the code is the return statement. That means you are likely being asked to write a function that, given some value, checks to see if that value is greater than 5.

Let's define it using your existing code:

function isGreaterThan5(value) {
  let greaterThan5 = false;

  if (value > 5 ) {
    console.log("That is true");
  }

  return greaterThan5;
}

So right now, we're always going to return false. All you need to do is reassign the value of greaterThanFive if value > 5. So, you can simply do that in your if-statement:

function isGreaterThan5(value) {
  let greaterThan5 = false;

  if (value > 5 ) {
    greaterThan5 = true;
  }

  return greaterThan5;
}

You can now test your code by calling the function with various values:

isGreaterThan5(1); // returns false
isGreaterThan5(5); // returns false
isGreaterThan5(6); // returns true

And we're done!

I'm wondering if what confused you was the use of let. You might want to read more about var, let, and const.

Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
0

if (value > 5) {greaterThanFive = true;}

  • please don't throw code only without some explanation. Also use proper code block formatting where appropriate. – Léa Gris Jul 25 '19 at 03:06