...

View Full Version : if...else script wont execute



D33
04-29-2008, 02:50 PM
Hi everyone, hope you can help.

Not the hottest on javascript yet so have probably done something really minor wrong but my code just will not execute!!! ARGH!


<HEAD>
<TITLE>TMA 02 Q4(iii)</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE = "JavaScript">

var currentPrice;
var bidIncrement;
var newPrice;
currentPrice = window.prompt('Please enter the current bid price', '');

if (currentPrice < 10)
{
bidIncrement = currentPrice + 0.1
}
else
{
if (currentPrice >=10 <100)
{
bidIncrement = currentPrice + 0.5
}
else
{
if (currentPrice >=100 <1000)
{
bidIncrement = currentPrice + 1
}
else
{
if (currentPrice = >= 1000)
{
bidIncrememnt = currentPrice + 10
}
}
}
};

newPrice = currentPrice + bidIncrement;

document.write('The new bid is ' + newPrice);
</SCRIPT>
</BODY>
</HTML>



I have a feeling I have gone wrong on currentPrice+increment bit but could do with the help on this one :(

Many thanks! Let's see how good I get at this over the coming months :D

D33

Philip M
04-29-2008, 03:26 PM
<TITLE>TMA 02 Q4(iii)</TITLE> - sounds like homework!

You have a good many errors. Try this - compare with your own work and learn from it!.


<HEAD>
<TITLE>TMA 02 Q4(iii)</TITLE>
</HEAD>
<BODY>

<script type = "text/javascript"> // Note that <script language = "Javascript"> is long deprecated

var currentPrice;
var bidIncrement;
var newPrice;
currentPrice = window.prompt('Please enter the current bid price', '');

if (currentPrice < 10) {
bidIncrement = 0.1;
}

else if (currentPrice >=10 && currentPrice <100) {
bidIncrement = 0.5;
}

else if (currentPrice >= 100 && currentPrice <1000 ) {
bidIncrement = 1;
}

else if (currentPrice >= 1000) {
bidIncrememnt = 10;
}


newPrice = parseFloat(currentPrice) + parseFloat(bidIncrement); // values must be numbers, not strings

document.write('The new bid is &#163;' + newPrice);
</script>

</BODY>
</HTML>

Quizmaster: Who was the Norwegian Explorer who first reached the South Pole?
Contestant: Sherpa Tenzing

D33
04-29-2008, 04:03 PM
Hi Philip,

You are right this is homework. It's not really something I can ask my parents (carpenter+shop worker) for help with as i'm sure you can imagine lol.

But thank you for giving me some pointers. What was confusing me was the use of parseFloat. My examples showed it used with 'if' statements but didn't give anything to do with numbers when they explained if... else!

Anyway no doubt i'll be needing help sometime again but in the mean time i'll try to help others when I can.;)

coothead
04-29-2008, 04:17 PM
Hi there D33,

and a warm welcome to these forums. ;)

You might like to check for correct prompt inputs, also...


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>TMA 02 Q4(iii)</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">

var currentPrice;
var bidIncrement=0;
var newPrice;

window.onload=function(){
init();
}

function init() {
currentPrice=prompt('Please enter the current bid price', '');
if((isNaN(currentPrice))||(currentPrice=='')||(currentPrice==null)) {
alert('numbers only');
init();
}
else {
currentPrice=parseFloat(currentPrice);
setBid();
}
}

function setBid() {
if(currentPrice<10){
bidIncrement=0.1;
}
else {
if((currentPrice>=10)&&(currentPrice<100)){
bidIncrement=0.5;
}
else {
if((currentPrice>=100)&&(currentPrice<1000)){
bidIncrement=1;
}
else {
if(currentPrice>=1000) {
bidIncrement=10;
}
}
}
}
newPrice=currentPrice+bidIncrement;
document.getElementById('theBid').firstChild.nodeValue='The new bid is '+newPrice;
}
</script>

</head>
<body>

<div id="theBid">&nbsp;</div>

</body>
</html>


coothead

Philip M
04-29-2008, 04:39 PM
Hi coothead,

Yes, an obvious improvement, but D33 will never be able to pass your script off as his own work!

e.g. document.getElementById('theBid').firstChild.nodeValue='The new bid is &#163;'+newPrice;

:D:D

Is it raining in Chertsey? It sure is here!

D33 - have a look at:-
http://www.kourbatov.com/faq/convert2.htm

coothead
04-29-2008, 04:49 PM
Hi there Philip,

Is it raining in Chertsey? It sure is here!
Yes it is, unfortunately. :(
Otherwise, I would have been out on the golf course, instead of sitting here typing...

firstChild.nodeValue
...for what appears to be an Open University course. :D

coothead

Philip M
04-29-2008, 04:59 PM
Otherwise, I would have been out on the golf course...

During the Cold War there was a summit meeting of all the world's leaders. One afternoon Kennedy and Krushchev decide to play a round of golf. Kennedy goes round in 4 strokes under par, but most of Krushchev's shots end up in the rough.

Isvestia reported the event as follows: "Yesterday the world's leaders took part in a golf competition. Although he had never played the game before Comrade President Krushchev achieved second place. The American President finished next to last."

:D:D:D

coothead
04-29-2008, 05:08 PM
Hi there Philip,

I play a similar game to Nikita Sergeyevich but have never had it described in such glowing terms. ;)

coothead

Arty Effem
04-29-2008, 06:21 PM
Hi everyone, hope you can help.

Not the hottest on javascript yet so have probably done something really minor wrong but my code just will not execute!!! ARGH!

You already know what the original errors were, but you can aid maintainability and simplify your code by tabulating the rates and just looping through them.

<script type = "text/javascript">

var currentPrice;

var rates = [ {n:1000, inc:10}, {n:100, inc:1}, {n:10, inc:0.5}, {n:0, inc:0.1} ];

while( !(currentPrice = window.prompt('Please enter the current bid price', '') )
|| isNaN( currentPrice=parseFloat( currentPrice ) )
|| currentPrice <= 0)
alert('Please enter positive number');

for(var i=0; i<rates.length && rates[i].n > currentPrice; i++)
;

document.write('The new bid is ' + (currentPrice + rates[i].inc) );

</script>

Philip M
04-29-2008, 06:32 PM
As I said, D33 will not be able to pass this off as his own work! Remember that he is a student! :D:D

D33
04-30-2008, 08:21 AM
Morning everyone! Thanks for all the replies! Wonderful to see so much detail in your codings... I doubt i'll ever get to the level you are at because the specific javascript part of this course is one part of a larger project. But knowing any is better than none - right!

What Philip said is true, I won't be able to pass off yours as mine but I only really came for a pointer in the right direction so it's good to see "there is more than one way to skin a cat".

Thanks again, D33

Philip M
04-30-2008, 08:30 AM
But knowing any is better than none - right!


Wrong! A little knowledge is a dangerous thing.

First used by Alexander Pope (1688 - 1744) in An Essay on Criticism, 1709:

"A little learning is a dangerous thing; drink deep, or taste not the Pierian spring: there shallow draughts intoxicate the brain, and drinking largely sobers us again."

Other well-known quotations from Pope:-

A bird in the hand is worth two in the bush.
Fools rush in where angels fear to tread.
To err is human, to forgive, divine.
The ends must justify the means.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum