Forum Moderators: open

Message Too Old, No Replies

can i assign 2 values to a variable?

         

marcus76

3:25 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



Hello,

I would like to have the following javascript variable check for the $ and the £ (pound sign)

myDollarSign = myPrice.indexOf("$",0)

anyone know how this is done?

Thanks

Bernard Marx

11:41 pm on Aug 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




dollar_pound = myPrice.indexOf("$")>-1 ¦¦ myPrice.indexOf("£")>-1

marcus76

10:12 am on Aug 11, 2004 (gmt 0)

10+ Year Member



thanks for your reply Bernard, i changed it a little: is the following valid javascript?

myDollarSign = myPrice.indexOf("£",0) ¦¦ myPrice.indexOf("$",0)

It seems to only compute the first section - myPrice.indexOf("£",0) - that's it, ignoring the rest of the line.

any ideas / changes?

Thanks

Marcus

marcus76

10:23 am on Aug 11, 2004 (gmt 0)

10+ Year Member



forget that last post - may help if it paste the code in and explain what i'm attempting.

the following piece of code displays 2 field choosers, which contain pricing both in $ and £ - at the moment the price only changes when the price with the £ is selected. I would like both currencies supported so the price changes when either is selected. At the moment it is the first currency of this line: myDollarSign = myPrice.indexOf("£") ¦¦ myPrice.indexOf("$")

so that being the £

Code as below:

Many thanks.

<html>
<head>
<title> Small JavaScript Example </title>
<script type="text/javascript">
<!--
function FormatNumber(num)
{
if (isNaN(num))
{
num = "0";
}
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
{
cents = "0" + cents;
}
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
{
num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
}
return (((sign)?'':'-') + num + '.' + cents);
}

function showPrice()
{
var myTotalPrice = 0;
var showUP = 0;
var myMathProblem = "";

if (document.main!= null)
{
myItemPrice = parseFloat(document.main.nuPrice.value);

for (var i = 0; i < document.main.elements.length; i++)
{
var e = document.main.elements[i];
if ( e.type == 'select-one' )
{
Item = e.selectedIndex;
myPrice = e.options[Item].text;

myDollarSign = myPrice.indexOf("£") ¦¦ myPrice.indexOf("$")

if ( myDollarSign!= "-1" )
{
myParSign = myPrice.indexOf(")", myDollarSign);
myAttributeString = myPrice.substring(myDollarSign+1, myParSign);
myAttributeString = myAttributeString.replace(/,/,"");
myAttributePrice = parseFloat(myAttributeString);
myMathProblem = myPrice.charAt(myDollarSign - 1);

}
else
{
myAttributePrice = 0;
}
if (myMathProblem == "+")
{
myTotalPrice = myTotalPrice + myAttributePrice;
}
else
{
myTotalPrice = myTotalPrice - myAttributePrice;
}
}
}
}
else
{
myTotalPrice = 0;
myItemPrice = 0;
}

myTotalPrice = FormatNumber(myTotalPrice + myItemPrice);
document.getElementById("productNEWprice").innerHTML = "Total Price with Options $" + myTotalPrice;

document.write(myDollarSign)
}
//-->
</script>
</head>
<body onload='showPrice()'>
<form id="main" name="main" action="">
<input type="hidden" name="nuPrice" value="999.99">
<select name="brian[]" onChange="showPrice();">
<option> Select One </option>
<option value="1">10 (+$1315.00)</option>
<option value="2"> 2 (+£20.00) </option>
</select>
<select name="brian[]" onChange="showPrice();">
<option> Select One </option>
<option value="1"> 1 (+$15.00)</option>
<option value="2"> 2 (+$10.00) </option>
<option value="3"> 3 (-$21.00) </option>
</select>
<br><br><div id="productNEWprice"></div>

</form>
</body>
</html>

Bernard Marx

11:51 am on Aug 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry I don't have time to check over the whole code (maybe later this afternoon). I'll quickly point out this:

myDollarSign = myPrice.indexOf("£") ¦¦ myPrice.indexOf("$")
...later...
if ( myDollarSign!= "-1" )

This is doubly wrong (if that's possible). The term below will produce an integer (-1, if £ isn't present)

myPrice.indexOf("£")

"-1" is a string. So

myDollarSign!= "-1" 
will always be
true
.

This assignment statement..

myDollarSign = myPrice.indexOf("£") ¦¦ myPrice.indexOf("$") 

..will produce a boolean (true or false). But, at the moment, the values either side of the ¦¦ are the integers produced by those terms. All values, except for zero, are considered to be true. The entire RHS will only give false if both values are zero, which means that both £ and $ are the first character. This is impossible. We need the terms on either side of the ¦¦ to give correct boolean values. Use the code I gave before.

In fact, we might as well check, not just for the presence of the character, but make sure that it's at the first position. So the relevant bits are:

myDollarSign = myPrice.indexOf("£")==0 ¦¦ myPrice.indexOf("$")==0
...later...
if ( myDollarSign )

NB: Don't forget to change the ¦¦ to unbroken pipes.

Good luck.

marcus76

9:48 pm on Aug 11, 2004 (gmt 0)

10+ Year Member



hi Bernard, thanks for the time you've spent on this, i really appreciate it.

I changed the pipes and added your code, no errors but when i select the drop down boxes, no price is displayed. My Previous syntax of:

myDollarSign = myPrice.indexOf("£") ¦¦ myPrice.indexOf("$")

Displayed the price change when the following drop down was selected:

<option value="2"> 2 (+£20.00) </option>

Therefore i figured it was ignoring everything to the RHS of the ¦¦ as when i selected anything price option with $ there was no change in price.

Just to re-cap , i need to have the price calculate regardless of currency options selected.

i now have the following code with your line change in it:

<html>
<head>
<title> Small JavaScript Example </title>
<script type="text/javascript">
<!--
function FormatNumber(num)
{
if (isNaN(num))
{
num = "0";
}
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
{
cents = "0" + cents;
}
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
{
num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
}
return (((sign)?'':'-') + num + '.' + cents);
}

function showPrice()
{
var myTotalPrice = 0;
var showUP = 0;
var myMathProblem = "";

if (document.main!= null)
{
myItemPrice = parseFloat(document.main.nuPrice.value);

for (var i = 0; i < document.main.elements.length; i++)
{
var e = document.main.elements[i];
if ( e.type == 'select-one' )
{
Item = e.selectedIndex;
myPrice = e.options[Item].text;

myDollarSign = myPrice.indexOf("£")==0 ¦¦ myPrice.indexOf("$")==0

if ( myDollarSign!= "-1" )
{
myParSign = myPrice.indexOf(")", myDollarSign);
myAttributeString = myPrice.substring(myDollarSign+1, myParSign);
myAttributeString = myAttributeString.replace(/,/,"");
myAttributePrice = parseFloat(myAttributeString);
myMathProblem = myPrice.charAt(myDollarSign - 1);

}
else
{
myAttributePrice = 0;
}
if (myMathProblem == "+")
{
myTotalPrice = myTotalPrice + myAttributePrice;
}
else
{
myTotalPrice = myTotalPrice - myAttributePrice;
}
}
}
}
else
{
myTotalPrice = 0;
myItemPrice = 0;
}

myTotalPrice = FormatNumber(myTotalPrice + myItemPrice);
document.getElementById("productNEWprice").innerHTML = "Total Price with Options $" + myTotalPrice;

}
//-->
</script>
</head>
<body onload='showPrice()'>
<form id="main" name="main" action="">
<input type="hidden" name="nuPrice" value="999.99">
<select name="brian[]" onChange="showPrice();">
<option> Select One </option>
<option value="1">10 (+$1315.00)</option>
<option value="2"> 2 (+£20.00) </option>
</select>
<select name="brian[]" onChange="showPrice();">
<option> Select One </option>
<option value="1"> 1 (+$15.00)</option>
<option value="2"> 2 (+$10.00) </option>
<option value="3"> 3 (-$21.00) </option>
</select>
<br><br><div id="productNEWprice"></div>

</form>
</body>
</html>

Thanks once again for your help, almost there i think.

cheers

Marcus