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

    Smile Java Script Beginner. For Loop Question.

    Hello, Been meaning to join this forum for a while but just now got around to it due to a problem I'm having. I was given an assignment to design a program that accepts five daily temperature readings and display the highest, lowest, and average temperatures. I'm supposed to use a For loop to take the five readings. I'm having trouble in figuring out how to use the data entered by the user to preform calculations on them. As in getting them from the user and storing them to use. So far this is the code I've gotten

    Code:
    <html>
    <body>
    <script type="text/javascript">
    
    // Program name: tempStats
    // Purpose: TempStats, five daily temps. 
    // display high, low, and mean using "forloop"
    // Author: Bran
    // Date last mod: 11-12-'12 
    
    
    // Declare Var and Con
    
    var ES = "";
    var BR = "<br/>";
    var index;
    var PA = "<PA/>";
    var temp;
    var numTemp;
    
    
    //Welcome
    document.write("Welcome to Temperature Stats" + BR);
    
    // Asking for Input from user
    document.write("Enter the number of Temperatures to analyze" + BR);
    numTemps = prompt ("Enter the number of temperatures to analyze:", ES);
    numTemps = parseFloat (numTemps)
    
    
    // forloop
    
    for (index = 1; index <= numTemps; index++) {
    	temp = prompt
    ("Enter the degrees for temperature # " + index,ES);
    temp =parseFloat
    }
    
    </script>
    </body>
    </html>
    I was thinking I was going to use max() and min() and for the mean just use sum and then / by 5 but I can't use the numbers entered by the user in any way. I'm sure this is a very easy problem but I've been stuck on it for a while. Maybe I'm just playing it to close to the vest but if anyone could help, I'd be very thankful.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    1. Don't use prompt() as it is intended for debugging only - use a form in your web page instead to collect the values.

    2. Don't use document.write - that went out of use when Netscape 4 died as subsequent browsers support the far more powerful innerHTML call instead.

    If someone is actually teaching either of those then they need to take a beginners JavaScript class to learn how to write JavaScript for 21st century web browsers.

    To be able to advise how to set up a for loop to process the five input fields we will first need to see how you have defined the form. You can convert strings to numbers using either Number(str) or (+str)

    You can use Math.max() and Math.min() to get the maximum and minimum values.
    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.

  • #3
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post

    If someone is actually teaching either of those then they need to take a beginners JavaScript class to learn how to write JavaScript for 21st century web browsers.
    I'm taking a class using a book published February 22, 2011 as the material and this is week 13 of the class... Haha. Maybe my college and instructor are both behind times.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Week 13 of a JavaScript course and you are only up to using for loops? Or does the course cover a number of web languages and you are just onto the small section of the course that attempts to cover JavaScript?

    Which book is it? There have been one or two JavaScript books that I know of that were published after 2006 that still taught antiquated code like that but none that I know of that were published as recently as 2011.

    Here's how a prompt looks in some browsers now - note the checkbox for turning off JavaScript (some other browsers have the checkbox simply stop any further prompts from appearing instead so that the following code would run but using an empty string for the remaining inputs):

    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.

  • #5
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The class is called "Program Design and Development". We only learn Java Script in it. Oh, that and some QBasic. Haha. Week 13 and yes, just now getting into For Loops.

    This is the book we're using
    http://www.amazon.com/Principles-Pro...ith+javascript

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,990
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    This will move you forward. I leave you to work out how to find the highest and lowest values in the array. Hint - set initial value of highest to -99999 and then loop through the array, if data[i] is greater than highest then highest = data[i].

    Code:
    <html>
    <body>
    <script type="text/javascript">
    
    // Program name: tempStats
    // Purpose: TempStats, five daily temps. 
    // display high, low, and mean using "forloop"
    // Author: Bran
    // Date last mod: 11-12-'12 
    
    
    // Declare Var and Con
    
    var ES = "";
    var BR = "<br/>";
    var index;
    var PA = "<PA/>";
    var temp;
    var numTemp;
    
    
    //Welcome
    document.write("Welcome to Temperature Stats" + BR);  // you can only use document.write() on page load.
    
    // Asking for Input from user
    numTemps = prompt ("Enter the number of temperatures to analyze:", ES);
    numTemps = Number(numTemps) || 0;  // trap NaN entries  Use Number() rather than parseFloat()
    
    // for loop
    var data = [];   // an array to hold the temperatures
    for (index = 0; index < numTemps; index++) {  // note array index starts at 0
    temp = prompt ("Enter the degrees for temperature # " + (index+1),ES);
    temp = Number(temp) || 0;
    data[index] = temp;
    }
    
    alert (data);  // for testing
    var total = 0;
    for (var i = 0; i<data.length; i++) {
    total += data[i];
    }
    var average = total/data.length;
    alert (average);  // when displaying values you will want to use .toFixed(2)
    
    
    </script>
    </body>
    </html>
    
    <body>
    
    </html>

    The book you mention is advertised at $87.32 . Surely that is a joke!
    Last edited by Philip M; 11-19-2012 at 08:08 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.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,990
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by minder View Post
    That's a poor way to do it in general. The same logic for an initial value might come back to bite you in another situation. A better way is to set the initial highest value to the first data value and then check each subsequent data value to see if it is higher or lower to get the max. value of the dataset. When you get a higher value, set the highest value variable to the new highest value.
    Rubbish, bullant! No temperture can be as low as -99999. In other situations I would use another (superior) method to get the min and max values of an array, not the beginner's method I suggest. You can only hope to improve!

    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.

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    closing thread since it was getting hijacked, HeadSetJones please start another thread if you need further help.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    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
    •