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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Someone plzzz help with script code!!!

    ive managed to keep a B in this class only because the test are multiple choice, but when it comes to writing code i can understand it after its wrote but have a hard time with logic.(plus im right brain dominant) but this is what i was given as a template, that needs to be modified. The goal is to prompt someone for the year they were born, find out how old they are with a date object and a basic function, then it displays the age, and last part is with an if statement, if the persons age is over 40 it has to change the bgcolor to red. The thing is this template seems so far from what i need i cant figure out how to modify it or start from scratch...

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> AGE CALCULATOR </TITLE>
    <script>
    var startTime = new Date();
    var startInt = startTime.getTime();
    var elapsedTime = new Date();
    function getTime()
    {
    var output ="";
    currentTime=new Date();
    var currentInt = currentInt - startInt;
    var elapsedInt = currentInt - startInt;
    elapsedTime = new Date (elapsedInt);
    output += elapsedTime.getMinutes()+":";
    output += elapsedTime.getMilliseconds()+"\n";
    document.myForm.txtOutput.value +=output;
    }
    </script>
    </HEAD>
    <BODY bgcolor=tan text=black>
    <h1><center><b>Date Object</b></center>
    <hr><hr>
    </h1>
    <center>
    <form name="myForm">
    <table border=4>
    <tr>
    <td><input type="button" value="Get Time"
    onClick="getTime()">
    </td>
    <td><input type="text" name="textOutput">
    </td>
    </tr>
    </HTML>
    </table>
    </form>
    </body>

    //main thing is i dont understand how the new Date getYear commands work,
    i assume i prompt for year, get the current year and subtract difference, than a simple if statement for bgcolor, but i just cant apply it. Someone plzzz help!!!!!!!

  • #2
    New to the CF scene
    Join Date
    Apr 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok without using the Date objects this is what i have..

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <h1> AGE CALCULATOR </H1>
    </HEAD>
    <BODY bgcolor =blue text=green>
    <SCRIPT>
    var year
    var curryear=2006
    var difference

    //function bgcolor

    year=prompt("Enter Year You Were Born")
    year=eval(year)
    difference= curryear - year
    alert(difference)

    if (difference < 40)
    {

    }



    </script>
    <body bgcolor = red>
    </BODY>
    </HTML>

    does anyone at least know what i have to put between the brackets to get the bgcolor to change to red?? do i use a function?? im not sure how..

  • #3
    New to the CF scene
    Join Date
    Apr 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    solved my own problem!!!

    it was through trial and error, nothing more, no real logic, just a lot of guessing, but now i makes sense. ill be back with more questions next week.

    <SCRIPT>

    var year
    var myDate =new Date()
    var difference

    year=prompt("Enter Year You Were Born")
    year=eval(year)
    alert("Thank You")
    myDate=(myDate.getYear())
    difference= myDate - year
    alert("You are " + difference + " years old")

    if (difference > 40)
    document.bgColor="red"

    //so simple i wanna scream

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,628
    Thanks
    0
    Thanked 648 Times in 638 Posts
    The more correct way to do it would be:

    document.getElementsByTagName('body')[0].style.backgroundColor = '#f00';

    That bgcolor attribute has already been long deprecated and may not exist in the next generation of browsers.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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