Experiencing difficulties with a JavaScript dice game

In my game, I'm facing an issue where one button needs to trigger two functions. The player die is working correctly, but the computer die isn't displaying a value.

function Welcome()
{
        alert("Welcome " + document.getElementById("fname").value + "!");
}
 
function oldEnough(age)
{
        if (age< 18)
        {alert("UNDER 18 LEAVE NOW");
        }
}
 
//player dice roll//
function rollDice() {
        var die1 = document.getElementById("die1");
        var die2 = document.getElementById("die2");
        var status = document.getElementById("status");
        //random number between 1 and 6(whole number)//
        var d1 = Math.floor(Math.random() * 6) + 1;
        var d2 = Math.floor(Math.random() * 6) + 1;
        var diceTotal = d1 + d2;
        //shows the random number value//
       
        die1.innerHTML = d1;
        die2.innerHTML = d2;
        status.innerHTML = "You Rolled "+diceTotal+".";
        if (d1 == d2){
                status.innerHTML +=" Doubles! Roll Again ";
        }
}
 
//computer dice roll//
function compDice() {
                var die3 = document.getElementById("die3")
                var die4 = document.getElementById("die4")
                var status2 = document.getElementById("status2")
               
                var d3 = Math.floor(Math.random() *6) +1;
                var d4 = Math.floor(Math.random() * 6) +1;
                var diceTotal = d3 + d4;
               
                die3.innerHTML = d3;
                die4.innerHTML = d4;
                status2.innerHTML = "Computer Rolled "+diceTotal+".";
}
div.dice{
        float:left;
        width:32px:
        background:#D6D6D6;
        border:#999 1px solid;
        padding:10px;
        font-size:24px;
        text-align:center;
        margin:5px;
}
 
 
div.die{
        float:left;
        width:32px:
        background:#D6D6D6;
        border:#999 1px solid;
        padding:10px;
        font-size:24px;
        text-align:center;
        margin:5px;
}
<form action="#" method="get">
<p>
Player Name:
<input id="fname" name="fname" type="text" size="20" maxlength="20" onblur="Welcome()" />
</p>
 
Age:
<input id="age" name="age" id="age" type="text" size="3" maxlength="3" onBlur="oldEnough(this.value)"/>
</p>
 
<div id="die1" class="dice">0</div>
<div id="die2" class="dice">0</div>
<button onclick="rollDice(); compDice();">Roll Dice</button>
<h2 id="status" style="clear:left;"></h2>
 
 
 
<div id="die3" class="die">0</div>
<div id="die4" class="die">0</div>
<h2 id="status2" style="clear:right;"></h2>

Answer №1

Revise the following:

<button onclick="rollDice()";"compDice()">Roll Dice</button>

to:

<button onclick="rollDice();compDice()">Roll Dice</button>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am struggling to pass my matrix of structures to a function after utilizing the malloc function

