let operators = [ '+', '-', '/', '*', '.' ]; let operation = ""; let operations = []; if ( localStorage.getItem( "operations" ) ) { operations = JSON.parse( localStorage.getItem( "operations" ) ); setTimeout( () => insertHistory(), 10 ); } function calculate() { let $display = $( ".display" ); try { let result = eval( operation ); operations.unshift( { calcul: operation, result, date: new Date().getTime() } ); $display.text( result ); operation = result.toString(); setFontSize( $display ); localStorage.setItem( "operations", JSON.stringify( operations ) ); insertHistory(); } catch (e) { } } function setFontSize( $display ) { let length = operation.length; let clearsButtons = $( ".clears-buttons-container" ); if ( length > 0 ) { clearsButtons.addClass( "show" ); } else { clearsButtons.removeClass( "show" ); } if ( length <= 10 ) { $display.removeClass( "big" ); $display.removeClass( "medium" ); $display.removeClass( "small" ); } else if ( length <= 12 ) { $display.addClass( "big" ); $display.removeClass( "medium" ); $display.removeClass( "small" ); } else if ( length <= 14 ) { $display.addClass( "medium" ); $display.removeClass( "big" ); $display.removeClass( "small" ); } else if ( length <= 16 ) { $display.addClass( "small" ); $display.removeClass( "big" ); $display.removeClass( "medium" ); } } function roundDate( date ) { date.setHours(0); date.setMinutes(0); date.setSeconds(0); date.setMilliseconds(0); return date; } function diff2Dates( date1, date2 ) { let one_day = 1000 * 60 * 60 * 24; let date1_ms = date1.getTime(); let date2_ms = date2.getTime(); let difference_ms = date2_ms - date1_ms; return Math.round( difference_ms / one_day ); } function insertHistory() { let $html = $( "
" ); let now = roundDate( new Date() ); let historyContainer = $( ".history-container" ); if ( operations.length > 0 ) { for ( let operation of operations ) { let date = roundDate( new Date( operation.date ) ); if ( $html.find( `.day-container[data-day="${ date.toDateString() }"]` ).length > 0 ) { $html.find( `.day-container[data-day="${ date.toDateString() }"] .title` ).after(`