Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I just need Help with JSON JavaScript I have HTML and CSS figured out. So this is the JSON string thats stored in local storage:

I just need Help with JSON JavaScript I have HTML and CSS figured out.

So this is the JSON string thats stored in local storage:

[{"f":"Grace","l":"Hopper","o":"UNIVAC","p":"555-262-6500","e":""},

{"f":"Alan","l":"Turing","o":"","p":"","e":"alan@bletchleypark.uk"}]

1. In the library_storage.js file, add code to the getContacts method that converts a string to a JavaScript object and returns it, or returns an empty array if the string is null. As well as add code to the setContacts method that converts a JavaScript object to a string. As well as add code to the loadJsonObject method that uses the object with short names to populate the Contact objects properties.

library_storage.js file

"use strict";

var storage = { keyContacts: "contacts_1", getContacts: function() { // get string from local storage var storageString = localStorage.getItem(this.keyContacts) || null; // convert string to JavaScript object and return, or return empty array if string is null }, setContacts: function(value) { // convert JavaScript object to string // store string in local storage localStorage.setItem(this.keyContacts, storageString); }, clearContacts: function() { localStorage.setItem(this.keyContacts, ""); } };

2. In the library_contact.js file, review the code for the Contact constructor function and its methods. Then, add code to the toJSON method that shortens the property names (you can refer to the JSON string above to see what the short names should be).

library_contact.js file

"use strict";

var Contact = function(first, last, org, phone, email) { if (arguments.length > 0) { this.firstName = first; this.lastName = last; this.organization = org; this.phone = phone; this.email = email; } };

Contact.prototype.isValid = function() { // must have first and last name, and either phone number or email address var isValid = true; if (this.firstName === "" || this.lastName === "") { isValid = false; } if (this.phone === "" && this.email === "") { isValid = false; } return isValid; };

Contact.prototype.displayContact = function() { return this.firstName.concat(" ", this.lastName, ", ", this.organization, " Phone: ", this.phone, " Email: ", this.email, "


"); };

Contact.prototype.loadJsonObject = function(obj) { // use json object from storage to populate properties

};

Contact.prototype.toJSON = function() { // shorten property names for storage

};

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Management Systems

Authors: Patricia Ward, George A Dafoulas

1st Edition

ISBN: 1844804526, 978-1844804528

More Books

Students also viewed these Databases questions