Need assistance with a coin flip using HTML and Javascript?

After spending hours trying to make the code work with the example provided, I find myself unable to get it functioning correctly. Is there anyone who can assist me in putting all of this together so that it runs smoothly?

var result,userchoice;
function resetAll(){
    var resetHTML = '<div class="tail"><img src="coin_F.png" /></div><div class="head"><img src="coin_G.png" /></div>';
    setTimeout(function(){
        $('.coinBox').fadeOut('slow',function(){
            $(this).html(resetHTML)
        }).fadeIn('slow',function(){
            $('#btnFlip').removeAttr('disabled');
        });
    },2500);
}
// Checking User Input
$(document).on('change','#userChoice', function(){
    userchoice = $(this).val();
    if(userchoice == "") {
        $(this).parent('p').prepend("<span class='text text-danger'>Please select a coin side to play the game</span>")
        $('#btnFlip').attr('disabled','disabled');
    } else {
        /**/
        $('#btnFlip').removeAttr('disabled');
        $(this).siblings('span').empty();
    }
    return userchoice;
});
// Final result declaration
function finalResult(result,userchoice){
    var resFormat = '<h3>';
    resFormat += '<span class="text text-primary">You choose : <u>'+userchoice+'</u></span> |';
    resFormat += '<span class="text text-danger"> Result : <u>'+result+'</u></span>';
    resFormat += '</h3>';
    var winr = '<h2 class="text text-success" style="color: #49DF3E;">You Won!!</h2>';
    var losr = '<h2 class="text text-danger" style="color: #c34f4f;">You Lost...</h2>';
    if(result == userchoice){
        $('.coinBox').append(resFormat+winr)
    } else{
        $('.coinBox').append(resFormat+losr)
    }
}
// Button Click Actions
$(document).on('click','#btnFlip',function() {
    if($('#userChoice').val() == "") return;
    var flipr = $('.coinBox>div').addClass('flip');
    var number = Math.floor(Math.random()*2);
    $(this).attr('disabled','disabled');
    setTimeout(function() {
        flipr.removeClass('flip');
        //result time
        if(number) {
            result = 'Global';
        //alert('Head = '+number);
            $('.coinBox').html('<img src="coin_G.png" /><h3 class="text-primary">Global</h3>');
            finalResult(result,userchoice);
            resetAll();
        } else {
            result = 'Fortune';
        //alert('Tail = '+number);
            $('.coinBox').html('<img src="coin_F.png" /><h3 class="text-primary">Fortune</h3>');
            finalResult(result,userchoice);
            resetAll();
        }
    },2000);
    return false;
});
#wrapper 
{
    width: 100%;
    height: auto;
    min-height: 500px;
}

.btn
{
    width: 12%;
    background-color: #c34f4f;
    color: white;
    padding: 14px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 22px;
}

.btn:hover
{
    background-color: #A64242;
}

input[type=submit]:hover {
    background-color: #A64242;
}

.container
{
    padding: 50px 0;
    text-align: center;
}

h1
{
    margin-bottom: 100px;
}

.head
{
    margin-top: -205px;
}

.flip img{animation: flipIt 0.5s linear infinite;}
.head img
{
    animation-delay: 0.25s;
}

