www.webdeveloper.com
Results 1 to 14 of 14

Thread: Simple Javascript help Please

  1. #1
    Join Date
    Jan 2011
    Posts
    6

    Simple Javascript help Please

    the user inpouts 1 of 3 codes to bring up a box displying the discout dependent on code selected can someone please help I beleive the while script is the one to use but am finding it near impossible to master


    FSSAVE, ISSAVE and SOSAVE.


    If (Discount code entered is FSSAVE)
    give discount one
    else if (discount code entered is ISSAVE)
    give discount 2
    else if
    (discount code entered is SOSAVE)
    give discount 3





    var discount; fSSAVE; iSSAVE; sOSAVE

    fSSAVE = 20p
    iSSAVE = 30p
    sOSAVE = 40p


    discount = window.prompt('Please enter a discount code,' Here');

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Code:
    function getDiscount() { 
      var applyDiscount = "0p"; 
      var thisDiscount = prompt("Please enter a discount code.");
      switch(toLowerString(thisDiscount)) {
        case "fssave":
          applyDiscount = "20p"; alert("Your discount is 20p");
        break;
        case "issave":
          applyDiscount = "30p"; alert("Your discount is 30p");
        break;
        case "sosave":
          applyDiscount = "40p"; alert("Your discount is 40p");
        break;
        default:
          alert("You did not enter an authorized discount code");
        break;
      }
    }

  3. #3
    Join Date
    Jan 2011
    Posts
    6
    thanks so much for your help I tried the code you supplied in firefox but it dosent seem to work is it a problem with my browser. Could you give a small explanation of the code involved to help me understand it further thanks so much

    <SCRIPT LANGUAGE = "JavaScript">

    function getDiscount() {
    var applyDiscount = "0p";
    var thisDiscount = prompt("Please enter a discount code.");
    switch(toLowerString(thisDiscount)) {
    case "fssave":
    applyDiscount = "20p"; alert("Your discount is 20p");
    break;
    case "issave":
    applyDiscount = "30p"; alert("Your discount is 30p");
    break;
    case "sosave":
    applyDiscount = "40p"; alert("Your discount is 40p");
    break;
    default:
    alert("You did not enter an authorized discount code");
    break;
    }
    }


    </SCRIPT>
    </HEAD>
    <BODY>
    </BODY>
    </HTML>

  4. #4
    Join Date
    Jan 2011
    Posts
    6
    thanks for this but it still dosent seem to work in my browser could you please have another look at it thankyou.

    If you could also give a brief ewxplanation to help me understand Javascript better I would be very grateful

  5. #5
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    How are you calling the function? In an onLoad, or onClick, or onMouseOver.. etc.

  6. #6
    Join Date
    Mar 2009
    Posts
    521
    Shouldn't the line
    Code:
    switch(toLowerString(thisDiscount)) {
    read
    Code:
    switch(toLowerCase(thisDiscount)) {
    instead?

  7. #7
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Yikes.. good eye, Tcobb. I thought I _had_ typed toLowerCase(). Thanks for pointing that out.
    Code:
    function getDiscount() { 
      var applyDiscount = "0p"; 
      var thisDiscount = prompt("Please enter a discount code.");
      switch(toLowerCase(thisDiscount)) {
        case "fssave":
          applyDiscount = "20p"; alert("Your discount is 20p");
        break;
        case "issave":
          applyDiscount = "30p"; alert("Your discount is 30p");
        break;
        case "sosave":
          applyDiscount = "40p"; alert("Your discount is 40p");
        break;
        default:
          alert("You did not enter an authorized discount code");
        break;
      }
    }
    ^_^ (is very tired)

  8. #8
    Join Date
    Jan 2011
    Posts
    6

    Thanks Very Much Guys

    Im just learning Javascript and am a complete beginner. thanks so much this is a function script rather than a while or else script could you please explain the advantages of this to a real beginer ty for all your help I apreciate it.
    Im not getting this to work Im pasting it into a document like this

    <HTML>
    <HEAD>
    <TITLE>
    Solution


    </TITLE>
    <SCRIPT LANGUAGE = "JavaScript">
    function getDiscount() {
    var applyDiscount = "0p";
    var thisDiscount = prompt("Please enter a discount code.");
    switch(toLowerCase(thisDiscount)) {
    case "fssave":
    applyDiscount = "20p"; alert("Your discount is 20p");
    break;
    case "issave":
    applyDiscount = "30p"; alert("Your discount is 30p");
    break;
    case "sosave":
    applyDiscount = "40p"; alert("Your discount is 40p");
    break;
    default:
    alert("You did not enter an authorized discount code");
    break;
    }
    }


    </SCRIPT>
    </HEAD>
    <BODY>
    </BODY>
    </HTML>

    but am getting no windows prompt box when I open the web page? could you also explain this code to help me to understand it further thanks for all your help
    Last edited by Mr Graham; 01-17-2011 at 02:26 PM.

  9. #9
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Since it is a function, it will not run unless called or referred to. If you want it to load automatically, then add the following to the BODY tag:
    onload="getDiscount();"
    ^_^

    I have a conference call in a few minutes.. I'll have to explain it, later.

  10. #10
    Join Date
    Jan 2011
    Posts
    6

    Clarification

    Im obviously not explaining myself well I do apologise.

    What I need is a basic script that takes up a windows prompt box in a web browser that allows a user to input a discount code.
    Upon doing this another windows prompt box should output or display a message showing the discount gained using the discount code supplied by the user.
    the code you have supplied looks good but maybe im just want a simple code utilising the while or else statement

    Hope this makes sense : - ) thankyou for your help Im grateful
    Last edited by Mr Graham; 01-17-2011 at 02:51 PM.

  11. #11
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Code:
    function getDiscount() { 
    	var applyDiscount = "0p"; 
    	var thisDiscount = prompt("Please enter a discount code.","");
    	var td = thisDiscount.toLowerCase();
    	switch(td) {
    		case "fssave":
    		applyDiscount = "20p"; alert("Your discount is 20p");
    		break;
    		case "issave":
    		applyDiscount = "30p"; alert("Your discount is 30p");
    		break;
    		case "sosave":
    		applyDiscount = "40p"; alert("Your discount is 40p");
    		break;
    		default:
    		alert("You did not enter an authorized discount code");
    		break;
    		}
    	}
    I'm so tired I used the toLowerCase incorrectly. It's working, now.
    Last edited by WolfShade; 01-17-2011 at 03:44 PM.

  12. #12
    Join Date
    Jan 2011
    Posts
    6
    thanks Ive removed the function text and 1 bracket

    oh nice Ive got the windows prompt box coming up with a message to input code however no output upon entering a code thanks very much how do I get the output displayed :bow:

    Its really just a learning excersise this for me rather than code to implant on a web page its part of a course Im doing at college

  13. #13
    Join Date
    May 2013
    Posts
    1
    Congratulations Mr Graham - I presume that you passed your Open University course!

  14. #14
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    Quote Originally Posted by elly3802 View Post
    Congratulations Mr Graham - I presume that you passed your Open University course!
    rofl

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles