Solved

JavaScript problem

Posted on 2016-09-19
9
31 Views
Last Modified: 2016-10-09
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!";
0
Comment
Question by:OMIED BADR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 41805655
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
 

Author Comment

by:OMIED BADR
ID: 41805701
no its part of a bootcamp and the instructor are too busy to help that is why I asked the problem.
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 250 total points (awarded by participants)
ID: 41805709
Try this:

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

var greet = greet(hour);

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:OMIED BADR
ID: 41805734
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
 

Author Comment

by:OMIED BADR
ID: 41805757
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 41806065
No probs... don't forget to close the question :-)
0
 
LVL 57

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points (awarded by participants)
ID: 41806109
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
 

Author Comment

by:OMIED BADR
ID: 41836290
yes problem was solved sorry saw your comment late close it thank you so much.
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question