0

I would like to compare the output of dice1() with the output of dice2(), I suspect I have to return the value, however I do not know how to. At the bottom, as comment, there is just the beginning of the conditional I would like to use but the values inside do not reflect what I thought.

function dice1() {
  const rdm1 = Math.random()
  let m = rdm1 * 6
  if (m >= 1) {
    let floor = (Math.floor(m) + 1);
    console.log("this is dice 1 = " + floor)
  } else if (m < 1) {
    let ceil = (Math.floor(m) + 1);
    console.log("this is dice 1 = " + ceil)
  }
}
dice1()

function dice2() {
  const rdm2 = (Math.random() * Math.random())
  let m = rdm2 * 6
  if (m >= 1) {
    let floor = (Math.floor(m) + 1);
    console.log("this is dice 2 = " + floor)
  } else if (m < 1) {
    let ceil = (Math.floor(m) + 1);
    console.log("this is dice 2 = " + ceil)
  }
}

dice2()

//if (dice1() < dice2()) {
//console.log( dice2() + " is bigger than " + dice1());
//}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
  <h1>Dice Game</h1>

  <script src="main.js"></script>
</body>

</html>
Matteo
  • 79
  • 8
  • "*I suspect I have to return the value, however I do not know how to.*" `return floor;` and `return ceil;` where `floor`/`ceil` are created. – VLAZ May 19 '21 at 19:36
  • 1
    On the same topic, see: [Generate random number between two numbers in JavaScript](https://stackoverflow.com/q/4959975) – VLAZ May 19 '21 at 19:37
  • 1
    I don't get that `if (m >= 1) {` condition. Both branches are doing exactly the same thing? – Bergi May 19 '21 at 19:41
  • @Bergi in addition, `dice2()` multiplies two random numbers together. That makes the result *less* random. – VLAZ May 19 '21 at 19:43
  • 1
    though it is an excellent way to get a higher concentration of rolls near 0 – Other Me May 19 '21 at 19:44
  • @VLAZ Not less random, but it skews the distribution - which might be desired here. Otherwise there wouldn't be a reason to have a second function… – Bergi May 19 '21 at 19:45
  • @Bergi right less *uniformly random* is what I meant. – VLAZ May 19 '21 at 19:45
  • @VLAZ good point about generating random numbers and thank you for your suggestions @ Bergi thank you! the 2 functions are almost the same, in the second Math.random() is doubled (in a very not uniform way) in order to create a different output – Matteo May 20 '21 at 08:27

4 Answers4

2

You use the return statement to return a value from a function like so

function add(a, b) {
  return a + b;
}

console.log(add(1, 2)); //should send 3 to the console

In your particular case here is what it would look like

function dice1() {
  const rdm1 = Math.random()
  let m = rdm1 * 6
  if (m >= 1) {
    let floor = (Math.floor(m) + 1);
    console.log("this is dice 1 = " + floor)
    return floor;
  } else if (m < 1) {
    let ceil = (Math.floor(m) + 1);
    console.log("this is dice 1 = " + ceil)
    return ceil;
  }
}
dice1()

function dice2() {
  const rdm2 = (Math.random() * Math.random())
  let m = rdm2 * 6
  if (m >= 1) {
    let floor = (Math.floor(m) + 1);
    console.log("this is dice 2 = " + floor)
    return floor;
  } else if (m < 1) {
    let ceil = (Math.floor(m) + 1);
    console.log("this is dice 2 = " + ceil)
    return ceil;
  }
}
dice2()

if (dice1() < dice2()) {
  console.log( dice2() + " is bigger than " + dice1());
}

Also I think your code may not have the effect you intend

if (dice1() < dice2()) {
  console.log( dice2() + " is bigger than " + dice1());
}

This will log newly random generated numbers rather than the ones used in your conditional statement. To fix this store the results in a variable

const r1 = dice1();
const r2 = dice2();
if (r1 < r2) {
  console.log( r2 + " is bigger than " + r1);
}
Other Me
  • 498
  • 2
  • 7
1
function dice1() {
  const rdm1 = Math.random()
  let m = rdm1 * 6
  if (m >= 1) {
    let floor = (Math.floor(m) + 1);
    console.log("this is dice 1 = " + floor)
    return floor
  } else if (m < 1) {
    let ceil = (Math.floor(m) + 1);
    console.log("this is dice 1 = " + ceil)
    return ceil
  }
}

function dice2() {
  const rdm2 = (Math.random() * Math.random())
  let m = rdm2 * 6
  if (m >= 1) {
    let floor = (Math.floor(m) + 1);
    console.log("this is dice 2 = " + floor)
    return floor
  } else if (m < 1) {
    let ceil = (Math.floor(m) + 1);
    console.log("this is dice 2 = " + ceil)
    return ceil
  }
}
Zhang Chandra
  • 81
  • 1
  • 6
1

function dice1() {
  const rdm1 = Math.random()
  let m = rdm1 * 6
  let value;
  if (m >= 1) {
    value = (Math.floor(m) + 1);
    console.log("this is dice 1 = " + value)
  } else if (m < 1) {
    value = (Math.floor(m) + 1);
    console.log("this is dice 1 = " + value)
  }
  return value;
}
dice1()

function dice2() {
  const rdm2 = (Math.random() * Math.random())
  let m = rdm2 * 6
  let value;
  if (m >= 1) {
    value = (Math.floor(m) + 1);
    console.log("this is dice 2 = " + value)
  } else if (m < 1) {
    value = (Math.floor(m) + 1);
    console.log("this is dice 2 = " + value)
  }
  return value
}

if (dice1() < dice2()) {
  console.log( dice2() + " is bigger than " + dice1());
}else{
  console.log( dice1() + " is bigger than " + dice2()); 
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
  <h1>Dice Game</h1>

  <script src="main.js"></script>
</body>

</html>
1

I think you mean to use math.ceil in your else statements as the if and else statements are doing the same thing. Nevertheless, just return the values from the if and else statements (returning a value will break the execution flow and next statements in the function wont be executed).

You are calling dice1() and dice2() twice and since these functions are generating random numbers, the results will vary in each call. It is best to call them once and store the results in variables.

Below is changes to your code (I have not changed math.floor to math.ceil in the else statements).

function dice1() {
    const rdm1 = Math.random()
    let m = rdm1 * 6
    if (m >= 1) {
      let floor = (Math.floor(m) + 1);
        console.log("this is dice 1 = " + floor)
        return floor;
    } else if (m < 1) {
      let ceil = (Math.floor(m) + 1);
        console.log("this is dice 1 = " + ceil)
        return ceil;
    }
  }
  var dice1_result = dice1()
  
  function dice2() {
    const rdm2 = (Math.random() * Math.random())
    let m = rdm2 * 6
    if (m >= 1) {
      let floor = (Math.floor(m) + 1);
        console.log("this is dice 2 = " + floor)
        return floor;
    } else if (m < 1) {
      let ceil = (Math.floor(m) + 1);
        console.log("this is dice 2 = " + ceil)
        return ceil;
    }
  }
  
  var dice2_result = dice2()
  
  if (dice1_result < dice2_result) {
  console.log( "dice 2 result " + dice2_result + " is bigger than " + "dice 1 result "+dice1_result);
}
else if(dice1_result > dice2_result)
  {
    console.log( "dice 1 result " + dice1_result + " is bigger than " + "dice 2 result "+dice2_result);

}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
  <h1>Dice Game</h1>

  <script src="main.js"></script>
</body>

</html>
  • Thank you. he Math.Ceil issueis fixed , but more importantly the return and const concepts are now introduced and it makes sense. – Matteo May 20 '21 at 12:44