how to add a localstorage for my Calculator?

家住魔仙堡 提交于 2021-01-29 10:33:55

问题


I have to work on a calculator. I got everything i need. The only thing is, I have to add a LocalStorage under my Result. So the Calculator adds everytime the result, untill i delete the session.

For example:

"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("Es wurden keine Zahlen eingegeben!");
        clear();
        clearresult();
    }
}

回答1:


*store your result in array and store that array in localstorage as mentioned below

let itemsArray = []
localStorage.setItem('items', JSON.stringify(itemsArray))
const data = JSON.parse(localStorage.getItem('items'))

putting new result in array

itemsArray.push(input.value)
localStorage.setItem('items', JSON.stringify(itemsArray))  

remove array from local storage

localStorage.clear()



回答2:


I think the solution below will go a long way into helping you out - The use of default variables may make code verbose, remove at wish.

// saves the result and update DOM with it accordingly
const saveResult = function (result, storageName = 'resultstorage') {
  let store = []
  store.push(result)
  localStorage.setItem(storageName, JSON.stringify(store))
  // update DOM with value
  getResult()
}

 // using try-catch statement ensures we did not get an error
 // if - say no value is in the localStorage
 // And it can enable us to retrieve the last result in memory on page-load
 // if peradventure we exited the browser without removing the item from localStorage
 // i.e we can call getResult() without error

const getResult = function (storageName = 'resultstorage') {
  let result;
  try {
    result = JSON.parse(localStorage.getItem(storageName))
    document.querySelector('#output').innerHTML = result
  } catch (err) {}
}

// we use **remove** not **clear** and the storage name
// to avoid removing other values unintended 

const removeResult = (storageName = 'resultstorage') => localStorage.removeItem(storageName)

// usage
let value = 23
saveResult(value)


来源:https://stackoverflow.com/questions/58446887/how-to-add-a-localstorage-for-my-calculator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!