-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Chirayu31/main
feat: view previous results using local storage
- Loading branch information
Showing
2 changed files
with
55 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,50 @@ | ||
let string=""; | ||
let buttons=document.querySelectorAll('.button'); | ||
Array.from(buttons).forEach((button)=>{ | ||
button.addEventListener('click',(e)=>{ | ||
if(e.target.innerHTML=='='){ | ||
string=eval(string); | ||
document.querySelector('input').value=string; | ||
} | ||
else if(e.target.innerHTML=='C'){ | ||
string=""; | ||
document.querySelector('input').value=string; | ||
} | ||
let calculationString = ""; | ||
let calculationHistory = []; // To store the calculation history | ||
|
||
else if(e.target.innerHTML=='Bksp'){ | ||
string=string.slice(0,string.length-1); | ||
document.querySelector('input').value=string; | ||
} | ||
// Function to update the input field and store history | ||
function updateInputAndHistory() { | ||
document.querySelector('input').value = calculationString; | ||
localStorage.setItem('calculationHistory', JSON.stringify(calculationHistory)); | ||
updateHistoryList(); // Update the history list | ||
} | ||
|
||
// Load calculation history from local storage if it exists | ||
const savedCalculationHistory = localStorage.getItem('calculationHistory'); | ||
if (savedCalculationHistory) { | ||
calculationHistory = JSON.parse(savedCalculationHistory); | ||
updateHistoryList(); // Update the history list when the page loads | ||
} | ||
function updateHistoryList() { | ||
const historyList = document.getElementById('history-list'); | ||
historyList.innerHTML = ""; // Clear the existing list | ||
|
||
else{ | ||
console.log(e.target); | ||
string+=e.target.innerHTML; | ||
document.querySelector('input').value=string; | ||
} | ||
calculationHistory.forEach((calculation) => { | ||
const listItem = document.createElement('li'); | ||
listItem.textContent = calculation; | ||
historyList.appendChild(listItem); | ||
}); | ||
} | ||
|
||
}) | ||
}) | ||
let buttons = document.querySelectorAll('.button'); | ||
Array.from(buttons).forEach((button) => { | ||
button.addEventListener('click', (e) => { | ||
if (e.target.innerHTML == '=') { | ||
|
||
let result = eval(calculationString); | ||
|
||
calculationHistory.push(`${calculationString} = ${result}`); | ||
calculationString = result.toString(); | ||
|
||
updateInputAndHistory(); | ||
} else if (e.target.innerHTML == 'C') { | ||
calculationString = ""; | ||
updateInputAndHistory(); | ||
} else if (e.target.innerHTML == 'Bksp') { | ||
calculationString = calculationString.slice(0, calculationString.length - 1); | ||
updateInputAndHistory(); | ||
} else { | ||
calculationString += e.target.innerHTML; | ||
updateInputAndHistory(); | ||
} | ||
}); | ||
}); |