0

This may sound like a dumb question, but is it possible to have a 0 at the start of a number check, and have it work?

This is what I have:

function checkCosts() {
    var date = document.getElementsByName("date")[0].value;
    var roomtype = document.getElementsByName("roomtype")[0].value;
    var night = document.getElementsByName("night")[0].value;
    var month = date.substring(0, 2);
    var year = date.substring(8, 10);
    var day = date.substring(4, 6);
    var time = month.concat(year);
    var fulldate = day.concat(time);
    if (time >= 0415 && <= 0915) {
        if (roomtype == "Classic") {
            if (night == "3") {
                document.getElementById("cost").innerHTML = "1,480";
            }
        }
    }
}

However, when I run it in jslint.com I get the following errors:

Unexpected '4' after '0'.
if(time >= 0415 && <= 0915){ 
line 9 column 28Unexpected trailing space.
if(time >= 0415 && <= 0915){ 

What's there is just one of a few different statements, all the variables will be used.

It would be possible to convert the strings into ints, but I don't know how to do this/if it will work.

Laxmikant Dange
  • 7,606
  • 6
  • 40
  • 65
jordsta95
  • 119
  • 1
  • 9
  • There was similar question: http://stackoverflow.com/questions/12888075/javascript-alert-number-starting-with-0 – IonDen Sep 14 '15 at 11:51
  • 1
    why do you want to compare with number starting with 0, There will not be any change if there is 0 or not – Laxmikant Dange Sep 14 '15 at 11:52
  • 3
    `if(time >= 0415 && <= 0915)` this isn't valid JavaScript. – sigod Sep 14 '15 at 11:52
  • 1
    `parseInt()` is the function to convert a string to int. – Uma Kanth Sep 14 '15 at 11:53
  • Think this is just a data type thing. You're creating a string as the value of 'time' using month.concat(year), then comparing it to an integer with a leading 0 which is what jslint isn't liking (as an integer, that would just be 415 and 915 for example). Why not compare the two separate parts of the date as integers (e.g. (parseInt(month) >= 4 && parseInt(year) >= 15) && (parseInt(month) <= 9 && parseInt(year) <= 15)) ? – Chris Disley Sep 14 '15 at 11:54
  • I tried that, it didn't work. But after looking in the console, I found something strange. Wordpress (which is where this is being done) turns && into &&... which seemed to be the issue, as when I nested the if (i.e. if greater than, if less than) then it worked just fine... Sometimes I hate WP >_> Thanks for your help :D – jordsta95 Sep 14 '15 at 12:15

4 Answers4

0

A leading 0 is not the way to use ints. You should take a look here. Otherwise just use the alpabetical order using a string comparison.

Matthijs van Hest
  • 769
  • 1
  • 6
  • 12
0

There is just no need to add zeros in front of the number. If the first number identifies the level of the room just add it, where you need it.

prizm1
  • 363
  • 1
  • 11
0

Instead of concatenating month with year, Concatenate year with month, And then check. You need to do some modification in your logic.

function checkCosts() {
    var date = document.getElementsByName("date")[0].value;
    var roomtype = document.getElementsByName("roomtype")[0].value;
    var night = document.getElementsByName("night")[0].value;
    var month = date.substring(0, 2);
    var year = date.substring(8, 10);
    var day = date.substring(4, 6);
    var time = year.concat(month);//Concat year with month.
    var fulldate = day.concat(time);
    if (time >= 1504 && <= 1509) { //Year should be first
        if (roomtype == "Classic") {
            if (night == "3") {
                document.getElementById("cost").innerHTML = "1,480";
            }
        }
    }
}  

If you are trying to convert date to number and want to compare, use year first then month.

Laxmikant Dange
  • 7,606
  • 6
  • 40
  • 65
-1

from this:

var time = month.concat(year);

I deduce "time" is a string.

then you should put quotes on 0415

strings are compared using alphabetical order (I hope you don't need globalization in this comparison) :

"04".concat("16") >= "0415"
 -> true
"04".concat("14") >= "0415"
 -> false
Regis Portalez
  • 4,675
  • 1
  • 29
  • 41