As I am a new programmer, it is very often that I find features of JavaScript that aren't any good, or I am not using them well.
I recently discovered a module for storing data called localStorage
and I thought of many ways to use it. One is to implement it into a code I have already created to make things easier for the user to use.
I thought of saving all of the data kinda like this.
const save = () => {
let saveInfo = [
els.scoreInEl.value, //input
els.maxInEl.value, //input
els.percentInEl.value, //input
els.percentInEl.textContent, //output
els.gradeEl.textContent, //output
els.scoreUp.textContent, //output
els.scoreDown.textContent, //output
els.percentOut.textContent //output
];
localStorage.setItem('savedInfo',saveInfo);
}
const removeSave = () => {localStorage.removeItem('savedInfo');};
window.onload = () => {
let savedInfo = localStorage.getItem('savedInfo')
if(savedInfo == null) {return} else {
savedInfo.split(',').forEach((x,i) => {
let currObject = Object.keys(els)[i];//els basically has all of the names in savedInfo this is just more for implementation
els[currObject].value = x'
}
}
}
But I ended up doing what is below, the difference is that in saveInfo
I don't have all of the outputs, instead, later I plug those inputs into the same equation that calculated the outputs.
I thought this would be less intensive on the computer as you are saving 1/3 of the information that is being stored. and although it probably doesn't matter on this level.
The reason why save ()
doesn't have any parameters is because I have all the information needed, and it is only needed when you click the save button.
This is the new code:
const save = () => {
let saveInfo = [
els.scoreInEl.value, //input
els.maxInEl.value, //input
els.percentInEl.value, //input
];
localStorage.setItem('savedInfo',saveInfo);
}
const removeSave = () => {localStorage.removeItem('savedInfo');};
window.onload = () => {
let savedInfo = localStorage.getItem('savedInfo')
if(savedInfo == null) {return} else {
savedInfo.split(',').forEach((x,i) => {
let currObject = Object.keys(els)[i];//basically has all of the names in savedInfo
els[currObject].value = x;
}
calc();//function I use to make those values, to begin with
}
}
I am wondering if this was a good way to handle the localStorage
along with any ways I can improve the way I am handling the storing and removing data, along with anything else that may be good to know.