Setting a Digit Limit on a Javascript Calculator
I'm trying to add functionality to my calculator that limits the amount of digits you can enter on the screen at one time. (In this case, 8, to prevent the calculator from expanding and looking ugly). I'm doing this by using 'if' statements to check if the variable representing the digits on the screen has a length longer or equal to 8, and then attempting to clear the screen if it does. At the moment, the calculator clears itself after attempting to enter two digits on the screen. Something tells me there is some integer/string conflict preventing me from achieving this task. What other solutions are there to this?
Thank you
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>My Calculator</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="flex-container">
<div class="calculator">
<div id="screen">
<div id="displayedVal">0</div>
</div>
<div id="button-set">
<div>
<button class="numberCalc" id="seven">7</button>
<button class="numberCalc" id="eight">8</button>
<button class="numberCalc" id="nine">9</button>
<button class="operatorCalc" id="divide">÷</button>
</div>
<div>
<button class="numberCalc" id="four">4</button>
<button class="numberCalc" id="five">5</button>
<button class="numberCalc" id="six">6</button>
<button class="operatorCalc" id="multiply">×</button>
</div>
<div>
<button class="numberCalc" id="one">1</button>
<button class="numberCalc" id="two">2</button>
<button class="numberCalc" id="three">3</button>
<button class="operatorCalc" id="minus">-</button>
</div>
<div>
<button class="numberCalc" id="zero">0</button>
<button class="operatorCalc" id="point">.</button>
<button class="operatorCalc" id="plus">+</button>
<button class="operatorCalc" id="equals">=</button>
</div>
</div>
</div>
</div>
<style>
.flex-container {
display: flex;
height: 610px;
justify-content: center;
align-items: center;
}
.calculator {
background-color: #28283e;
padding: 30px;
border-radius: 20px;
box-shadow: 2px 0 2px 1px;
height: 350px;
}
#screen {
background-color: #ffffff;
height: 70px;
border-radius: 5px;
margin-top: -12px;
}
#displayedVal {
font-size: 45px;
text-align: right;
margin-right: 5px;
}
#button-set {
margin-top: 16px;
margin-bottom: -20px;
}
button {
margin: 10px 5px;
width: 40px;
height: 40px;
border-radius: 20px;
}
</style>
<script>
// grabbing the digit on the screen
var displayedNum = document.getElementById("displayedVal");
// grabbing the calculator buttons
var seven = document.getElementById("seven");
var eight = document.getElementById("eight");
var nine = document.getElementById("nine");
var divide = document.getElementById("divide");
var four = document.getElementById("four");
var five = document.getElementById("five");
var six = document.getElementById("six");
var multiply = document.getElementById("multiply");
var one = document.getElementById("one");
var two = document.getElementById("two");
var three = document.getElementById("three");
var minus = document.getElementById("minus");
var zero = document.getElementById("zero");
var point = document.getElementById("point");
var plus = document.getElementById("plus");
var equals = document.getElementById("equals");
var defaultVal = "0";
var pendingVal;
evalStringArray = ;
// separating the number buttons from the operator buttons
var numberCalc = document.getElementsByClassName("numberCalc");
var operatorCalc = document.getElementsByClassName("operatorCalc");
// when a number button is clicked, the screen will display the number corresponding to that button
var updateDisplayValue = (clickObj) => {
var valOutput = clickObj.target.innerText;
if (defaultVal === "0")
defaultVal = "";
// currentVal = "" + whichever number is pressed (string format)
currentVal = defaultVal + valOutput;
// what displays on the screen
displayedVal.innerText = currentVal;
var numOfDigits = displayedVal.innerText.length();
if (numOfDigits.length >= "8")
clearDigits();
}
function clearDigits() {
displayedVal.innerText.clear();
}
var activateOperator = (clickObj) => {
operatorOutput = clickObj.target.innerText;
switch(operatorOutput) {
case divide:
displayedVal.innerText = valOutput / valOutput
break;
case multiply:
displayedVal.innerText = valOutput * valOutput
break;
case minus:
displayedVal.innerText = valOutput - valOutput
break;
case point:
break;
case plus:
displayedVal.innerText = valOutput + valOutput
break;
case equals:
break;
default:
}
}
// iterating through each number
for (let i = 0; i < numberCalc.length; i++) {
numberCalc[i].addEventListener("click", updateDisplayValue);
}
// iterating through each operator
for (let i = 0; i < operatorCalc.length; i++) {
operatorCalc[i].addEventListener("click", activateOperator);
}
</script>
</body>
</html>
javascript calculator
add a comment |
I'm trying to add functionality to my calculator that limits the amount of digits you can enter on the screen at one time. (In this case, 8, to prevent the calculator from expanding and looking ugly). I'm doing this by using 'if' statements to check if the variable representing the digits on the screen has a length longer or equal to 8, and then attempting to clear the screen if it does. At the moment, the calculator clears itself after attempting to enter two digits on the screen. Something tells me there is some integer/string conflict preventing me from achieving this task. What other solutions are there to this?
Thank you
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>My Calculator</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="flex-container">
<div class="calculator">
<div id="screen">
<div id="displayedVal">0</div>
</div>
<div id="button-set">
<div>
<button class="numberCalc" id="seven">7</button>
<button class="numberCalc" id="eight">8</button>
<button class="numberCalc" id="nine">9</button>
<button class="operatorCalc" id="divide">÷</button>
</div>
<div>
<button class="numberCalc" id="four">4</button>
<button class="numberCalc" id="five">5</button>
<button class="numberCalc" id="six">6</button>
<button class="operatorCalc" id="multiply">×</button>
</div>
<div>
<button class="numberCalc" id="one">1</button>
<button class="numberCalc" id="two">2</button>
<button class="numberCalc" id="three">3</button>
<button class="operatorCalc" id="minus">-</button>
</div>
<div>
<button class="numberCalc" id="zero">0</button>
<button class="operatorCalc" id="point">.</button>
<button class="operatorCalc" id="plus">+</button>
<button class="operatorCalc" id="equals">=</button>
</div>
</div>
</div>
</div>
<style>
.flex-container {
display: flex;
height: 610px;
justify-content: center;
align-items: center;
}
.calculator {
background-color: #28283e;
padding: 30px;
border-radius: 20px;
box-shadow: 2px 0 2px 1px;
height: 350px;
}
#screen {
background-color: #ffffff;
height: 70px;
border-radius: 5px;
margin-top: -12px;
}
#displayedVal {
font-size: 45px;
text-align: right;
margin-right: 5px;
}
#button-set {
margin-top: 16px;
margin-bottom: -20px;
}
button {
margin: 10px 5px;
width: 40px;
height: 40px;
border-radius: 20px;
}
</style>
<script>
// grabbing the digit on the screen
var displayedNum = document.getElementById("displayedVal");
// grabbing the calculator buttons
var seven = document.getElementById("seven");
var eight = document.getElementById("eight");
var nine = document.getElementById("nine");
var divide = document.getElementById("divide");
var four = document.getElementById("four");
var five = document.getElementById("five");
var six = document.getElementById("six");
var multiply = document.getElementById("multiply");
var one = document.getElementById("one");
var two = document.getElementById("two");
var three = document.getElementById("three");
var minus = document.getElementById("minus");
var zero = document.getElementById("zero");
var point = document.getElementById("point");
var plus = document.getElementById("plus");
var equals = document.getElementById("equals");
var defaultVal = "0";
var pendingVal;
evalStringArray = ;
// separating the number buttons from the operator buttons
var numberCalc = document.getElementsByClassName("numberCalc");
var operatorCalc = document.getElementsByClassName("operatorCalc");
// when a number button is clicked, the screen will display the number corresponding to that button
var updateDisplayValue = (clickObj) => {
var valOutput = clickObj.target.innerText;
if (defaultVal === "0")
defaultVal = "";
// currentVal = "" + whichever number is pressed (string format)
currentVal = defaultVal + valOutput;
// what displays on the screen
displayedVal.innerText = currentVal;
var numOfDigits = displayedVal.innerText.length();
if (numOfDigits.length >= "8")
clearDigits();
}
function clearDigits() {
displayedVal.innerText.clear();
}
var activateOperator = (clickObj) => {
operatorOutput = clickObj.target.innerText;
switch(operatorOutput) {
case divide:
displayedVal.innerText = valOutput / valOutput
break;
case multiply:
displayedVal.innerText = valOutput * valOutput
break;
case minus:
displayedVal.innerText = valOutput - valOutput
break;
case point:
break;
case plus:
displayedVal.innerText = valOutput + valOutput
break;
case equals:
break;
default:
}
}
// iterating through each number
for (let i = 0; i < numberCalc.length; i++) {
numberCalc[i].addEventListener("click", updateDisplayValue);
}
// iterating through each operator
for (let i = 0; i < operatorCalc.length; i++) {
operatorCalc[i].addEventListener("click", activateOperator);
}
</script>
</body>
</html>
javascript calculator
Actually, at the moment, the calculator crashes when you click any number.
– Scott Marcus
Nov 24 '18 at 16:04
Hmmm... This is strange. I've checked mine and it lets you enter any number and that number changes if you enter another number. I've given out the full code so I'm not sure what's going on here.
– Harrison
Nov 24 '18 at 16:15
You have a number of problems, most of which can be seen in the javascript console. For starters,.length
is a property, not a function, so no()
on it.
– Jim B.
Nov 24 '18 at 16:49
I can't see any of these errors on my console. Ah of course, didn't notice that, thanks!
– Harrison
Nov 24 '18 at 17:06
Would a 'for loop' possibly be a better solution instead of an 'if' statement?
– Harrison
Nov 24 '18 at 17:21
add a comment |
I'm trying to add functionality to my calculator that limits the amount of digits you can enter on the screen at one time. (In this case, 8, to prevent the calculator from expanding and looking ugly). I'm doing this by using 'if' statements to check if the variable representing the digits on the screen has a length longer or equal to 8, and then attempting to clear the screen if it does. At the moment, the calculator clears itself after attempting to enter two digits on the screen. Something tells me there is some integer/string conflict preventing me from achieving this task. What other solutions are there to this?
Thank you
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>My Calculator</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="flex-container">
<div class="calculator">
<div id="screen">
<div id="displayedVal">0</div>
</div>
<div id="button-set">
<div>
<button class="numberCalc" id="seven">7</button>
<button class="numberCalc" id="eight">8</button>
<button class="numberCalc" id="nine">9</button>
<button class="operatorCalc" id="divide">÷</button>
</div>
<div>
<button class="numberCalc" id="four">4</button>
<button class="numberCalc" id="five">5</button>
<button class="numberCalc" id="six">6</button>
<button class="operatorCalc" id="multiply">×</button>
</div>
<div>
<button class="numberCalc" id="one">1</button>
<button class="numberCalc" id="two">2</button>
<button class="numberCalc" id="three">3</button>
<button class="operatorCalc" id="minus">-</button>
</div>
<div>
<button class="numberCalc" id="zero">0</button>
<button class="operatorCalc" id="point">.</button>
<button class="operatorCalc" id="plus">+</button>
<button class="operatorCalc" id="equals">=</button>
</div>
</div>
</div>
</div>
<style>
.flex-container {
display: flex;
height: 610px;
justify-content: center;
align-items: center;
}
.calculator {
background-color: #28283e;
padding: 30px;
border-radius: 20px;
box-shadow: 2px 0 2px 1px;
height: 350px;
}
#screen {
background-color: #ffffff;
height: 70px;
border-radius: 5px;
margin-top: -12px;
}
#displayedVal {
font-size: 45px;
text-align: right;
margin-right: 5px;
}
#button-set {
margin-top: 16px;
margin-bottom: -20px;
}
button {
margin: 10px 5px;
width: 40px;
height: 40px;
border-radius: 20px;
}
</style>
<script>
// grabbing the digit on the screen
var displayedNum = document.getElementById("displayedVal");
// grabbing the calculator buttons
var seven = document.getElementById("seven");
var eight = document.getElementById("eight");
var nine = document.getElementById("nine");
var divide = document.getElementById("divide");
var four = document.getElementById("four");
var five = document.getElementById("five");
var six = document.getElementById("six");
var multiply = document.getElementById("multiply");
var one = document.getElementById("one");
var two = document.getElementById("two");
var three = document.getElementById("three");
var minus = document.getElementById("minus");
var zero = document.getElementById("zero");
var point = document.getElementById("point");
var plus = document.getElementById("plus");
var equals = document.getElementById("equals");
var defaultVal = "0";
var pendingVal;
evalStringArray = ;
// separating the number buttons from the operator buttons
var numberCalc = document.getElementsByClassName("numberCalc");
var operatorCalc = document.getElementsByClassName("operatorCalc");
// when a number button is clicked, the screen will display the number corresponding to that button
var updateDisplayValue = (clickObj) => {
var valOutput = clickObj.target.innerText;
if (defaultVal === "0")
defaultVal = "";
// currentVal = "" + whichever number is pressed (string format)
currentVal = defaultVal + valOutput;
// what displays on the screen
displayedVal.innerText = currentVal;
var numOfDigits = displayedVal.innerText.length();
if (numOfDigits.length >= "8")
clearDigits();
}
function clearDigits() {
displayedVal.innerText.clear();
}
var activateOperator = (clickObj) => {
operatorOutput = clickObj.target.innerText;
switch(operatorOutput) {
case divide:
displayedVal.innerText = valOutput / valOutput
break;
case multiply:
displayedVal.innerText = valOutput * valOutput
break;
case minus:
displayedVal.innerText = valOutput - valOutput
break;
case point:
break;
case plus:
displayedVal.innerText = valOutput + valOutput
break;
case equals:
break;
default:
}
}
// iterating through each number
for (let i = 0; i < numberCalc.length; i++) {
numberCalc[i].addEventListener("click", updateDisplayValue);
}
// iterating through each operator
for (let i = 0; i < operatorCalc.length; i++) {
operatorCalc[i].addEventListener("click", activateOperator);
}
</script>
</body>
</html>
javascript calculator
I'm trying to add functionality to my calculator that limits the amount of digits you can enter on the screen at one time. (In this case, 8, to prevent the calculator from expanding and looking ugly). I'm doing this by using 'if' statements to check if the variable representing the digits on the screen has a length longer or equal to 8, and then attempting to clear the screen if it does. At the moment, the calculator clears itself after attempting to enter two digits on the screen. Something tells me there is some integer/string conflict preventing me from achieving this task. What other solutions are there to this?
Thank you
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>My Calculator</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="flex-container">
<div class="calculator">
<div id="screen">
<div id="displayedVal">0</div>
</div>
<div id="button-set">
<div>
<button class="numberCalc" id="seven">7</button>
<button class="numberCalc" id="eight">8</button>
<button class="numberCalc" id="nine">9</button>
<button class="operatorCalc" id="divide">÷</button>
</div>
<div>
<button class="numberCalc" id="four">4</button>
<button class="numberCalc" id="five">5</button>
<button class="numberCalc" id="six">6</button>
<button class="operatorCalc" id="multiply">×</button>
</div>
<div>
<button class="numberCalc" id="one">1</button>
<button class="numberCalc" id="two">2</button>
<button class="numberCalc" id="three">3</button>
<button class="operatorCalc" id="minus">-</button>
</div>
<div>
<button class="numberCalc" id="zero">0</button>
<button class="operatorCalc" id="point">.</button>
<button class="operatorCalc" id="plus">+</button>
<button class="operatorCalc" id="equals">=</button>
</div>
</div>
</div>
</div>
<style>
.flex-container {
display: flex;
height: 610px;
justify-content: center;
align-items: center;
}
.calculator {
background-color: #28283e;
padding: 30px;
border-radius: 20px;
box-shadow: 2px 0 2px 1px;
height: 350px;
}
#screen {
background-color: #ffffff;
height: 70px;
border-radius: 5px;
margin-top: -12px;
}
#displayedVal {
font-size: 45px;
text-align: right;
margin-right: 5px;
}
#button-set {
margin-top: 16px;
margin-bottom: -20px;
}
button {
margin: 10px 5px;
width: 40px;
height: 40px;
border-radius: 20px;
}
</style>
<script>
// grabbing the digit on the screen
var displayedNum = document.getElementById("displayedVal");
// grabbing the calculator buttons
var seven = document.getElementById("seven");
var eight = document.getElementById("eight");
var nine = document.getElementById("nine");
var divide = document.getElementById("divide");
var four = document.getElementById("four");
var five = document.getElementById("five");
var six = document.getElementById("six");
var multiply = document.getElementById("multiply");
var one = document.getElementById("one");
var two = document.getElementById("two");
var three = document.getElementById("three");
var minus = document.getElementById("minus");
var zero = document.getElementById("zero");
var point = document.getElementById("point");
var plus = document.getElementById("plus");
var equals = document.getElementById("equals");
var defaultVal = "0";
var pendingVal;
evalStringArray = ;
// separating the number buttons from the operator buttons
var numberCalc = document.getElementsByClassName("numberCalc");
var operatorCalc = document.getElementsByClassName("operatorCalc");
// when a number button is clicked, the screen will display the number corresponding to that button
var updateDisplayValue = (clickObj) => {
var valOutput = clickObj.target.innerText;
if (defaultVal === "0")
defaultVal = "";
// currentVal = "" + whichever number is pressed (string format)
currentVal = defaultVal + valOutput;
// what displays on the screen
displayedVal.innerText = currentVal;
var numOfDigits = displayedVal.innerText.length();
if (numOfDigits.length >= "8")
clearDigits();
}
function clearDigits() {
displayedVal.innerText.clear();
}
var activateOperator = (clickObj) => {
operatorOutput = clickObj.target.innerText;
switch(operatorOutput) {
case divide:
displayedVal.innerText = valOutput / valOutput
break;
case multiply:
displayedVal.innerText = valOutput * valOutput
break;
case minus:
displayedVal.innerText = valOutput - valOutput
break;
case point:
break;
case plus:
displayedVal.innerText = valOutput + valOutput
break;
case equals:
break;
default:
}
}
// iterating through each number
for (let i = 0; i < numberCalc.length; i++) {
numberCalc[i].addEventListener("click", updateDisplayValue);
}
// iterating through each operator
for (let i = 0; i < operatorCalc.length; i++) {
operatorCalc[i].addEventListener("click", activateOperator);
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>My Calculator</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="flex-container">
<div class="calculator">
<div id="screen">
<div id="displayedVal">0</div>
</div>
<div id="button-set">
<div>
<button class="numberCalc" id="seven">7</button>
<button class="numberCalc" id="eight">8</button>
<button class="numberCalc" id="nine">9</button>
<button class="operatorCalc" id="divide">÷</button>
</div>
<div>
<button class="numberCalc" id="four">4</button>
<button class="numberCalc" id="five">5</button>
<button class="numberCalc" id="six">6</button>
<button class="operatorCalc" id="multiply">×</button>
</div>
<div>
<button class="numberCalc" id="one">1</button>
<button class="numberCalc" id="two">2</button>
<button class="numberCalc" id="three">3</button>
<button class="operatorCalc" id="minus">-</button>
</div>
<div>
<button class="numberCalc" id="zero">0</button>
<button class="operatorCalc" id="point">.</button>
<button class="operatorCalc" id="plus">+</button>
<button class="operatorCalc" id="equals">=</button>
</div>
</div>
</div>
</div>
<style>
.flex-container {
display: flex;
height: 610px;
justify-content: center;
align-items: center;
}
.calculator {
background-color: #28283e;
padding: 30px;
border-radius: 20px;
box-shadow: 2px 0 2px 1px;
height: 350px;
}
#screen {
background-color: #ffffff;
height: 70px;
border-radius: 5px;
margin-top: -12px;
}
#displayedVal {
font-size: 45px;
text-align: right;
margin-right: 5px;
}
#button-set {
margin-top: 16px;
margin-bottom: -20px;
}
button {
margin: 10px 5px;
width: 40px;
height: 40px;
border-radius: 20px;
}
</style>
<script>
// grabbing the digit on the screen
var displayedNum = document.getElementById("displayedVal");
// grabbing the calculator buttons
var seven = document.getElementById("seven");
var eight = document.getElementById("eight");
var nine = document.getElementById("nine");
var divide = document.getElementById("divide");
var four = document.getElementById("four");
var five = document.getElementById("five");
var six = document.getElementById("six");
var multiply = document.getElementById("multiply");
var one = document.getElementById("one");
var two = document.getElementById("two");
var three = document.getElementById("three");
var minus = document.getElementById("minus");
var zero = document.getElementById("zero");
var point = document.getElementById("point");
var plus = document.getElementById("plus");
var equals = document.getElementById("equals");
var defaultVal = "0";
var pendingVal;
evalStringArray = ;
// separating the number buttons from the operator buttons
var numberCalc = document.getElementsByClassName("numberCalc");
var operatorCalc = document.getElementsByClassName("operatorCalc");
// when a number button is clicked, the screen will display the number corresponding to that button
var updateDisplayValue = (clickObj) => {
var valOutput = clickObj.target.innerText;
if (defaultVal === "0")
defaultVal = "";
// currentVal = "" + whichever number is pressed (string format)
currentVal = defaultVal + valOutput;
// what displays on the screen
displayedVal.innerText = currentVal;
var numOfDigits = displayedVal.innerText.length();
if (numOfDigits.length >= "8")
clearDigits();
}
function clearDigits() {
displayedVal.innerText.clear();
}
var activateOperator = (clickObj) => {
operatorOutput = clickObj.target.innerText;
switch(operatorOutput) {
case divide:
displayedVal.innerText = valOutput / valOutput
break;
case multiply:
displayedVal.innerText = valOutput * valOutput
break;
case minus:
displayedVal.innerText = valOutput - valOutput
break;
case point:
break;
case plus:
displayedVal.innerText = valOutput + valOutput
break;
case equals:
break;
default:
}
}
// iterating through each number
for (let i = 0; i < numberCalc.length; i++) {
numberCalc[i].addEventListener("click", updateDisplayValue);
}
// iterating through each operator
for (let i = 0; i < operatorCalc.length; i++) {
operatorCalc[i].addEventListener("click", activateOperator);
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>My Calculator</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="flex-container">
<div class="calculator">
<div id="screen">
<div id="displayedVal">0</div>
</div>
<div id="button-set">
<div>
<button class="numberCalc" id="seven">7</button>
<button class="numberCalc" id="eight">8</button>
<button class="numberCalc" id="nine">9</button>
<button class="operatorCalc" id="divide">÷</button>
</div>
<div>
<button class="numberCalc" id="four">4</button>
<button class="numberCalc" id="five">5</button>
<button class="numberCalc" id="six">6</button>
<button class="operatorCalc" id="multiply">×</button>
</div>
<div>
<button class="numberCalc" id="one">1</button>
<button class="numberCalc" id="two">2</button>
<button class="numberCalc" id="three">3</button>
<button class="operatorCalc" id="minus">-</button>
</div>
<div>
<button class="numberCalc" id="zero">0</button>
<button class="operatorCalc" id="point">.</button>
<button class="operatorCalc" id="plus">+</button>
<button class="operatorCalc" id="equals">=</button>
</div>
</div>
</div>
</div>
<style>
.flex-container {
display: flex;
height: 610px;
justify-content: center;
align-items: center;
}
.calculator {
background-color: #28283e;
padding: 30px;
border-radius: 20px;
box-shadow: 2px 0 2px 1px;
height: 350px;
}
#screen {
background-color: #ffffff;
height: 70px;
border-radius: 5px;
margin-top: -12px;
}
#displayedVal {
font-size: 45px;
text-align: right;
margin-right: 5px;
}
#button-set {
margin-top: 16px;
margin-bottom: -20px;
}
button {
margin: 10px 5px;
width: 40px;
height: 40px;
border-radius: 20px;
}
</style>
<script>
// grabbing the digit on the screen
var displayedNum = document.getElementById("displayedVal");
// grabbing the calculator buttons
var seven = document.getElementById("seven");
var eight = document.getElementById("eight");
var nine = document.getElementById("nine");
var divide = document.getElementById("divide");
var four = document.getElementById("four");
var five = document.getElementById("five");
var six = document.getElementById("six");
var multiply = document.getElementById("multiply");
var one = document.getElementById("one");
var two = document.getElementById("two");
var three = document.getElementById("three");
var minus = document.getElementById("minus");
var zero = document.getElementById("zero");
var point = document.getElementById("point");
var plus = document.getElementById("plus");
var equals = document.getElementById("equals");
var defaultVal = "0";
var pendingVal;
evalStringArray = ;
// separating the number buttons from the operator buttons
var numberCalc = document.getElementsByClassName("numberCalc");
var operatorCalc = document.getElementsByClassName("operatorCalc");
// when a number button is clicked, the screen will display the number corresponding to that button
var updateDisplayValue = (clickObj) => {
var valOutput = clickObj.target.innerText;
if (defaultVal === "0")
defaultVal = "";
// currentVal = "" + whichever number is pressed (string format)
currentVal = defaultVal + valOutput;
// what displays on the screen
displayedVal.innerText = currentVal;
var numOfDigits = displayedVal.innerText.length();
if (numOfDigits.length >= "8")
clearDigits();
}
function clearDigits() {
displayedVal.innerText.clear();
}
var activateOperator = (clickObj) => {
operatorOutput = clickObj.target.innerText;
switch(operatorOutput) {
case divide:
displayedVal.innerText = valOutput / valOutput
break;
case multiply:
displayedVal.innerText = valOutput * valOutput
break;
case minus:
displayedVal.innerText = valOutput - valOutput
break;
case point:
break;
case plus:
displayedVal.innerText = valOutput + valOutput
break;
case equals:
break;
default:
}
}
// iterating through each number
for (let i = 0; i < numberCalc.length; i++) {
numberCalc[i].addEventListener("click", updateDisplayValue);
}
// iterating through each operator
for (let i = 0; i < operatorCalc.length; i++) {
operatorCalc[i].addEventListener("click", activateOperator);
}
</script>
</body>
</html>
javascript calculator
javascript calculator
edited Nov 24 '18 at 16:03
Scott Marcus
38.3k52036
38.3k52036
asked Nov 24 '18 at 16:01
HarrisonHarrison
12
12
Actually, at the moment, the calculator crashes when you click any number.
– Scott Marcus
Nov 24 '18 at 16:04
Hmmm... This is strange. I've checked mine and it lets you enter any number and that number changes if you enter another number. I've given out the full code so I'm not sure what's going on here.
– Harrison
Nov 24 '18 at 16:15
You have a number of problems, most of which can be seen in the javascript console. For starters,.length
is a property, not a function, so no()
on it.
– Jim B.
Nov 24 '18 at 16:49
I can't see any of these errors on my console. Ah of course, didn't notice that, thanks!
– Harrison
Nov 24 '18 at 17:06
Would a 'for loop' possibly be a better solution instead of an 'if' statement?
– Harrison
Nov 24 '18 at 17:21
add a comment |
Actually, at the moment, the calculator crashes when you click any number.
– Scott Marcus
Nov 24 '18 at 16:04
Hmmm... This is strange. I've checked mine and it lets you enter any number and that number changes if you enter another number. I've given out the full code so I'm not sure what's going on here.
– Harrison
Nov 24 '18 at 16:15
You have a number of problems, most of which can be seen in the javascript console. For starters,.length
is a property, not a function, so no()
on it.
– Jim B.
Nov 24 '18 at 16:49
I can't see any of these errors on my console. Ah of course, didn't notice that, thanks!
– Harrison
Nov 24 '18 at 17:06
Would a 'for loop' possibly be a better solution instead of an 'if' statement?
– Harrison
Nov 24 '18 at 17:21
Actually, at the moment, the calculator crashes when you click any number.
– Scott Marcus
Nov 24 '18 at 16:04
Actually, at the moment, the calculator crashes when you click any number.
– Scott Marcus
Nov 24 '18 at 16:04
Hmmm... This is strange. I've checked mine and it lets you enter any number and that number changes if you enter another number. I've given out the full code so I'm not sure what's going on here.
– Harrison
Nov 24 '18 at 16:15
Hmmm... This is strange. I've checked mine and it lets you enter any number and that number changes if you enter another number. I've given out the full code so I'm not sure what's going on here.
– Harrison
Nov 24 '18 at 16:15
You have a number of problems, most of which can be seen in the javascript console. For starters,
.length
is a property, not a function, so no ()
on it.– Jim B.
Nov 24 '18 at 16:49
You have a number of problems, most of which can be seen in the javascript console. For starters,
.length
is a property, not a function, so no ()
on it.– Jim B.
Nov 24 '18 at 16:49
I can't see any of these errors on my console. Ah of course, didn't notice that, thanks!
– Harrison
Nov 24 '18 at 17:06
I can't see any of these errors on my console. Ah of course, didn't notice that, thanks!
– Harrison
Nov 24 '18 at 17:06
Would a 'for loop' possibly be a better solution instead of an 'if' statement?
– Harrison
Nov 24 '18 at 17:21
Would a 'for loop' possibly be a better solution instead of an 'if' statement?
– Harrison
Nov 24 '18 at 17:21
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459925%2fsetting-a-digit-limit-on-a-javascript-calculator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459925%2fsetting-a-digit-limit-on-a-javascript-calculator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Actually, at the moment, the calculator crashes when you click any number.
– Scott Marcus
Nov 24 '18 at 16:04
Hmmm... This is strange. I've checked mine and it lets you enter any number and that number changes if you enter another number. I've given out the full code so I'm not sure what's going on here.
– Harrison
Nov 24 '18 at 16:15
You have a number of problems, most of which can be seen in the javascript console. For starters,
.length
is a property, not a function, so no()
on it.– Jim B.
Nov 24 '18 at 16:49
I can't see any of these errors on my console. Ah of course, didn't notice that, thanks!
– Harrison
Nov 24 '18 at 17:06
Would a 'for loop' possibly be a better solution instead of an 'if' statement?
– Harrison
Nov 24 '18 at 17:21