Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const CACHE_KEY = "calculation_history";
- function checkForStorage() {
- return typeof(Storage) !== "undefined"
- }
- function putHistory(data) {
- if (checkForStorage()) {
- let historyData = null,
- if (localStorage.getItem(CACHE_KEY) === null) {
- historyData = [];
- } else {
- historyData = JSON.parse(localStorage.getItem(CACHE_KEY));
- }
- historyData.unshift(data);
- if (historyData.length > 5) {
- historyData.pop();
- }
- localStorage.setItem(CACHE_KEY, JSON.stringify(historyData));
- }
- }
- function showHistory() {
- if (checkForStorage()) {
- return JSON.parse(localStorage.getItem(CACHE_KEY)) || [];
- } else {
- return [];
- }
- }
- function renderHistory() {
- const historyData = showHistory();
- let historyList = document.querySelector("#historyList");
- // Hapus kontent HTML pada historylist biar tidak menampilkan data ganda
- historyList.innerHTML = "";
- for (let history of historyData) {
- let row = document.createElement('tr');
- row.innerHTML = "<td>" + history.firstNumber + "</td>";
- row.innerHTML += "<td>" + history.operator + "</td>";
- row.innerHTML += "<td>" + history.secondNumber + "</td>";
- row.innerHTML += "<td>" + history.result + "</td>";
- historyList.appendChild(row);
- }
- }
- renderHistory();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement