Skip to content

Commit

Permalink
some major changes
Browse files Browse the repository at this point in the history
calculator position absolute and height 100% in css.
in script.js file I've commented my previous tries to fixing the calculation issue.
changed version code to v5.0.0
  • Loading branch information
sr-tamim committed Mar 7, 2022
1 parent 1517b21 commit 61cf1c1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!-- Copyright 2022 | All rights reserved by SR TAMIM -->
<!DOCTYPE html>
<html lang="en">
<!-- calculator-v4.0.0 -->
<!-- calculator-v5.0.0 -->

<head>
<meta charset="UTF-8">
Expand Down
49 changes: 47 additions & 2 deletions script/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ function equalFunc() {
let result = 0;

/* --- This part has been commented just because javascript is not so good at math
javascript calculates 0.1 + 0.2 and returns 0.30000000000000004
to solve this javascript calculation problem I've used mathjs library --- */
javascript calculates 0.1 + 0.2 and returns 0.30000000000000004 --- */
/*
switch(operator){
case '+':
Expand All @@ -269,6 +268,52 @@ function equalFunc() {
}
*/

// I again tried to solve the issue natively in JavaScript
/*
if (operator === '+' || operator === '-') {
const [value1, value2] = [firstValue.toString(), secondValue.toString()]
if (value1.includes('.') || value2.includes('.')) {
const splittedValues = [value1.split('.'), value2.split('.')]
const nonFractionalParts = splittedValues.map(value => Number(value[0]));
const nonFractionalResult = operator === '+' ?
nonFractionalParts.reduce((a, b) => a + b, 0)
: nonFractionalParts[0] - nonFractionalParts[1];
const fractionalParts = splittedValues.map(value => value[1] ? value[1] : 0);
const largestFractionLength = Math.max(...fractionalParts.map(x => x.toString().length));
const fractionalResult =
fractionalParts.reduce((a, b) => Math.abs(a) + (operator === '+' ? 1 : -1) * (b.length < largestFractionLength ?
(b * Math.pow(10, largestFractionLength - b.length))
: Number(b)), 0)
result = nonFractionalResult + (fractionalResult / Math.pow(10, largestFractionLength));
} else {
result = operator === '+' ? firstValue + secondValue :
firstValue - secondValue
}
}
else if (operator === '*') {
const [value1, value2] = [firstValue.toString(), secondValue.toString()];
if (value1.includes('.') || value2.includes('.')) {
let fractionalPartLength = (value1.includes('.') && value2.includes('.')) ?
(value1.split('.')[1].length > value2.split('.')[1].length) ?
value1.split('.')[1].length : value2.split('.')[1].length
: value1.includes('.') ? value1.split('.')[1].length
: value2.split('.')[1].length;
result = ((firstValue * Math.pow(10, fractionalPartLength)) *
(secondValue * Math.pow(10, fractionalPartLength))) /
Math.pow(10, fractionalPartLength + fractionalPartLength);
} else {
result = firstValue * secondValue;
}
} else if (operator === '/') {
result = firstValue / secondValue;
}
*/

// at last I use mathjs library to fix the calculation issue
switch (operator) {
case '+':
result = math.number(math.add(math.fraction(firstValue), math.fraction(secondValue)));
Expand Down
5 changes: 4 additions & 1 deletion style/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ body{
margin: 0;
}
.container{
position: fixed;
top: 0;
left: 0;
width: 100%;
min-height: 100vh;
min-height: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
Expand Down
2 changes: 1 addition & 1 deletion sw.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// service worker file
const staticCacheName = 'calculator-v4.0.0';
const staticCacheName = 'calculator-v5.0.0';
const assets = [
'/smart-calculator/',
'/smart-calculator/index.html',
Expand Down

0 comments on commit 61cf1c1

Please sign in to comment.