0

I'm trying to store two random numbers between 0 and 10, in two different variables play1 and play2.

The maximum value for play2 will be 10 minus play1 value, ie, if play1 gets 6, maximum value for play2 will be 4.

how can I trigger play2 to store the second value? I need it to be triggered with a second click.

var min = 0;
var max = 10;
var play1;
var play2;


function myFunction() {
  play1 = Math.floor(Math.random() * (max - min + 1)) + min;
  max = 10 - play1
  document.getElementById("result").innerHTML = play1;
  
  if (play1 < 10 ) {
    //trigger play2 
  } 
  
}
<button onclick="myFunction()">Click me</button>
<p id="result"></p>

4
  • simply play2 = Math.floor(Math.random() * (max - min + 1)) + min; is it not? Commented Oct 3, 2017 at 23:22
  • Hi Jaromanda X, i've edited the question as i forgot to mention i needed the second value to be also triggered by a second click Commented Oct 3, 2017 at 23:41
  • clicking the same button? or a different one? Commented Oct 3, 2017 at 23:51
  • the same button Commented Oct 3, 2017 at 23:58

4 Answers 4

2

You can simply calculate the value of play2 the same way you calculated the value of play1 by using - play2 = Math.floor(Math.random()*(max-min+1)) + min;. I have updated your code below-

var min = 0;
var max = 10;
var play1;
var play2;


function myFunction() {
  play1 = Math.floor(Math.random() * (max - min + 1)) + min;
  max = 10 - play1
  document.getElementById("result").innerHTML = play1;
  
  if (play1 < 10 ) {
    play2 = Math.floor(Math.random()*(max-min+1)) + min; 
  } else {
    play2 = 0;
  }
  document.getElementById("result2").innerHTML = play2;
  
}
<button onclick="myFunction()">Click me</button>
<p id="result"></p>
<p id="result2"></p>

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

1 Comment

Thats spot on, but now i see I made the wrong question, i need the second value to be triggered with a second click. my apologies for the mistake.
1

Don't repeat yourself. That's what functions are for:

function r(min, max){
  return Math.floor(Math.random() * (max-min+1)) + min;
}

var min = 0;
var max = 10;
var play1;
var play2;

function myFunction() {
  play1 = r(min, max);  
  play2 = r(min, max - (play1-min)); 

  document.getElementById("result").innerHTML = [play1, play2];
}
<button onclick="myFunction()">Click me</button>
<p id="result"></p>

i need the second value to be triggered with a second click.

function r(min, max){
  return Math.floor(Math.random() * (max-min+1)) + min;
}

var min = 0;
var max = 10;
var play1;
var play2;
var nthClick = 0;

function myFunction() {
  ++nthClick;
  if(nthClick & 1){
    //odd
    play1 = r(min, max);
    play2 = null;
  }else{
    //even
    play2 = r(min, max - (play1-min)); 
  }
  document.getElementById("result").innerHTML = [play1, play2];
}
<button onclick="myFunction()">Click me</button>
<p id="result"></p>

like this?

2 Comments

Thats spot on, but now i see I made the wrong question, i need the second value to be triggered with a second click. my apologies for the mistake
@JoãoMateus I've extended the answer. Better?
1

Perhaps this

var min = 0;
var max = 10;
var play1;
var play2;


function myFunction() {
    var target = document.getElementById("result");
    var firstNum = parseInt(target.innerHTML);
    if (isNaN(firstNum)) { // first click
        play1 = Math.floor(Math.random() * (max - min + 1)) + min;
        target.innerHTML = play1;
    } else {
        play2 = Math.floor(Math.random() * ((10 - firstNum) - min + 1)) + min;
        // do what with it????? you haven't shown!
        document.getElementById("result2").innerHTML = play2;
    }  
}
<button onclick="myFunction()">Click me</button>
<p id="result"></p>
<p id="result2"></p>

Comments

1

Try adding a control to the click count. Code copied and modified from Thomas' answer

var controlVariable = 1;

function r(min, max){
  return Math.floor(Math.random() * (max-min+1)) + min;
}

var min = 0;
var max = 10;
var play1;
var play2;

function myFunction() {
  if (controlVariable == 1) {
    play1 = r(min, max);
    controlVariable++; 
  }
  else if (controlVariable == 2) {
    play2 = r(min, max - (play1-min));
    controlVariable = 1;
  }

  document.getElementById("result").innerHTML = [play1, play2];
}
<button onclick="myFunction()">Click me</button>
<p id="result"></p>

1 Comment

this is it! it works! great tip with the controlvariable, took me a while to get it but i see what you did there. thank you very much Zach A

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.