Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    D33
    D33 is offline
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    if...else script wont execute

    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!

    Code:
    <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

    D33
    Last edited by D33; 04-29-2008 at 02:56 PM. Reason: topic title edited

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    <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!.

    Code:
    <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
    Last edited by Philip M; 04-29-2008 at 03:32 PM.

  • Users who have thanked Philip M for this post:

    D33 (04-29-2008)

  • #3
    D33
    D33 is offline
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,680
    Thanks
    0
    Thanked 230 Times in 225 Posts
    Hi there D33,

    and a warm welcome to these forums.

    You might like to check for correct prompt inputs, also...
    Code:
    
    <!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

  • Users who have thanked coothead for this post:

    D33 (04-30-2008)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    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;



    Is it raining in Chertsey? It sure is here!

    D33 - have a look at:-
    http://www.kourbatov.com/faq/convert2.htm
    Last edited by Philip M; 04-29-2008 at 04:43 PM.

  • #6
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,680
    Thanks
    0
    Thanked 230 Times in 225 Posts
    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.

    coothead

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by coothead View Post
    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."


  • #8
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,680
    Thanks
    0
    Thanked 230 Times in 225 Posts
    Hi there Philip,

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

    coothead

  • #9
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by D33 View Post
    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.
    Code:
    <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>

  • Users who have thanked Arty Effem for this post:

    D33 (04-30-2008)

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    As I said, D33 will not be able to pass this off as his own work! Remember that he is a student!

  • #11
    D33
    D33 is offline
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by D33 View Post
    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.
    Last edited by Philip M; 04-30-2008 at 08:36 AM. Reason: More quotes


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •