2023-12-27 13:40:15 -07:00
const mainScreen = document . querySelector ( ".main-screen" ) ;
2023-12-27 13:52:37 -07:00
const topScreen = document . querySelector ( ".top-screen" ) ;
2023-12-26 20:44:05 -07:00
const buttons = document . querySelectorAll ( "button" ) ;
const operators = [ "+" , "-" , "*" , "/" ] ;
2023-12-26 20:11:44 -07:00
buttons . forEach ( ( button ) => {
button . addEventListener ( "click" , displayToScreen ) ;
} ) ;
2023-12-26 22:29:32 -07:00
2023-12-26 20:11:44 -07:00
function displayToScreen ( ) {
2023-12-26 20:12:58 -07:00
if ( this . className == "equal" ) {
2023-12-26 21:25:33 -07:00
let calc = new Calculator ;
2023-12-27 13:52:37 -07:00
topScreen . textContent += mainScreen . textContent ;
2023-12-26 21:25:33 -07:00
2023-12-27 13:52:37 -07:00
mainScreen . textContent = calc . calculate ( topScreen . textContent ) ;
2023-12-27 02:25:18 -07:00
} else if ( this . classList . contains ( "all" ) ) {
2023-12-27 13:40:15 -07:00
mainScreen . textContent = "0" ;
} else if ( this . classList . contains ( "error" ) && mainScreen . textContent !== "0" ) {
mainScreen . textContent = mainScreen . textContent . trimEnd ( )
mainScreen . textContent = mainScreen . textContent . slice ( 0 , - 1 ) ;
2023-12-27 00:55:32 -07:00
2023-12-27 13:40:15 -07:00
if ( ! isNaN ( mainScreen . textContent . charAt ( - 2 ) ) ) {
mainScreen . textContent = mainScreen . textContent . trimEnd ( )
2023-12-27 08:57:39 -07:00
}
2023-12-27 13:40:15 -07:00
if ( mainScreen . textContent === "" ) {
mainScreen . textContent = "0"
2023-12-27 08:57:39 -07:00
}
2023-12-26 20:11:44 -07:00
} else {
2023-12-26 20:44:05 -07:00
if ( this . className == "operator" ) {
2023-12-27 13:52:37 -07:00
let screenContent = topScreen . textContent ;
2023-12-26 22:29:32 -07:00
2023-12-27 00:55:32 -07:00
// Add some logic for handling negatives with -
// Add key support
2023-12-27 13:40:15 -07:00
// limit character size in mainScreen -- first step is to move numbers from the main mainScreen to somewhere else every time a new operator is used and then change what gets sent to the calculator to that new location
2023-12-27 00:55:32 -07:00
2023-12-26 20:44:05 -07:00
for ( let i = 0 ; i < screenContent . length ; i ++ ) {
if ( operators . includes ( screenContent [ i ] ) ) {
2023-12-26 21:45:12 -07:00
let calc = new Calculator ;
2023-12-27 13:52:37 -07:00
result = calc . calculate ( topScreen . textContent ) ;
2023-12-27 13:40:15 -07:00
mainScreen . textContent = result ;
2023-12-26 20:44:05 -07:00
break ;
}
2023-12-26 22:35:59 -07:00
}
2023-12-27 13:52:37 -07:00
topScreen . textContent += mainScreen . textContent + " " + this . textContent + " " ;
mainScreen . textContent = "0" ;
2023-12-26 22:29:32 -07:00
2023-12-27 13:40:15 -07:00
} else if ( mainScreen . textContent === "0" && this . className == "num" ) {
mainScreen . textContent = this . textContent ;
2023-12-27 08:57:39 -07:00
} else if ( this . className == "num" ) {
2023-12-27 13:40:15 -07:00
mainScreen . textContent += this . textContent ;
2023-12-26 21:25:33 -07:00
}
2023-12-26 20:11:44 -07:00
}
2023-12-26 21:25:33 -07:00
}
2023-12-27 01:51:17 -07:00
function addDecimals ( a , b ) {
a = String ( a ) ;
b = String ( b ) ;
const adec = ( a . split ( "." ) [ 1 ] || "" ) . length ;
const bdec = ( b . split ( "." ) [ 1 ] || "" ) . length ;
2023-12-27 02:16:30 -07:00
const maxDec = Math . max ( adec , bdec ) ;
2023-12-27 01:51:17 -07:00
let divdec = "1" ;
2023-12-27 02:16:30 -07:00
divdec = divdec . padEnd ( maxDec , "0" ) ;
return ( ( + a * divdec + + b * divdec ) / divdec ) . toFixed ( maxDec ) ;
}
function multiplyDecimals ( a , b ) {
a = String ( a ) ;
b = String ( b ) ;
const adec = ( a . split ( "." ) [ 1 ] || "" ) . length ;
const bdec = ( b . split ( "." ) [ 1 ] || "" ) . length ;
const totalDec = adec + bdec ;
return ( a * b ) . toFixed ( totalDec )
}
function divideDecimals ( a , b ) {
a = String ( a ) ;
b = String ( b ) ;
const adec = ( a . split ( "." ) [ 1 ] || "" ) . length ;
const bdec = ( b . split ( "." ) [ 1 ] || "" ) . length ;
const maxDec = Math . max ( adec , bdec ) ;
const divisor = Math . pow ( 10 , maxDec ) ;
return ( a * divisor ) / ( b * divisor ) ;
2023-12-27 01:51:17 -07:00
}
2023-12-26 21:25:33 -07:00
function Calculator ( str ) {
console . log ( "Calculator has been called" )
2023-12-26 17:15:28 -07:00
this . methods = {
2023-12-27 01:51:17 -07:00
"-" : ( a , b ) => addDecimals ( a , - b ) ,
"+" : ( a , b ) => addDecimals ( a , b ) ,
2023-12-27 02:16:30 -07:00
"/" : ( a , b ) => divideDecimals ( a , b ) ,
"*" : ( a , b ) => multiplyDecimals ( a , b )
2023-12-26 17:15:28 -07:00
} ;
this . calculate = function ( str ) {
let parts = str . split ( " " ) ;
a = + parts [ 0 ] ;
op = parts [ 1 ] ;
b = + parts [ 2 ] ;
2023-12-26 22:44:28 -07:00
2023-12-27 08:57:39 -07:00
if ( ( a || a == "0" ) && ! b ) {
2023-12-26 22:44:28 -07:00
return a ;
}
2023-12-26 17:15:28 -07:00
if ( isNaN ( a ) || isNaN ( b ) ) {
return NaN ;
}
if ( ! this . methods [ op ] ) {
return "Operation not supported."
}
return this . methods [ op ] ( a , b ) ;
} ;
}