Answers for "basic calculator using javascript"

3

a simple javascript calculator

<div class="calculator">

  <input type="text" class="calculator-screen" value="" disabled />
  
  <div class="calculator-keys">
    
    <button type="button" class="operator" value="+">+</button>
    <button type="button" class="operator" value="-">-</button>
    <button type="button" class="operator" value="*">×</button>
    <button type="button" class="operator" value="/">÷</button>

    <button type="button" value="7">7</button>
    <button type="button" value="8">8</button>
    <button type="button" value="9">9</button>


    <button type="button" value="4">4</button>
    <button type="button" value="5">5</button>
    <button type="button" value="6">6</button>


    <button type="button" value="1">1</button>
    <button type="button" value="2">2</button>
    <button type="button" value="3">3</button>


    <button type="button" value="0">0</button>
    <button type="button" class="decimal" value=".">.</button>
    <button type="button" class="all-clear" value="all-clear">AC</button>

    <button type="button" class="equal-sign operator" value="=">=</button>

  </div>
</div>
Posted by: Guest on February-11-2020
2

how to create a simple calculator in html

<!DOCTYPE html>
<html lang="en">
<head>
<style>
.contain{
display: flex;
}
#display{
  width: 98%;
  
}
.mathButtons{
  width: 100px;
  height: 40px;
  background-color: rgb(0,0,0,0.3);
  color: white;
}
.mathButtons:hover{
  background-color: DodgerBlue;
  
  }
  #clearButton:hover{
    background-color: orangered;
  }
  #container{
  
    width: 430px;
    height: 300px;

    }
    
    
    .buttondigits{
      background-color:  black;
      color: white;
      height: 40px;
      width: 100px;
    }
    #clearButton{
      width: 100px;
      height: 40px;
      background-color: rgb(0,0,0,0.3);
      font-weight: bold;
    }
	.buttondigits:hover {
background-color: gray;
}

</style>
<title> standard calculator </title>
<meta charset="utf-8">
<link rel="stylesheet" href="cal.css"/>
</head>
<body>
  
     <div class="container">
	<fieldset id="container">
	  <form name="calculator">
<input type="text" id="display" name="display" readonly>
<br>
<br>
<br>
<div class="contain">
<input class="buttondigits" type="button" value="7" onclick="calculator.display.value += '7'">
<input class="buttondigits" type="button" value="8" onclick="calculator.display.value += '8'">
<input class="buttondigits" type="button" value="9" onclick="calculator.display.value += '9'">
<input class="mathButtons" type="button" value="+" onclick="calculator.display.value += '+'">
</div>

<div class="contain">
<input class="buttondigits" type="button" value="4" onclick="calculator.display.value += '4'">
<input class="buttondigits" type="button" value="5" onclick="calculator.display.value += '5'">
<input class="buttondigits" type="button" value="6" onclick="calculator.display.value += '6'">
<input class="mathButtons" type="button" value="-" onclick="calculator.display.value += '-'">
</div>

<div class="contain">
<input class="buttondigits" type="button" value="1" onclick="calculator.display.value += '1'">
<input class="buttondigits" type="button" value="2" onclick="calculator.display.value += '2'">
<input class="buttondigits" type="button" value="3" onclick="calculator.display.value += '3'">
<input class="mathButtons" type="button" value="*" onclick="calculator.display.value += '*'">
</div>

<div class="contain">
<input id="clearButton" class="button" type="button" value="C" onclick="calculator.display.value = ''">
<input class="buttondigits" type="button" value="0" onclick="calculator.display.value += '0'">
<input class= "mathButtons" type="button" value="=" onclick="calculator.display.value = eval(calculator.display.value)">
<input class="mathButtons" type="button" value="/" onclick="calculator.display.value += '/'">
</div>
</form> 
</fieldset>
</div>
</body>
</html>
Posted by: Guest on September-10-2020
0

calculator JavaScript

class Calculator {
  constructor(numbers, num1, num2){
      this.numbers = numbers;
      this.num1 = num1;
      this.num2 = num2;
  }

    add(numbers){
        var sum = 0;

        for(let i = 0; i < numbers.length; i++){
            if(typeof numbers[i] === "number"){
                sum += numbers[i];
            }
        }

        return sum;
    }
    multiply (numbers) {
        let total = 1;

        for (let i = 0; i < numbers.length; i++) {
            if (typeof numbers[i] === 'number')
            total *= numbers[i]
        };

        return total;
    }

    subtract (num1, num2) {
        return num1 - num2
    }
    
    divide (num1, num2) {
        return num1 / num2
    }
}
Posted by: Guest on August-26-2021
0

how to make a calculator websiteJS

let add =(...params) => {
    let sum = 0;
    params.forEach(el =>{
        sum = sum + parseInt(el);
    })
    return sum
}

document.querySelector('.add').addEventListener('click' , function (e) {
    const adder = document.querySelector('.addinput').value 
    const meow = adder.split(',')
    let result = add(...meow)
    if (isNaN(result)) {
        result = 'ENTER A NUMBER'
    }
    document.querySelector('.result').textContent = result
})

let times =(...params) => {
    let multiply = 1;
    params.forEach(el =>{
        multiply = multiply * el;
    })
    return multiply
}

document.querySelector('.multiply').addEventListener('click' , function (e) {
    const multiplier = document.querySelector('.multiplyinput').value 
    const moo = multiplier.split(',')
    let product = times(...moo)
    if (isNaN(product)) {
        product = 'ENTER A NUMBER'
    }
    document.querySelector('.product').textContent = product
})

let subtract =(...params) => {
    let diff = 0;
   diff = params[0] - params[1]
    return diff
}

document.querySelector('.subtract').addEventListener('click' , function (e) {
    const subtracter = document.querySelector('.subtractinput').value 
    const woof = subtracter.split(',')
    let diff = subtract(...woof)
    if (isNaN(diff)) {
        diff = 'ENTER A NUMBER'
    }
    document.querySelector('.difference').textContent = diff
})

let divide =(...params) => {
    let quotient = 0;
    quotient = params[0] / params[1]
    return quotient
}

document.querySelector('.divide').addEventListener('click' , function (e) {
    const divider = document.querySelector('.divideinput').value 
    const moo2 = divider.split(',')
    let quotient = divide(...moo2)
    if (isNaN(quotient)) {
        quotient = 'ENTER A NUMBER'
    }
    document.querySelector('.quotient').textContent = quotient
})
Posted by: Guest on June-27-2020

Code answers related to "basic calculator using javascript"

Browse Popular Code Answers by Language