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
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Help Please (deadline 7 hours) ( Javascript)

    Hey My Friend and I are sort of new to programming and we are doing a competition involving code.....Goody!
    So we have chosen to use JS ( JavaScript) , We started with a simple code to add one to the starting input and then add 1 to the next value.
    We made a Sample Code which will not open and reports with the error :
    Line : 1
    Char : 1
    Error : Object expected
    Code : 800A138F
    Source : Microsoft JScript Runtime Error.

    also here is the code :

    var line = readline().split(' ')
    var xA = parseInt(line[0])
    var xB = parseInt(line[1])
    var xC = parseInt(line[2])
    print(Math.abs(xA - xB) + Math.abs(xB - xC))

    Thank you for anything in advance .....please help.
    And for no reason whatsoever I'm a 2nd level student in Ireland!....who is actually not drunk!

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    what does readline() look like?

    what is at line 1?

    you should have a look at the javascript console when your code runs in chrome - you will probably get a much more descriptive error message

    failing that, probably better to post your whole code.

    Not drunk you say? Still hungover after St. Pat's?

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    var line = readline().split(' ')

    readline() with parentheses references a function, not a variable.

    There is no print statement in Javascript.
    Last edited by Philip M; 04-03-2014 at 05:42 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Em...yeah about posting the rest of my code....that is all my code, so any words of advice as to the rest of my code would be much appreciated ! and how may I make it a function. I am extremely hung over after St.Pats as a matter of fact

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Quote Originally Posted by denniganman View Post
    Em...yeah about posting the rest of my code....that is all my code, so any words of advice as to the rest of my code would be much appreciated ! and how may I make it a function. I am extremely hung over after St.Pats as a matter of fact
    well, then your code makes no sense whatsoever (the hangover bit, on the other hand, explains mountains).

    what is the starting input? what is this "next value"? surely there's some html associated with this - can we at least see that?

  • #6
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That was talk of the top of my hungover head......I have no idea how we can start this code ....I just know that this is "hopefully" a decent start?

  • #7
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Generally speaking:
    1. figure out what you want it to do
    2. write the html to accept user input and display a result
    3. try to write some javascript that will get you from input to result
    4. if you get stuck, post back here with specific questions.

    Few people here will write your entire code for you, especially if it's homework or a competition or whatever (that's generally seen as cheating). But many people will help you if you show some effort and hit a genuine dead end.

  • #8
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok then, I have made a HTML file with my knowledge of that.....and some CSS.
    I have inserted the JS in a <Script> tag and now we are trying to figure out how to roughly get it working.....just to let you know the code we are making is part of this challenge https://www.calltocode.ie/cf/start-c...st/9/problem/G , if that helps I'm glad it did.

  • #9
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok so in quite a brief moment we turned our whole idea upside down and started from scratch....this is what we've got now:

    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function myFunction()
    {
    var input = prompt("Insert Number Here", "");
    var inputN = parseInt ("input")
    var N1 = inputN/3+1;
    var N2 = inputN/3;
    var N3 = inputN/3-1;
    alert(N1)
    }
    </script>
    </head>

    <body>
    <button onclick="myFunction()">Start</button>
    </body>
    </html>

    Its in the form of a HTMl Doc....obviously ....but then we press the button type in our value it alerts with NaN ...could you please shed some light as to what this means and where we went wrong . Thanks

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by denniganman View Post
    Ok so in quite a brief moment we turned our whole idea upside down and started from scratch....this is what we've got now:

    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function myFunction()
    {
    var input = prompt("Insert Number Here", "");
    var inputN = parseInt ("input")
    var N1 = inputN/3+1;
    var N2 = inputN/3;
    var N3 = inputN/3-1;
    alert(N1)
    }
    </script>
    </head>

    <body>
    <button onclick="myFunction()">Start</button>
    </body>
    </html>

    Its in the form of a HTMl Doc....obviously ....but then we press the button type in our value it alerts with NaN ...could you please shed some light as to what this means and where we went wrong . Thanks

    NaN means "not a number". That is because you have placed quote marks around "input" thus making it a literal string. But it is not very good practice to use the HMTL tagword input as a Javascript variable. It may cause probems, especially in older IE.


    Code:
    <!DOCTYPE html>
     <html>
     <head>
     <script type = "text/javascript">
     function myFunction()  {
     var input = prompt("Insert Number Here", "");
     var inputN = parseInt (input,10) || 0;  // trap NaN entries - if the value of input is NaN use 0 instead
     var N1 = inputN/3+1;
     var N2 = inputN/3;
     var N3 = inputN/3-1;
     alert(N1 + "  "  + N2 +  "  " + N3);
     }
     </script>
     </head>
    
     <body>
     <button onclick="myFunction()">Start</button>
     </body>
     </html>
    Be aware that both prompts and alerts are long-obsolete ways of getting information from and displaying a message to your users.

    Note that even though inputN is an integer the results of your calculations are real. You could use .toFixed(2) to show the results to 2 decimal places.

    Code:
    var N1 = (inputN/3+1).toFixed(2);
     var N2 = (inputN/3).toFixed(2);
     var N3 = (inputN/3-1).toFixed(2);
    Last edited by Philip M; 04-04-2014 at 07:22 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    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
    •