Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Can you use the < and > to improve this?

Hi guys, this is a program from a quiz. I wanted to improve it using the stuff I just learned to make if more intuitive. I was wondering if I could use the < and > sign so that if the day the user typed was greater than a certain day it would give a different response. I was thinking in terms of where Sunday was greater than Monday and Monday was greater than Tuesday and Tuesday was less than Monday. I don't know if that's correct to think that way. I was just going off what Guil Hernandez said where you can use the < and > than on words too. Where apple is less than bear because the first letter in apple, a comes before the first letter in bear, b so a is less than b. don't know if this works for days of the week. What I was thinking is a prompt dialog asks the user "what day is it?" and say the user typed "Friday" then it would respond "the weekend is almost here" but I wanted it to do like if they typed "Thursday for example it would say something like "two more days to go before the weekend gets here." So I was thinking the code would give that response if answer was less than Friday etc. Don't know if this is making any sense at all. anyway I changed the program a bit. here is the code.

var day = 'Sunday';
var day = 'Monday';
var day = 'Tuesday';
var day = 'Wednesday';
var day = 'Thursday';
var day = 'Friday';
var day = 'Saturday';
var day = prompt('What day is it?');
if ( day.toUpperCase() === 'FRIDAY' ) {
alert("The weekend is almost here.");
} else if ( day.toUpperCase() === 'THURSDAY' ) {
  alert("Two more day to go to the weekend.");
} else if ( day.toUpperCase() === 'WEDNESDAY' ) {
  alert("Man when will the weekend get here?");
}else if ( day.toUpperCase() === 'MONDAY') {
alert("Here comes another week.");
} else {
alert("Man I miss Friday.");
}

Thanks

2 Answers

Hey Samuel,

What I meant the other day is this, take a look! And ofcourse, the teacher will talk about arrays, but this could give you an idea on how it works

    var days = ["Sunday", "Monday", "Tuesday", "Wednessday", "Thursday", "Friday", "Saturday"];
    var answer = prompt("What day is it?");

    if (answer == days[6])
    {
      alert("The weekend is almost here.");
    }
    else if (answer == days[5])
    {
      alert("Two more day to go to the weekend.");
    }
    else if (answer == days[4])
    {
      alert("Man when will the weekend get here?");
    }
    else if (answer == days[1])
    {
      alert("Here comes another week.");
    }
    else {
      alert("Here comes nothin");
    }

Wow, that looks awesome, can't wait to learn about it. I noticed the square bracket is being used. I'm going to copy this code. Thanks Luc de Brouwer

Hi Samuel,

In order to accomplish that you'll have to find a way to make an index based list or array of your values, since days can't be compared in string values by using the < or > operator.

var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",  "Saturday", "Sunday"];

// Do some if else to compare the array index

I think you can do this by just counting the length of an array or, array value or just compare it to it's index.

Thanks Luc de Brouwer for the reply. Unfortunately I only did the JavaScript basics course so we didn't touch on arrays. I'm guessing that's the more advance course. I am doing CSS layout techniques now. Thanks though for the reply.