Solved

JavaScript problem

Posted on 2016-09-19
9
26 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 51

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now