1

I am working on writing code for a course and need to figure out why the code output is not executing properly. I am very new to coding, this is a beginner assignment so all help and explanations are greatly appreciated.

The output should look like this:

Output: 

How many times to repeat?  2
Stats Solver execution 1 ====================
give a number  10.10203
give a number  20
give a number  30
give a number  40
give a number  50
sum: 150.10203
average: 30.020406
max: 50
min: 10.10203
ratio: 4.94
Stats Solver execution 2 ====================
give a number  3.21
give a number  2.1
give a number  1
give a number  5.4321
give a number  4.321
sum: 16.0631
average: 3.21262
max: 5.4321
min: 1
ratio: 5.43
done ====================

Here is the code:

"use strict";

function myMain() {
  var num = Number(prompt("give a number of times to repeat, must be 0 or greater"));
  var count = num;
  for (var a=0; a<=num; a++) {count++;}
  alert("Stats Solver execution " + num + " ===================");
  if (num===0){alert("done ==================="); return;}
  wall()
  alert("done ===================");

   
}                
function wall(){  
  var num1 = Number(prompt("provide a number"));
  var num2 = Number(prompt("provide a second number"));
  var num3 = Number(prompt("provide a third number"));
  var num4 = Number(prompt("provide a fourth number"));
  var num5 = Number(prompt("provide a fifth number"));
  
  var sum = (num1+num2+num3+num4+num5);
  alert("sum: " + sum);
  var avg = (sum/5);
  alert("average: " + avg);
  var max = (Math.max(num1,num2,num3,num4,num5));
  alert("max: " + max);
  var min = (Math.min(num1,num2,num3,num4,num5));
  alert("min: " + min);
  var ratio = (max/min);
  alert("ratio: " + Math.floor(ratio*100)/100);

}

myMain(); 
   

5
  • What is it that's wrong with your program? Do you want it to print out that output instead of showing the little alert windows? Commented Jun 18, 2018 at 20:52
  • I am not sure what is wrong, maybe also output your current output. One very obvious issue I see is that your loop runs from 0 to num (inclusive) which means it runs num+1 times. So you should consider changing the <= num to < num. Commented Jun 18, 2018 at 20:54
  • Your for loop currently does nothing but increment count which isn’t used anywhere else. Why do you need this variable? Just put the actual code you want to repeat in the for loop. Commented Jun 18, 2018 at 20:55
  • Also, not sure exactly what the role of count is. The actual code needs to be in the loop and use a instead of num to get the iteration number. Commented Jun 18, 2018 at 20:56
  • The code that should be repeated is the function.wall listed below the myMain function. I'm trying to call that function in myMain, if that makes sense? I apologize if I am not explaining this well. Commented Jun 18, 2018 at 21:08

1 Answer 1

1

Well you really aren't very far off at all. Actually your solution has all the code you need just some of it is in the wrong place. I would have posted this as a comment but as this is a new work account I can't actually post comments so here is a full solution with the explanations.

Your wall function, while annoying with all the alerts is actually correct and doesn't need any adjustments. With that said you might want to play with parseInt and parseFloat to make sure you are getting valid numbers but I am assuming that is outside of the scope of the assignment.

Now on to your main function.

var num = Number(prompt("give a number of times to repeat, must be 0 or greater"));

This is ok and will prompt the user for a number, once again you might want to test that you got a valid number using the aforementioned links.

var count = num;
  for (var a=0; a<=num; a++) {count++;}
  alert("Stats Solver execution " + num + " ===================");
  if (num===0){alert("done ==================="); return;}
  wall()
  alert("done ===================");

This is where things start to fall apart a bit and where i think you are having problems. So I will break this down line for line and explain what each line is doing and you can compare that to what you think its doing.

var count = num;

Nothing crazy here, you are just creating another variable to hold the value in the num variable. Slightly redundant but not really a big deal.

for (var a=0; a<=num; a++) {count++;}

This is the line that appears to have given you the most confusion. This is the actual loop but inside the body of the loop { .... } nothing is being done except 1 is being added to count (count++). If I am understanding the assignment correctly, inside this loop is where you need to call your wall function after alerting the 'Stats Solver execution .....' stuff. All you need to do is move your function call inside this loop.

if (num===0){alert("done ==================="); return;}
      wall()
      alert("done ===================");

This part is clearly you a little lost and just trying things to get it to work, don't worry even after 12+ years of development i still write code like this ;). You really don't need this as the actual call to wall() will work fine for you.

I am bored and am waiting on work so for the sake of being a complete answer here is an example of how your code should look. Please don't just hand this in rather try and actually understand the difference between what i wrote and what you did because these are some very basic concepts that if you gloss over will make your life much harder down the road. Always feel free to ask, thats how people learn.

function myMain(){
    // get the number of repetitions from the user and cast as a number
    var num = Number(prompt('Please enter the number of times to repeat'));
    // loop from 0 to the number the user provided - 1 hence the < 
    for (var i = 0; i < num; i++){
        // alert the iteration of the loop, you will notice i add 1 to num when we alert it; this is because it starts at 0 so the first time it displays it would show 'Stats Solver execution 0 ======' instead of 'Stats Solver execution 1 ======'
        alert('Stats Solver execution ' + (num + 1) + ' ===============');
        // call your wall function
        wall();
        // go back to the top of the loop
    }
    // alert that we are done. 
    alert('done===============')
}
// your wall function goes here
// call your myMain function to kick everything off
myMain();

Just for fun you might want to look at console.log instead of alert to not make it such an annoying process with all the popups.

If i missed anything or you are confused about anything don't hesitate to ask and i'll do my best to answer.

Sign up to request clarification or add additional context in comments.

2 Comments

I did not reply to this but I SINCERELY appreciate your explanation. I went back and watched some videos to get a better understanding of loops. They are so challenging for me to understand for some reason. Your explanation was great and I did not just turn in what you sent, as it will serve me not purpose in the future to just copy someone else's work. Thank you!
@user9958716 Glad to hear I could help out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.