[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JavaScript problem

Posted on 2016-09-19
9
Medium Priority
?
34 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
8 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 60

Assisted Solution

by:Julian Hansen
Julian Hansen earned 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

873 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