@keyframes flipIt
{
    0%{width: 0px;
        height: 200px;}
    25%{width: 200px;
        height: 200px;}
    50%{width: 0px;
        height: 200px;}
    100%{width: 0px;
        height: 200px;}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
    <div class="container">
        <div class="row">
            <div class="col-lg-12">
                <h1>Coin Flip | <span>Global or Fortune</span></h1>
            </div>
            <div class="col-lg-12">
                <!--blank-->
                <div class="col-lg-4"></div>
                <!--coin-->
                <div class="col-lg-4">
                    <div class="coinBox">
                        <div class="tail">
                            <img src="coin_F.png" />
                        </div>
                        <div class="head">
                            <img src="coin_G.png" />
                        </div>
                    </div>
                </div>
                <!--user form elements-->
                <div class="col-lg-4 text-left">
                        <p>
                        <div class="form-control">
                          <button name="Global" id="userChoice">Global</button>
                          <button name="Fortune" id="userChoice">Fortune</button>
                        </div>
                        <p>
                            <button class="btn btn-lg btn-primary" id="btnFlip" disabled>Flip It</button>
                        </p>
                </div>
            </div>
        </div>
    </div>
</div>

I attempted to implement this example without success: https://jsfiddle.net/8jw1ogLd/

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

Floating multiple block-level elements within an <li> tag in HTML allows for greater control over the layout and

I am facing a complex issue that requires attention. Let me explain the situation: There is an unordered list consisting of list items displayed vertically with alternating background colors, all having the same width within a fixed-width div. Some list i ...

Anomalous Snackbar and Alert Interactions

Why am I getting an error with this code snippet: import Snackbar from "@mui/material/Snackbar"; import MuiAlert from "@mui/material/Alert"; const Alert = ({children}) => <MuiAlert>{children}</MuiAlert> <Snackbar ope ...

Looking to switch from using HTTP to HTTPS in Node.js on localhost... encountering a few error messages

I'm facing an issue with integrating my code into the entire localhost project "app." It works fine with a simple implementation: https.createServer(options) But when I try to run it within the "app" project using the following setup: const server = ...

What is the process for installing fontawesome using npm?

I encountered an error while attempting to install that looks like the following: $ npm install --save @fortawesome/fontawesome-free npm WARN saveError ENOENT: no such file or directory, open 'C:\Users\Admin\Desktop\package.json&a ...

Issue encountered while attempting to create entity in jhipster

After executing the creation of an entity in the command line, JHipster successfully generated Java and script files but encountered issues with updating the database. No new table was inserted despite starting MySQL and turning off the application befor ...

Input field modified upon focus

I am currently using selectize js in my section to create a select box. My goal is to make the input editable after selecting an option when it is focused on. Check out the live demo: live demo HTML <label>Single selection <select id=" ...

Thumbnail vanishes upon being selected

To access my template site currently, please click here. Currently, the first thumbnail on my website is set up to display a gallery of images in FancyBox once clicked, similar to a question asked on StackOverflow that you can view here. Below is a snipp ...

What role does the "deep" parameter serve when declaring a watcher in VueJS?

I recently discovered a feature in Vue.js called watchers while working on my web app. As I was exploring the API documentation, I came across a flag known as deep. This flag caught my attention because it defaults to false. I'm curious to know what s ...

Show the output of a MySQL query on a modal, alert, or popup box within a webpage initiated by a search box, with the option to close the

Having worked in IT for 16 years, I am new to coding and seeking help on displaying search results from an input HTML box. I want the data from a MySQL database to be displayed in a modal, alert, or popup box with a closing "X" button. This database only c ...

Struggling to employ JavaScript events when submitting a form to verify the fields

My goal is to create a form with fields that have real-time validation using JavaScript. I have achieved this by utilizing Java events in the following way: Text field onkeyup: This event sends a request to check for errors in the field every time a key ...

jQuery Typeahead implementation malfunctioning when text matches either the first or last character

It seems like there is a problem with the .split() method, especially when the characters match the beginning or end of a name. To see the issue in action, visit this JSFiddle link: http://jsfiddle.net/5uebxvex/ I'm working with the Employees tabl ...

Refresh the page following a database update using asynchronous JavaScript and XML

After sending data to the controller using an ajax request, I need to reload my page. The controller will save the data into the DB and then the reloaded page should display the updated content: [Controller] public class ProcessingController : Controller ...

Click to dynamically rotate images using jQuery, or easily swap out images by clicking on them

I am working with 2 images at the moment. My goal is to have the first image visible and the second one hidden initially. When I click on the image, it should switch to display the second image. Clicking again will revert back to the first image. This cyc ...

Exploring AngularJS $compile and the concept of scoping within JavaScript windows

I've encountered a scoping issue with the use of this inside an angular-ui bootstrap modal. The code below functions perfectly outside of a modal, but encounters problems when run within one: var GlobalVariable = GlobalVariable || {}; (fun ...

What is the best way to exclude a field from a Joi schema?

I've defined a Joi schema for a User with the following rules: const userRules = Joi.object({ name: Joi.string().pattern(new RegExp('^[A-Za-zÁÉÍÓÚáéíóúãõÃÕâêôÂÊÔ ]+$')).required(), email: Joi.string().email().requ ...

Obtaining images from JSON data and displaying them in a modal using AngularJS

Utilizing AngularJS to retrieve a JSON file: { "albumId": 1, "id": 1, "title": "accusamus beatae ad facilis cum similique qui sunt", "url": "http://placehold.it/600/92c952", "thumbnailUrl": "http://placehold.it/150/92c952" }, and showcasing it with ...

Tips for resolving an issue with mongoose Model.create becoming unresponsive indefinitely

I'm having trouble understanding why my mongoose Model.create operation isn't completing successfully. The same connection is working well with other controller functions. vscode postman I am attempting to create a new document, but my code s ...

The HTML canvas may sometimes produce inaccuracies in the bytes returned by the getImageData function, a phenomenon known as "

I've come across an issue where the getImageData function of an HTML canvas is returning incorrect byte values. To illustrate, I created a 1x1 pixel image using the following Python code: from PIL import Image import numpy as np a = np.array([[[12, ...

Validating date inputs with ng-change in AngularJS

I am currently utilizing AngularJS along with AngularJS bootstrap within my webpage. One of the components I have is a date picker directive that is structured like this: <div class="form-group {{dateStatus.class}}"> <p class="input-g ...

converting time stamps to written text

Is there a JavaScript library available that can convert a timestamp (Date Java class value) to text? For example: Just two minutes ago 4 hours back 23 August 2009 Does such a library exist? If not, I'll take on the challenge of implementing it ...