<p>There is one part to this practice problem. This exercise is designed to get you comfortable with using <code>window.localStorage</code>, the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Storage" target="_blank">Storage object</a>, and the utilities of the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify" target="_blank">JSON object</a>.</p>
<ol>
<li>On lines 11-12, the <code>personInfo</code> object is being converted into a string using JSON.stringify(), and assigned to localStorage. Your task is to do the opposite: retrieve the string from localStorage (Storage.getItem() may help), and output it to the page using the logMessage() function that's provided.
</li>
<p><b>Output:</b>
</p>
<div id="output"></div>
// Initialize our personInfo Object
var personInfo = {
fname: "Bill",
lname: "Adama",
addr: "Galactica CIC",
email: "N/A",
title: "Admiral",
}
// Write the object to localStorage
var jsonPerson = JSON.stringify(personInfo);
window.localStorage.setItem("person", jsonPerson);
var personInfo = localStorage.getItem('jsonPerson');
if (personInfo !== null) {
persistedItem = JSON.parse(personInfo);
console.log('A personInfo was found in LocalStorage.');
console.log(persistedItem);
//var name = personInfo.name;
// var quantity = personInfo.quantity;
//var comment = personInfo.comment;
// Insert your code below to read the object back from localStorage,
// convert it back to an object,
// and iterate over its properties, printing the property names and their
// values using the logMessage() function. This last part should be
// identical to code you wrote for the previous pratice set
// Utility function for logging convenience
// Logs msg to the element with given id
// If id is undefined, logs to #output
logMessage(personInfo.fname);//Bill
function logMessage(msg, id) {
if (!id) {
id = "output";
}
document.getElementById(id).innerHTML += msg + "<br>";
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.