1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

$5 for adding a small piece of working javascript

Discussion in 'JavaScript' started by explorer, Jan 4, 2008.

  1. #1
    I'll paypal $5 to the first person who posts clean, working javascript that works in all major browsers and does the following:

    Background

    I already have some code that takes inputs from people on an html page as follows.

    function getTotal()
    {
    var v1 = parseInt(document.getElementById("txt1").value)
    var v2 = parseInt(document.getElementById("txt2").value)
    var v3 = parseInt(document.getElementById("txt3").value)
    var v4 = parseInt(document.getElementById("txt4").value)
    var v5 = parseInt(document.getElementById("txt5").value)

    and then we do:

    var stp = 0
    if (isNaN(v1))
    {
    stp = 1
    document.getElementById("txt1").value = "";
    document.getElementById("txt1").focus()
    alert("Not a valid entry!")
    return
    }

    etc, etc,etc.


    So I get five numbers which are then added:


    var total = v1+v2+v3+v4+v5;
    document.getElementById("total").value = total;



    The Job

    Please modify the script so that, instead of printing the total, it prints phrases on the html page depending on what the total is -

    IF the total is less than 5, output “you like butter” on the page

    If the total is more than or equal to 5 and less than 10, output “you like cheese”

    If the total is more than or equal to 10 and less than 15, output “you like milk”

    If the total is more than or equal to 15 and less than 29, output “you like yogurt”

    If the total is more than or equal to 20 and less than 25, output “you like custard”

    If the total is more or equal to 25 and less than 30, output “you like cream”
     
    explorer, Jan 4, 2008 IP
  2. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Didn't test it, but you could try this:

    
    var total = v1+v2+v3+v4+v5;
    var str = "";
    
    if (total < 5)
    {
    	str = "you like butter";
    }
    else if ((total >=5)&&(total <10))
    {
    	str = "you like cheese";
    }
    else if ((total >=10)&&(total <15))
    {
    	str = "you like milk";
    }
    else if ((total >=15)&&(total <20))
    {
    	str = "you like yogurt";
    }
    else if ((total >=20)&&(total <25))
    {
    	str = "you like custard";
    }
    else if ((total >=25)&&(total <30))
    {
    	str = "you like cream";
    }
    
    document.getElementById("total").value = str;
    
    Code (markup):
     
    hogan_h, Jan 4, 2008 IP
  3. explorer

    explorer Well-Known Member

    Messages:
    463
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    110
    #3
    It works! Thank you. Please PM me your paypal email and I'll paypal you the $5.
     
    explorer, Jan 4, 2008 IP
    hogan_h likes this.
  4. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You're wellcome and keep the money, just give me some rep for the answer, that's all ;)

    The code could eventually be further optimized, something along these lines should work fine too:
    
    if (total < 5)
    {
    	str = "you like butter";
    }
    else if (total <10)
    {
    	str = "you like cheese";
    }
    else if (total <15)
    {
    	str = "you like milk";
    }
    ... etc ...
    
    Code (markup):
     
    hogan_h, Jan 4, 2008 IP
    explorer likes this.