I'm currently working on a calculator project and I have all the necessary resources at hand. The only thing left to do is to incorporate LocalStorage beneath the Result section. This way, the Calculator will keep track of each result until the session is cleared.
For instance:
"1+1=2," 2*2=4, 8/8=1
Localstorage: 2,4,1
function clear()
{
number1.value = "";
number2.value = "";
}
function clearresult()
{
result.innerText = '';
}
function calc()
{
var number1 = parseFloat(document.getElementById('number1').value);
var number2 = parseFloat(document.getElementById('number2').value);
var oper = document.getElementById('operators').value;
if ( !isNaN(number1) && !isNaN(number2) )
{
if ( oper === '+' )
{
result.innerText = document.getElementById('result').value = parseFloat(number1) + parseFloat(number2);
}
if ( oper === '-' )
{
result.innerText = document.getElementById('result').value = parseFloat(number1) - parseFloat(number2);
}
if ( oper === '/' && number1 )
{
result.innerText = document.getElementById('result').value = parseFloat(number1) / parseFloat(number2);
}
if ( oper === '*' )
{
result.innerText = document.getElementById('result').value = parseFloat(number1) * parseFloat(number2);
}
clear();
for (var i = 0; i < localStorage.length; i++)
{
localStorage.setItem('resultstorage', result.innerText);
output.innerText = localStorage.getItem('resultstorage');
}
}
else
{
alert("No numbers were entered!");
clear();
clearresult();
}
}