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

Thread: debug my script

  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    debug my script

    Hi I'm a newbie to javascript and I made this script

    Code:
    <script>
    var num = parseInt(prompt("Enter the number of integers to follow"));
    var sum = 0;
    for (i = 0; i < num; i++){
    sum += parseInt(prompt("Enter a number"));
    }
    
    if (isNaN(num)) {
    alert("Invalid");
    }
    
    else {
    if (sum < 0) {
    document.writeln("The sum is 0 and the average is 0");
    }
    
    else {
    document.writeln("The sum is " + sum + " and the average is " + sum/num);
    }
    }
    </script>
    The scenario : Create in javascript that will read a series of integers at the terminal. The first integer is special, as it indicates how many more integers will follow. Your javascript is to calculate the sum and average of the integers, excluding the first integer, and display these values to the screen.
    If the total is not greater than 0 then display "The sum is 0 and the average is 0"

    Did I write the script correctly? am I missing anything that a dumb person might do? for example the person might type in letters instead of numbers.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Obviously this is homework.

    You can avoid the input being undefined:-

    var num = parseInt(prompt("Enter the number of integers to follow",""));


    As you say, the user may type in letters rather than numbers, and you need to trap that, and any blank entry. Also presumably disallow negative numbers.

    You should note that document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot sensibly be used to update the content of your page after that page has loaded.


    “A man ceases to be a beginner in any given science and becomes a master in that science when he has learned that he is going to be a beginner all his life.” Robin G. Collingwood (English Philosopher, 1889-1943)

  • #3
    New Coder
    Join Date
    Jan 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have this script in the <body>. Should I move it to the <head> ?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by Ruriko View Post
    I have this script in the <body>. Should I move it to the <head> ?
    If you want. It is preferable, but in this situation it does not really matter.

    http://www.tutorialspoint.com/javasc..._placement.htm
    Last edited by Philip M; 05-25-2009 at 01:15 PM.


  •  

    Posting Permissions

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