Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help with JSON and JavaScript...I have the other files but I only need help on t

ID: 3861879 • Letter: H

Question

Help with JSON and JavaScript...I have the other files but I only need help on this. Let me know if you want to see the rest.

1. Add code to the getContacts method that creates a function named reviver. Code the reviver method so it adds dashes back to regular phone numbers (7 characters), phone numbers with area codes (10 characters), and phone numbers with 1 + area code (11 characters). Then, use the reviver function with the parse method.

2. add code to the setContacts method that creates a function named replacer. Code the replacer function so it strips all non-numeric characters from the phone number value. Then, use the replacer function with the stringify method.

"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
return JSON.parse(storageString) || [];
},
setContacts: function(value) {
// convert JavaScript object to string
var storageString = JSON.stringify(value);
  
// store string in local storage
localStorage.setItem(this.keyContacts, storageString);
},
clearContacts: function() {
localStorage.setItem(this.keyContacts, "");
}
};

Explanation / Answer

"use strict";

var storage = {
   keyContacts: "contacts_1",
   getContacts: function() {     
       // get string from local storage
        var storageString = localStorage.getItem(this.keyContacts) || null;
      
       var reviver = function b(storageString) {
            if (storageString == null || storageString == undefined) {
                 return null;
            }

                var formattedNumber;

            if (storageString.length == 7) {
                formattedNumber = storageString.substr(0,3) + '-' + storageString.substr(3,4);    
            }
            else if (storageString.length == 10) {
                formattedNumber = storageString.substr(0,3) + '-' + storageString.substr(3,3) + '-' + storageString.substr(6,4);  
             }
                else if (storageString.length == 11) {
                   formattedNumber = storageString.substr(0,1) + '-' + storageString.substr(1,3) + '-' + storageString.substr(4, 3) + '-' + storageString.substr(7, 4);
                }
                return formattedNumber;
       }

       var formattedNumber = reviver(storageString);

           // convert string to JavaScript object and return, or return empty array if string is null
       return JSON.parse(formattedNumber) || [];
    },
    setContacts: function(value) {
       // convert JavaScript object to string
       var storageString = JSON.stringify(value);

       var replacer = function b(storageString) {
           if (storageString == null || storageString == undefined) {
                 return null;
           }
           return storageString.replace(/D/g,'');                          
       }
      
       var formattedNumber = replacer(storageString);

       // store string in local storage
       localStorage.setItem(this.keyContacts, storageString);
    },
    clearContacts: function() {
       localStorage.setItem(this.keyContacts, "");
    }
};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote