JavaScript problem

I'm having issues with the problem below. It's telling me to Use the conditional operator to rewrite the if...else statement in the editor.  on the very bottom is my answer and the beginning part is where we to take the problem and rewrite it.  please help.


function greet(hour) {
  if (hour < 12) {
    return "Good morning!";
  } else {
    return "Good afternoon!";
  }
}

var greet = greet(hour);
 greet() = (hour < 12) ? "Good morning!" : "Good afternoon!";
OMIED BADRAsked:
Who is Participating?
 
Terry WoodsConnect With a Mentor IT GuruCommented:
Try this:

function greet(hour) {
  return ( hour < 12 ? "Good morning!" : "Good afternoon!");
}

var greet = greet(hour);

Open in new window

0
 
Terry WoodsIT GuruCommented:
Is this a homework question?

Do you still need to keep your code wrapped in the greet function? If so, then you'd just remove the if else part of the function and replace it with your code, but you will need to edit your code to actually return the result.

You can return a result a bit like this:
return (a==b?"same":"different");

Open in new window

0
 
OMIED BADRAuthor Commented:
no its part of a bootcamp and the instructor are too busy to help that is why I asked the problem.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
OMIED BADRAuthor Commented:
when I use your solution below are the errors messages I get.
 
greet is defined and is a function
expected 'Good afternoon!' to be a function

 returns "Good morning!" if the hour is before noon
TypeError: greet is not a function at Context.<anonymous> (decoded.txt:24:12)

 returns "Good afternoon!" if hour is after noon
TypeError: greet is not a function at Context.<anonymous> (decoded.txt:28:12)
0
 
OMIED BADRAuthor Commented:
I took out var greet = greet(hour); out of the code you gave me and it worked it was correct thank you for all your help.
0
 
Terry WoodsIT GuruCommented:
No probs... don't forget to close the question :-)
0
 
Julian HansenConnect With a Mentor Commented:
Comment on the code above
function greet(hour) {
  return ( hour < 12 ? "Good morning!" : "Good afternoon!");
}

var greet = greet(hour);

Open in new window

You are assigning the return from the function greet to the variable greet - which is going to wipe out your function.
Consider this code
function greet(hour) {
  return ( hour < 12 ? "Good morning!" : "Good afternoon!");
}
var hour = 10;
var greet = greet(hour);
console.log(greet);
var x = greet(13);
console.log(x);

Open in new window

You will get an error on line 7 that greet is not a function

What you want to do is
function greet(hour) {
  return ( hour < 12 ? "Good morning!" : "Good afternoon!");
}
var hour = 10;
var message = greet(hour);

Open in new window

If you are learning JavaScript it is important that you understand the above concept. JavaScript does not have any typing so you can easily overwrite something you might want to keep.
Another point - the brackets around your return value are fine but unnecessary. This would work just as well
<script>
function greet(hour) {
  return  
          hour < 12 
            ? "Good morning!" 
            : "Good afternoon!";
}
var hour = 10;
var message = greet(hour);
console.log(message);
var message = greet(13);
console.log(message);
</script>

Open in new window

0
 
OMIED BADRAuthor Commented:
yes problem was solved sorry saw your comment late close it thank you so much.
0
All Courses

From novice to tech pro — start learning today.