cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div id="total"><div id="numRight"></div></div>
<div id="wrapper">    
    <h1>
        <span id="num1"></span>
        <span id="sign"></span>
        <span id="num2"></span>
        =
        <input id="userAnswer" type="number"/>
    </h1>
    <button id="submit">Solve</button>
    <button id="reset">Next &rarr;</button>
    <fieldset id="chooseSign">
    <input type="radio" name="operator" id="add" checked="checked"/><label for="add">+</label>
    <input type="radio" name="operator" id="subtract" /><label for="subtract">&ndash;</label>
    <input type="radio" name="operator" id="multiply" /><label for="multiply">X</label>
    <input type="radio" name="operator" id="divide" /><label for="divide">&divide;</label>
    </fieldset>
</div>
            
          
!
            
              * {
    box-sizing: border-box;
}
#wrapper {
  max-width: 600px;
  margin: 0 auto;
}
body {
    margin: 0 auto;
    padding: 0;
    font: 2em/1.5 Arial, sans-serif;
    text-align: center;
    background-color: aquamarine;
}
h1 { 
    font-weight: normal;
  line-height: 1;
  margin: .25em 0;
    color: rebeccapurple;
}
#total {
    background-color: white;
    height: 10px;
    width: 100%;
}
#numRight {
    background-color: limegreen;
    height: 10px;
    width: 0px;
    transition: all 0.2s;
}
#userAnswer {
    color: rebeccapurple;
    font-size: 1em;
    width: 25%;
    text-align: center;
    background-color: aquamarine;
    border: none;
    border-bottom: 5px solid rebeccapurple;
}
#userAnswer:active, #userAnswer:focus {
    outline: none;
}
button {
    width: 45%;
    height: 2em;
    color: white;
    font-size: 1em;
    letter-spacing: 1px;
    text-transform: uppercase;
    border-radius: 10px;
    background-color: rebeccapurple;
    border: none;
}
button:disabled {
    color: grey;
    background-color: aquamarine;
    box-shadow: none;
}
input[type="radio"] {
    display: none;
}
fieldset {
    margin-top: 1em;
    border: none;
}
input:checked + label {
    color: white;
    background-color: rebeccapurple;
}
label {
    color: rebeccapurple;
    background-color: white;
    border-radius: 10px;
    display: inline-block;
    width: 20%;
}
            
          
!
            
              // Query DOM for page elements
var leftnum = document.getElementById("num1");
var rightnum = document.getElementById("num2");
var operator = document.getElementById("sign");
var submit = document.getElementById("submit");
var reset = document.getElementById("reset");
var userAnswer = document.getElementById("userAnswer");
var mathSigns = document.getElementsByName("operator");
var totalDiv = document.getElementById("total");
var numRightDiv = document.getElementById("numRight");
// Initialize solution and counters
var solution, runningTotal = 0, tally = 0;
// Store available math symbols in array
// var mathSigns = ["+", "x"];
// var mathSigns = ["+", "-", "X", "\u00F7"];
// Add event handlers to both form buttons
submit.addEventListener("click", solve, false);
reset.addEventListener("click", newEquation, false);
document.getElementsByTagName("fieldset")[0].addEventListener("click", init, false);

// Generate new numbers and math signs
function init() {
    // Generate two random numbers
    // between 1 and 10
    var num1 = Math.ceil(Math.random() * 99);
    var num2 = Math.ceil(Math.random() * 9);
    for (var i = 0; i < mathSigns.length; i++) {
      if (mathSigns[i].checked) {
        chosenSign = mathSigns[i];
      }
    }
    // var pickedSign = mathSigns[Math.floor(Math.random() * mathSigns.length)]

    // Place random numbers in their
    // respective <span>s on the page
    leftnum.textContent = num1;
    rightnum.textContent = num2;
    operator.textContent = chosenSign.nextElementSibling.textContent;
    
    userAnswer.textContent = "";
    // Focus the keyboard on the answer field
    userAnswer.focus();
    
    if (submit.hasAttribute("disabled")) {
        submit.removeAttribute("disabled");
        submit.style.color = "white";
        submit.textContent = "Solve";
        userAnswer.value = "";
        userAnswer.style.backgroundColor = "aquamarine";
        userAnswer.style.color = "rebeccapurple";
        reset.setAttribute("disabled");
    }
    reset.setAttribute("disabled", "disabled");
    
    // Return an array containing the randomly generated items
    return numbers = [num1, num2, chosenSign.id];
}

function newEquation() {
    if (!submit.hasAttribute("disabled")) {
        solve();
    }
    submit.textContent = "Solve";
    submit.style.color = "white";
    tally++;
    userAnswer.style.backgroundColor = "aquamarine";
    userAnswer.style.color = "rebeccapurple";
    userAnswer.value = "";
    userAnswer.focus();
    submit.removeAttribute("disabled");
    init();
}

function increaseProgressWidth() {
    if (numRightDiv.style.width != "0px") {
        var currentWidth = Number(numRightDiv.style.width.slice(0,2));
        currentWidth += 10;
        numRightDiv.style.width = currentWidth + "%";
        if (numRightDiv.style.width == "100%") {
                    document.body.innerHTML = "<h1>Great job</h1><p>You solved 10 math problems!</p><a href='javascript:history.go(0);'>Again</a>";
        }
    } else {
        numRightDiv.style.width = "10%";
    }
}

// Display answer on page
function solve() {
    switch (numbers[2]) {
        case "add":
            solution = numbers[0] + numbers[1];
            break;
        case "subtract":
            solution = numbers[0] - numbers[1];
            break;
        case "multiply":
            solution = numbers[0] * numbers[1];
            break;
        case "divide":
            solution = numbers[0] / numbers[1];
            break;
    }
    if (userAnswer.value == solution) {
        userAnswer.style.backgroundColor = "white";
        userAnswer.style.color = "green";
        this.textContent = "Correct";
        this.style.color = "green";
        runningTotal++;
        increaseProgressWidth();
    } else {
        userAnswer.style.backgroundColor = "crimson";
        userAnswer.style.color = "white";
        userAnswer.value = solution;
        this.textContent = "Wrong";
        this.style.color = "crimson";
    }
    submit.setAttribute("disabled", "disabled");
    reset.removeAttribute("disabled");
    return solution;
}

init();
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console