Forum Moderators: open

Message Too Old, No Replies

a big 'else if' headache

else if statements not working correctly for a quick quote form

         

alij81

2:52 pm on Feb 24, 2009 (gmt 0)

10+ Year Member



Hi everyone,

I have been trawling the internet for answers to my 'else if' problem but can't find anything.

A friend of mine made a little quick quote field for my website last year but it isn't working properly and being all but completely html/ javascript illiterate I cannot see why. It did work for a little while which just adds to my confusion. If anyone would be kind enough to have a quick look at the javascript and see if there is a glaring error I would be very grateful.

The script is as follows:

<SCRIPT language="JavaScript">
<!--
function getQuote(form){

// These are the lists of prices in pounds for each of the seasons

var low=["10","12","15","18","22","24","28","28","28","35","40","43","46","49","49","49","52","55","58","61","64"]
var high=["10","20","25","25","30","35","39","39","45","50","55","60","65","69","69","80","85","90","95","95","100"]
var medium=["10","12","15","18","22","24","28","28","28","35","40","43","46","49","49","49","52","55","58","61","64"]

// This is the text that precedes the quote

var text="Your quote is: £"

var day = form.ddfrom.value;
if (day.length == 1) day = "0" + day
var month = form.mmfrom.value;
if (month.length == 1) month = "0" + month
var date = parseInt(month + day);
var days = parseInt(form.NoDays.value);


// the dates here are in the format MMDD. If you need to change the date ranges, eg 14th Jan becomes 3rd Feb, then 0114 becomes 0203


if (date < 0501)
{
alert(text + low[(days-1)]);
}
else if (date < 1031)
{
alert(text + high[(days-1)]);
}
else if (date < 1231)
{
alert(text + medium[(days-1)]);
{
else
{
alert("Please check the date range and try again. The date format is DD/MM");
}
}

//-->
</SCRIPT>

The script for the corresponding form, just in case that is the problem is as follows:

<fieldset>
<legend>
<span class="legendsub">Quick Quote </span></legend>
<form name="QQuote" id="chooseDateForm" action=""><center>
<p>Parking From:<br /><input type="Text" name="ddfrom" id="doody" maxlength="2" size="1">/<input type="Text" name="mmfrom" id="doody" maxlength="2" size="1"><br />
<b>Valid date format:</b> DD/MM<br>
Number of Days:<br /><select name="NoDays">

<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>

</select> <br /><br />
<input type="button" value="Get Quote" onclick="getQuote(this.form)">
</center>
</form>
</fieldset>

Any help that anyone coulkd give would be much appreciated.

Alex

alij81

2:58 pm on Feb 24, 2009 (gmt 0)

10+ Year Member



P.S I see that I have put the curly brackets/ braces the wrong way round in this part of the javascript. That is just a typing error. Apologies.

{
alert(text + medium[(days-1)]);
{

alij81

5:10 pm on Feb 24, 2009 (gmt 0)

10+ Year Member



This gets even more weird and wonderful as I try new things!

I have discovered that the javascript seems to have issues with number 8 and number 9!

For example if I put in the date as the 8th May or 5th August (5/8) it gives a low season price, even though they are both high season date ranges. Same if the date has a 9 anywhere in it. I have racked my brains and allmy limited know how and the mind boggles!

LifeinAsia

5:48 pm on Feb 24, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Add "alert(date)" before the "if (date < 0501)" part just to make sure that what you're testing is even the correct value.

Note that whenever you convert back and forth between and compare dates and strings, you're asking for trouble. It's almost always better to convert to actual dates and do true date comparisons.

alij81

6:18 pm on Feb 24, 2009 (gmt 0)

10+ Year Member



Ha! Thank you somuch for taking the time to look at that for me. I tried your suggestion and just as you must have summised it doesn't seem to be converting to intergers corectly at all. The numbers that are coming up seem to bear no relation to dates that have been put in. But it just seems so odd that it is working perfectly for every date except those dates with a number 8 or 9 in them. I will soldier on for a while then may just, as you suggest start from scratch.

Thank you so much for taking the time to help!

LifeinAsia

7:01 pm on Feb 24, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Another quick suggestion- don't use date as a variable name. It can be easy to confuse it with the Date() object.

alij81

11:21 am on Feb 25, 2009 (gmt 0)

10+ Year Member



Morning!

I have tried out your suggestion and changed 'date' to 'arr' but still just doesn't like 8's and 9's. If the date has an 8 or 9 anywhere parseint allocates a really low integer (hence going to low season automatically)... I'm using your previous suggestion to check for changes but still no joy.

I will keep you updated though and let you know if I make any breakthroughs.

Thank you again!

Fotiman

5:47 pm on Feb 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The problem with your code is that numbers that begin with zero (0nnn) are considered to be Octal numbers (note, numbers that begin with zero followed by the letter 'x' are hexidecimal numbers: 0xnnn). Therefore, if you have numbers like "0225", then when you call parseInt on that value, it will be converted to 149 (which is the decimal value of the octal number 225).

[edited by: Fotiman at 8:38 pm (utc) on Feb. 26, 2009]

alij81

5:50 pm on Feb 26, 2009 (gmt 0)

10+ Year Member



Oh dear! So parseInt simply won't cut the mustard for this. I thought it was such a clever bit of encoding when i saw it too... Ok well back to the drawing board for me. Thanks so much for your help I would have been staring at that until my head fell off without you... very kind of you both. Alex