Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 39 additions & 41 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Calculator</title>
<link rel="stylesheet" href="style.css" />
</head>

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
<link rel="stylesheet" href="style.css">
</head>

<body>
<div class="calculator">
<div class="display">&nbsp;</div>
<div class="buttons">
<div class="row">
<button class="number" id="number-7">7</button>
<button class="number" id="number-8">8</button>
<button class="number" id="number-9">9</button>
</div>
<div class="row">
<button class="number" id="number-4">4</button>
<button class="number" id="number-5">5</button>
<button class="number" id="number-6">6</button>
</div>
<div class="row">
<button class="number" id="number-1">1</button>
<button class="number" id="number-2">2</button>
<button class="number" id="number-3">3</button>
</div>
<div class="row operations">
<!-- <button id="add">+</button>
<button id="subtract">-</button>
<button id="multiply">*</button>
<button id="divide">/</button> -->
</div>
<div class="row last-row">
<button id="clear">C</button>
<button class="number" id="number-0">0</button>
<button id="equal">=</button>
<body>
<div class="calculator">
<div class="display">&nbsp;</div>
<div class="buttons">
<div class="row">
<button class="number" id="number-7">7</button>
<button class="number" id="number-8">8</button>
<button class="number" id="number-9">9</button>
</div>
<div class="row">
<button class="number" id="number-4">4</button>
<button class="number" id="number-5">5</button>
<button class="number" id="number-6">6</button>
</div>
<div class="row">
<button class="number" id="number-1">1</button>
<button class="number" id="number-2">2</button>
<button class="number" id="number-3">3</button>
</div>
<div class="row operations">
<button id="add">+</button>
<button id="subtract">-</button>
<button id="multiply">*</button>
<button id="divide">/</button>
</div>
<div class="row last-row">
<button id="clear">C</button>
<button class="number" id="number-0">0</button>
<button id="equal">=</button>
</div>
</div>
</div>
</div>
<script src="script.js"></script>
</body>

</html>
<script src="script.js"></script>
</body>
</html>
77 changes: 77 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* ****************************************************
* DevClub excercise 2
* CALCULATOR
/* ****************************************************
* This file implements the logic of a caluclator
* that can do 4 basic operations: (+), (-), (*), (/)
/* **************************************************** */

let operand;
let operator;
let result;

const display = document.querySelector('.display');
const numbers = document.querySelectorAll('.number');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good practice to get all of the numbers at once

const operators = document.querySelectorAll('.row.operations button');

numbers.forEach((numButton) => {
numButton.addEventListener('click', () => {
if (display.textContent === '0') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 should be saved inside constant variable

display.textContent = '';
}

operand += numButton.textContent;

display.textContent += numButton.textContent;
});
});

operators.forEach((button) => {
button.addEventListener('click', () => {
calc();

operator = button.textContent;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a null check on the operator

display.textContent = result + operator;
});
});

clear.addEventListener('click', handleClear);

equal.addEventListener('click', () => {
calc();
display.textContent = result;
operand = result;
result = 0;
});

handleClear();

function calc() {
switch (operator) {
case '+':
result = result + parseInt(operand);
break;
case '-':
result = result - parseInt(operand);
break;
case '*':
result = result * parseInt(operand);
break;
case '/':
result = result / parseInt(operand);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will you see in case of number/0?

break;
default:
result = parseInt(operand);
}

operand = '';
operator = '';
}

function handleClear() {
operator = '';
operand = '';
result = 0;

display.textContent = '0';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you would removed the above 0 into constant you could reuse it in here

}