Solved

JavaScript problem

Posted on 2016-09-19
9
29 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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 54

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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'…
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…

785 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