0

I'm writing a choose your own adventure program where If a specific option is chosen (example to wait) the user gets a random number between 1-10 to do push ups(the push-ups would be the user clicking on the prompt "ok" button however many times the random number is equal to) here's my code so far but I keep getting errors. I'm a complete noob so go easy on me.

 var count = Math.floor((Math.random() * 10) + 1);
var setsOf10 = false;
function pushUps() {
  alert("Nice! Lets see you crank out " + pushUps + "!");
}
if (setsOf10 == pushUp) {
    alert("Nice! Lets see you crank out " + pushUp + "!");
    setsOf10 = true;
  }
for (var i=0; i<count; i++){
  pushUps();
}
  else {
    alert("Really, thats it? Try again");
  }

while ( setsOf10 == false);
}

After playing with this some more I can tell i'm close but still don't have it. and again, I'M NOT ASKING YOU TO SOLVE THIS FOR ME JUST NEED POINTERS AS TO WHAT IM DOING WRONG OR MISSING. Here's what I have, Its giving me my random number I just need it to allow me to click the "ok" button however many times the random number has assigned me.

    var pushUpSets = Math.floor((Math.random() * 10) + 1);
function pushUps(){
  alert(pushUpSets);
  if (pushUpSets < 3){
    var weak = "Thats it? Weak sauce!";
    alert(weak);
  }
  else{
    alert("Sweet lets get some reps in!");
  }
  for (i=0; i>3; i++){
pushUps(pushUpSets);
}
}
8
  • alert("Nice! Lets see you crank out " + pushUps + "!"); <-- pushUps is a function... I see no prompt and I do not see pushUp defined anywhere. Commented Feb 25, 2019 at 21:23
  • Oh duh! should I define pushUp after my setsOf10? like so... var pushUp = setsOf10; ? Commented Feb 25, 2019 at 21:30
  • pushUp is supposed to be the number of push ups the user has done correct? Then you would need to change that somewhere and you would want it declared right underneath count because you need to access it from the different functions. Commented Feb 25, 2019 at 21:34
  • You would not want it set to the same as setsOf10 because that is a bool and you want to add to the number of push ups as they click okay. Commented Feb 25, 2019 at 21:35
  • the amount of pushUps is suppose to be the result of my random number between 1 - 10. should I have Math.floor((Math.random() * 10) + 1); inside my pushUps function? rather than my count variable? Commented Feb 25, 2019 at 21:36

1 Answer 1

1

Here, the make a choice button is just dummy to allow us to go to do push ups. Each click decrements our count.

// This is important, we use this event to wait and let the HTML (DOM) load
// before we go ahead and code. 
document.addEventListener('DOMContentLoaded', () => {
  document.querySelector('#choice').addEventListener('click', makeChoice);
});

function makeChoice() {
  // Call a method to set random pushups and setup the click event
  setUpPushUp();
  // Here we change the display style of the push up section so that it shows to the player.
  document.querySelector('.activity').style.display = 'block';
}

// The pushups variable is declared at the document level
// This way our setUpPushUp and doPushUp functions have easy access.
let pushUps = 0;

function setUpPushUp() {
  // Create a random number of pushups, in sets of 10.
  // We add an extra 1 so we can call the doPushUp method to initialize.
  pushUps = (Math.floor((Math.random() * 10)+1)*10)+1 ;

  // Add a click event to the push up button and call our doPushUp method on each click.
  document.querySelector('#push').addEventListener('click', doPushUp);
  
  // This is just an init call, it will use the extra 1 we added and place test in our P tag.
  doPushUp();
}


function doPushUp() {
  // Get a reference to our output element, we will put text to player here.
  let result = document.querySelector('p');
  // They have clicked, so remove a push up. 
  pushUps--;
  
  // See if the player has done all the required push ups (i.e. pushUps is 0 or less.)
  if (pushUps > 0) {
    result.innerText = `You need to crank out ${pushUps} pushUps`;
  } else {
    result.innerText = 'Nice work!';
  }

}
.activity {
  display: none;
}
<button id="choice">Make a choice !</button>
<div class="activity">
  <p></p>
  <button id="push">Push</button>
</div>

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

6 Comments

You kinda solved the whole thing for him here and this is clearly a school question...
Maybe I should remove?
I would if I were you. Even if it's not for school the idea seems to be to learn how to do things, and you didn't include a full tutorial.
If you want to remove what you posted go on a head, I am just asking for help as to what i'm doing wrong, not asking you to solve this for me. The answer provided is not what Ill be using as its not what I asked. Forget I asked, Ill just look for help with my code else where. SO is not working out for me.
Not at all. I added the comments so it is clear what I am doing. :) If it helps thats great.
|

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.