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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy basic code help for newbie

    here is the js code:

    var firstName = window.prompt("Please Enter Your First Name");
    var secondName = window.prompt("Please Enter Your Second Name");
    var widthofRoom = window.promt("Please Enter The Width Of The Room In Metres");
    var lengthofRoom = window.promt("Please Enter The Length Of The Room In Metres");
    var areaofRoom = lengthofRoom * widthofRoom;
    widthofRoom = parseFloat(widthofRoom);
    lengthofRoom = parseFloat(lengthofRoom);
    var costPerSqMetre = window.prompt("Please Enter The Cost Per Square Metre");
    var totalCost = costPerSqMetre * areaofRoom;
    costPerSqMetre = parseFloat(costPerSqMetre);
    areaofRoom = parseFloat(areaofRoom);
    document.write = ("hello") + "firstName" + " " + "secondName" + ("The Area Of The Room Was") + "areaofRoom" + ("When Multiplyed By The Flooring Costs Gave A Total Price Of ") + "totalCost.toFixed(2)" + "As of" + new Date().toString());


    and here is the site code:


    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Assignment 1 Task 1</title>
    </head>
    <body>
    <h1>draft1</h1>
    <script type="text/javascript" src="oneass.js"></script>
    </body>
    </html>


    it runs the first 2 lines of code prompting for names but im stuck and i cant ifgure out whats up

  • #2
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    not that i have corrected the promt on width and lenth and nothing is happening now just the draft1 apears on site

  • #3
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    *sorry wireless keyboard is playing up so it keeps missing chars

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    try like this ...

    document.write ("hello" ,firstName ," ",secondName," The Area Of The Room Was ",areaofRoom," When Multiplyed By The Flooring Costs Gave A Total Price Of ",totalCost.toFixed(2)," As of ",new Date().toString());

  • #5
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    216
    Thanks
    0
    Thanked 29 Times in 27 Posts
    Javascript variables are not Strings !
    Code:
    document.write = ("hello") + "firstName" + " " + "secondName" + ("The Area Of The Room Was") + "areaofRoom" + ("When Multiplyed By The Flooring Costs Gave A Total Price Of ") + "totalCost.toFixed(2)" + "As of" + new Date().toString());
    
    // firstName, secondName... are not strings but javascript variables !
    // Then write them without quotes... but concatenate strings !
    document.write = "(hello)" + firstName + " " + secondName + "(The Area Of The Room Was)" + areaofRoom + "(When Multiplyed By The Flooring Costs Gave A Total Price Of )" + (totalCost.toFixed(2)) + " As of " + (new Date().toLocaleString()));
    Last edited by 007julien; 01-26-2014 at 03:06 PM.

  • #6
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    does it matter if the quotes are on the outside of brackets like "(hello)" or can i still use the ("hello")

  • #7
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    and thx guys btw

  • #8
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    also i know variables arnt strings is that wahat was messing it up at the
    (new Date().toLocaleString()))

  • #9
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    216
    Thanks
    0
    Thanked 29 Times in 27 Posts
    "Hello" is a string, which do not need parenthesis.

    It's not the same with a method like toLocaleString() which is to applied at new Date(), but, after tests, the parenthesis is not necessary...
    Last edited by 007julien; 01-26-2014 at 05:04 PM.

  • #10
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,465
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Unless your target browser is Netscape 4 you should be using a form instead of prompt() and you should be using innerHTML instead of document.write().

    Also parseFloat is not the right function for converting a string to a number - the Number() function serves that purpose.
    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.


  •  

    Tags for this Thread

    Posting Permissions

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