Another dice error, code not quite working - javascript

Line 6 says it's expecting an identifier.
I'm trying to make it so rolling a 1 on either die makes the score 0, and rolling doubles doubles your score, otherwise, the score is the sum of the two die.
var die1 = Math.floor(Math.random()*6 + 1);
var die2 = Math.floor(Math.random()*6 + 1);
var score;
if(die1 === 1 || die2 === 1){
score = 0;
else {
score = die1 + die2;
}
if(die1 === die2)
score = 2*(die1 +die2);
else{
score = die1 + die2;
}
}
console.log("You rolled a "+die1+" and a "+die2+" for a score of "+score);

You never closed the bracket before the "else".
Change
if(die1 === 1 || die2 === 1){
score = 0;
else {
score = die1 + die2;
}
to be
if(die1 === 1 || die2 === 1){
score = 0;
} else {
score = die1 + die2;
}

Here is your code properly formatted and with the if statements cleaned:
var die1 = Math.floor(Math.random() * 6 + 1);
var die2 = Math.floor(Math.random() * 6 + 1);
var score;
if (die1 === 1 || die2 === 1)
score = 0;
else if (die1 === die2)
score = 2 * (die1 + die2);
else
score = die1 + die2;
console.log("You rolled a " + die1 + " and a " + die2 + " for a score of " + score);

Related

While loop multiple statements in js. "The Dice Game"

I have to create a dice roller game, if it lands on 2,3,6,12 the game is supposed to stop and tell me which of the numbers the sum of both my dices landed on and how many "rolls" or tries it took me to get there. I haven't been able to figure out why my program isn't working, if I erase the conditions in the while loop and leave only one for example: while(dice != 2); the program will run but it would obviously only match if it lands on 2. Please help me!
function play() {
var dice = -1;
var rolls = 1;
var numb1 = 2;
var numb2 = 3;
var numb3 = 6;
var numb4 = 12;
while (dice != numb1 || dice != numb2 || dice != numb3 || dice != numb4) {
dice = Math.floor(Math.random() * 11) + 2;
rolls++;
}
document.getElementById('results').innerHTML = 'Your dice landed on: ' + dice;
document.getElementById('rolls').innerHTML = 'Rolls: ' + rolls;
}
Change your || to && in your while
function play() {
var dice = -1;
var rolls = 1;
var numb1 = 2;
var numb2 = 3;
var numb3 = 6;
var numb4 = 12;
while (dice != numb1 && dice != numb2 && dice != numb3 && dice != numb4) {
dice = Math.floor(Math.random() * 11) + 2;
rolls++;
}
document.getElementById('results').innerHTML = 'Your dice landed on: ' + dice;
document.getElementById('rolls').innerHTML = 'Rolls: ' + rolls;
}
play()
<div id="results"></div>
<div id="rolls"></div>
you can improve the while condition by using includes
function play() {
var dice = -1;
var rolls = 1;
var numb1 = 2;
var numb2 = 3;
var numb3 = 6;
var numb4 = 12;
while (![numb1, numb2, numb3, numb4].includes(dice)) {
dice = Math.floor(Math.random() * 11) + 2;
rolls++;
}
document.getElementById('results').innerHTML = 'Your dice landed on: ' + dice;
document.getElementById('rolls').innerHTML = 'Rolls: ' + rolls;
}
play()
<div id="results"></div>
<div id="rolls"></div>
Your code doesn't work, because dice would need to be each of these numbers at the same time to work, which is impossible.
Try doing this:
while (![2, 3, 6, 12].includes(dice)) {
Also, your random function can't get 1, maybe try:
Math.floor(Math.random() * 12) + 1

I don't know why my console always outputs the same thing

I was given this simple scenario: “Write a program that simulates the rolling of two dice. Keep rolling the dice UNTIL the sum of the dice is either a 7 OR an 11. Your program should display the results of each roll."
It's very easy, I know. But the sum of each print is always the same and it's not adding properly. Probably just not seeing something right, but I'm a mega-beginner.
NOTE- the write() function just displays text on a screen in this case.
Here is my code:
var die1 = randomNumber(1, 6);
var die2 = randomNumber(1, 6);
var sum = die1 + die2;
while (!(sum == 7 || sum == 11)) {
die1 = randomNumber(1,6);
die2 = randomNumber(1,6);
write("Rolled " + die1 + " and " + die2 + ", sum is " + sum);
}
write("Done.");
You forgot to recalculate the sum inside of the while loop:
var die1 = randomNumber(1, 6);
var die2 = randomNumber(1, 6);
var sum = die1 + die2;
while (!(sum == 7 || sum == 11)) {
die1 = randomNumber(1,6);
die2 = randomNumber(1,6);
// FIX: recalculate sum
sum = die1 + die2;
write("Rolled " + die1 + " and " + die2 + ", sum is " + sum);
}
write("Done.");
Also it would be cleaner with a do-while loop:
var die1, die2, sum;
do {
die1 = randomNumber(1,6);
die2 = randomNumber(1,6);
sum = die1 + die2;
write("Rolled " + die1 + " and " + die2 + ", sum is " + sum);
} while(!(sum == 7 || sum == 11));
write("Done.");
Note: It is also depend on logic written inside randomNumber() function.

Unbalanced tree from document.write and variable un-defined

I am writing a program to display total test grades and avg with a loop. however when I try to test it, I cant get the final message "Good Job" to show up and I am getting a error for unbalanced tree and undefined "Avg" variable. I dont see how "avg" is undefined when it works during the loop just not after
var Testscore = 0;
var Testcount = 0;
var Total = 0;
var Avg = 0;
do {
Testscore = prompt("Enter the test score ", 0);
Testcount = (Testcount + 1);
Total = parseFloat(Testscore) + parseFloat(Total);
Avg = (Total / Testcount);
document.write("Total test score is " + Total + "<br>");
document.write("Average test score is " + Avg + "<br>" + "<br>");
} while (Testcount < 4)
Avg = (Total / Testcount);
if (avg > 80) {
document.write("Good Job!");
} else {
documet.write("Try harder..");
}
You just had a few typos in your code!
avg and Avg are different, as variables are case sensitive.
Additionally, there's a documet instead of document
var Testscore = 0;
var Testcount = 0;
var Total = 0;
var Avg = 0;
do
{
Testscore = prompt("Enter the test score ",0);
Testcount = (Testcount + 1);
Total = parseFloat(Testscore) + parseFloat(Total);
Avg = (Total / Testcount);
document.write("Total test score is " + Total + "<br>");
document.write("Average test score is " + Avg + "<br>" + "<br>");
} while (Testcount < 4)
Avg = (Total / Testcount);
if (Avg > 80)
{
console.log("Good Job!");
}
else
{
console.log("Try harder..");
}

Dice Game - need to roll more dice based upon circumstances

I am developing a game idea. Weapons can be equipped on your character. Different weapons have different amounts of damage dice and critical hit dice. I have it working currently so that the program rolls the appropriate amount of dice based on your equipped weapon.
However, the program only rolls one critical dice, despite whether the equipped weapon contains two critical roll dies.
var WepEquipped = { "name": "Broken Sword", "attack_dice": "2", "critical_dice": "1", "min_base_dmg": "2", "max_base_dmg": "12", "max_total_dmg": "24", "weapon_type": "Slash" };
function Attack(rolls) {
var total = 0;
var dice = [];
for (var i = 1; i <= rolls; i++) {
var d6 = Math.floor((Math.random() * 6) + 1);
$("#dice_container").append("<div class='die_roll'><p class='atk-roll'>" + d6 + "</p></div>");
dice.push(d6);
total += d6;
}
// Needs to be able to roll for multiple critical dice
if ($.grep(dice, function (elem) {
return elem === dice[0];
}).length == rolls) {
var d12 = Math.floor((Math.random() * 12) + 1);
total += d12;
$("#dice_container").append("<div class='die_roll'><p id='crit-roll'>" + d12 + "</p></div>");
}
$("#attack").html("<div>You attack for " + total + "</div>");
};
$('#attack_button').off('click').on('click', function(){
$('.die_roll').hide();
Attack(WepEquipped.attack_dice);
// Attack(WepEquipped.attack_dice);
});
I can explain much more, but I hope this is enough code to grasp what I'm asking. Something here needs to change but I cannot figure out what:
// Needs to be able to roll for multiple critical dice
if ($.grep(dice, function (elem) {
return elem === dice[0];
}).length == rolls) {
var d12 = Math.floor((Math.random() * 12) + 1);
total += d12;
$("#dice_container").append("<div class='die_roll'><p id='crit-roll'>" + d12 + "</p></div>");
}
$("#attack").html("<div>You attack for " + total + "</div>");
};
your grep returns the number of elements in the dice array that equal to your first roll and if the length of that array equals the number of rolls that were made you roll the critical dice once.
if ($.grep(dice, function (elem) {
return elem === dice[0];
}).length == rolls) {
var d12 = Math.floor((Math.random() * 12) + 1);
total += d12;
$("#dice_container").append("<div class='die_roll'><p id='crit-roll'>" + d12 + "</p></div>");
}
$("#attack").html("<div>You attack for " + total + "</div>");
};
If you're trying to roll as many times as the grep returns you need something like this.
var crits = $.grep(dice, function (elem) {return elem === dice[0];});
if( crits.length == rolls ){
for( var x=0;x<crits.length;x++){
var d12 = Math.floor((Math.random() * 12) + 1);
total += d12;
$("#dice_container").append("<div class='die_roll'><p id='crit-roll'>" + d12 + "</p></div>");
}
}
Sorry for the double post, was on an abandoned account.

Why does the score not equal 0 when I roll a 1?

So I'm making a dice game where if either ofd the dice roll a 1, the score is 1, but I can't seem to make that work. I believe everything else it ok.
var die1 = Math.floor(Math.random()*6 + 1);
var die2 = Math.floor(Math.random()*6 + 1);
var score;
if (die1 === 1 || die2 === 1){
score === 0;
}
if (die1 !== 1 || die2 !== 1){
score = die1 + die2;
}
console.log("You rolled a "+die1+" and a "+die2+" for a score of "+score);
First off, you don't set score to 0. Change score === 0 to score = 0.
Second, your second if is still evaluated. Try making it an else if:
if (die1 === 1 || die2 === 1){
score = 0;
}
else if ...
Tying together all of the great suggestions here:
if (die1 === 1 || die2 === 1) {
score = 1; // "if either of the dice roll a 1, the score is 1"
// Also, use = for assignment, not == or ===
}
else { // No need to check the inverse - just use else
score = die1 + die2;
}
if (die1 === 1 || die2 === 1) {
score = 1; // You are not comparing you are assigning here
}
else if ... // Better use else if than using if as in your code
For more information :-
else if javascript

Categories

Resources