Reputation: 79
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>
Upvotes: 0
Views: 146
Reputation: 101
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>
Upvotes: 1
Reputation: 508
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);
}
Upvotes: 2
Reputation: 40
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>
Upvotes: 1
Reputation: 121
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
}
}
Upvotes: 1