user2856563
user2856563

Reputation: 29

If/else statement comparing numbers

I am writing jquery and i need to change colors if great than 730 days.. between 730 and 183 days ... ect. well its not working on my page if there something wrong with my if else statement?Here is all of the jquery but I just need help with the if/else part.I am very new to this and any help is great.

$(document).ready(function () {
'use strict';

var monthNames = [ "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December" ],

    dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],

    newDate = new Date();

$('#safetyRecord').hide();

$('#today').text(dayNames[newDate.getDay()] + "," + ' ' + monthNames[newDate.getMonth()] + ' ' + newDate.getDate() + "," + ' ' + newDate.getFullYear());

$('#checkRecord').click(function () {

    var dateOfLastAccident = new Date($('#dateOfLastAccident').val());

    var today = new Date(),

        daysSinceLastAccident = Math.floor((today.getTime() - dateOfLastAccident.getTime()) / (25 * 60 * 60 * 1000));

    $('#daysSinceLastAccident').text(daysSinceLastAccident);

    $('#safetyRecord').show();

if (daysSinceLastAccident >= parseInt(730)){
    {
    $('#safetyRecord').addClass('great');
    }

else if (daysSinceLastAccident >=730<=183)
{
    $('#safetyRecord').addClass('good');
}

else if (daysSinceLastAccident >=183<=60) 
{
    $('#safetyRecord').addClass('.marginal');
}
else if (daysSinceLastAccident >=60<= 14) 
{
    $('#safetyRecord').addClass('.poor');
}
else (daysSinceLastAccident < 14)
{
    $('#safetyRecord').addClass('.disaster');
}


});

});

Upvotes: 1

Views: 107

Answers (3)

Bill the Lizard
Bill the Lizard

Reputation: 405765

This doesn't really make any sense:

else if (daysSinceLastAccident >=730<=183

If your first condition is false, you can just fall through to the next one.

if (daysSinceLastAccident >= 730) {
{
    $('#safetyRecord').addClass('great');
}
else if (daysSinceLastAccident >= 183) 
{
    $('#safetyRecord').addClass('good');
}
// and so on...

You don't need any kind of && logic because your ranges don't overlap.

Upvotes: 1

Jay Harris
Jay Harris

Reputation: 4271

This is not possible daysSinceLastAccident >=183<=60 and you have alot of coding errors with your parentheses and curly brackets.. BTW i hope you know how to use 'use strict'

Upvotes: 0

user1864610
user1864610

Reputation:

Your if conditions are broken. You have:

else if (daysSinceLastAccident >=730<=183 

when you should have something like

else if ((daysSinceLastAccident < 730) && (daysSinceLastAccident>=183)) {

You'll need to change them all, and watch the direction of your comparisons too. You won't find much that is > 730 and also <= 183

Upvotes: 1

Related Questions