I am looking to establish a matrix that includes structures. The structure I am using is: typedef struct { int a; int b; int c; } myStruct; Here is the approach I am taking to create the matrix: myStruct (*matrix)[WIDTH] = malloc(HEIGHT * size ...

Having trouble achieving the desired results with C file management

As I tackle my school assignment, I've encountered a significant roadblock. I'm struggling to write to a file and retrieve input using scanf and fgets. The First Problem: FILE *f1; char date_trans[100][15]; f1 = fopen("test.txt", "w"); if ...

When a user clicks on JavaScript, it will return true if clicked and false if

Currently working with selenium Java code and incorporating JavaScript execution using the executeScript method, as seen below: WebElement menu = driver.findElement(By.cssSelector(string1)); ((JavascriptExecutor) driver).executeScript("arguments ...

Show User-Specific Information Using DataTable

After conducting extensive research, I have been unable to find a suitable example to reference. My goal is to customize my DataTable so that it only displays data relevant to the currently logged-in user (admin accounts will have access to all data). I am ...

The function being called within useEffect is rendering too quickly, causing it to break. However, it functions correctly after

When using React-id-swiper to load images for products, I encountered an issue with setting the useState 'loading' state to false after all images have been successfully loaded. In this case, there are 5 products. To achieve this, I implemented ...

Pressing the button does not register outside of the loop function

Just as the title says, I am experiencing an issue where my click event is not being triggered when I click on my button. The button itself was created using a JavaScript loop. Interestingly, I found that when I placed my click event function inside the lo ...

Isn't it strange how a Java.lang.ArrayIndexOutOfBoundsException error occurred, yet the program managed to execute

I am working on a project that involves generating a histogram based on user input, including the number of lines and the actual numbers for each line. The program is functioning correctly, with the exception of an error message: Exception in thread ...

Utilizing Angular 6 and JavaScript to invoke two functions within an (ngClick) event in both the Component and JavaScript

I have a requirement to execute two functions in my click event, one for my component and the other for a custom JavaScript function. Here is the code snippet: Angular click event: <button type="button" class="btn btn-primary" (click)="Plans(); " [att ...

What strategies can I implement to stop Iframes from disrupting the browser's history when interacting with them?

In my particular situation, I utilize Iframes to display Grafana on my page, which showcases beautiful and user-friendly graphs. After examining, I noticed that interactions like zooming in or out on the graph using mouse clicks trigger a type of refresh ...

nextJS does not recognize the term 'Window'

I'm encountering the error "window is not defined" in my NextJS project. The variable isMobile determines whether the window size is less than 767.98 to handle the hamburger menu functionality. This code worked in ReactJS but seems to be causing issue ...

"Error encountered when trying to initialize a new string array with a null

I am attempting to create two string arrays, one for each team. To achieve this, I have designed the following classes: public class MyTeamWeapons { public string[] weapons; } public class MyLoadout { public MyTeamWeapons[] teamWeapons; ...

Error: The variable fullName has not been declared

To start off, I need to develop a registration form that includes fields for full name, email, password, mobile number, and date of birth. Once the email validation is successfully completed and the submit button is clicked, the user should be redirected t ...

When launching a .app file, the Node.Js subprocess fails to initiate, however, it successfully starts when executed from a Unix executable

Currently, I am developing an application using ElectronJS with a Python backend. The communication between Electron and Python is facilitated through Flask, which hosts a webserver to handle JSON data exchange. The issue I am facing is that the applicati ...

jQuery Failing to Work

I'm experimenting with creating a falling effect for a DIV element when a piece of text is hovered over. The original effect can be seen here. var $dropDiv = $('#dropDiv'); $('#holder p').on('hover', function () { // ...

Tips for invoking the export function in node.js using sequelize

Need help with calling the function (search_all_user) to export in node.js using sequelize module.exports = function (sequelize, Sequelize, DataTypes) { var User = sequelize.define('user', { id: {type: Sequelize.INTEGER, unique: true, prima ...

Require Google Chrome to show a blank page upon refresh

After reloading the page in either IE/Edge or Chrome/Firefox, I noticed a significant difference. IE/Edge clears the page and displays a white page, while Chrome/Firefox does not. I'm wondering if there is a way to use JavaScript to instruct Chrome/F ...

Wait until the user submits the form before activating Angular validations, and do not display any validation messages if the user deletes text from a text box

I am looking to implement angular validations that are only triggered after the user clicks on submit. I want the validations to remain hidden if the user removes text from a textbox after submitting it. Here is what I need: - Validations should not be dis ...

Error: When attempting to submit to MondoDB, a TypeError occurred stating that property '_id' cannot be created on the number '1'

After retrieving 25 new comments in JSON format from a subreddit, my code converts it to a string, removes special characters, and generates a dictionary to track word frequency. The challenge lies in deciding whether to treat each word as a separate entry ...

Display an array in php

Here is the challenge I am facing: array(2, 2, 2, 2, 2, 3, 4, 2, 1, 2, 5, 5, 68); I want the output to be like this: There are a total of 6 "2"s in the array 1 "3" 1 "4" 2 "5"s 1 "68" Any suggestions on how to achieve this? ...

Struggling with grasping the concept of a pointer to an array of n elements in C (int (*p)[10])?

I have been delving into the realm of pointers in C and have come up with the following code snippet: int main(){ int a = 10; int *pa = &a; printf("Value of pa : %p\n",pa); //0x7fffd215b5b8 printf("Value of *pa : %d& ...