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

Need help with JSON and JavaScript. 1.add code to the setContacts method that cr

ID: 3863018 • Letter: N

Question

Need help with JSON and JavaScript.

1.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;

//my code   
var reviver = function (key, value) {
    if (key === "") return value;
  
if (key === "p") {
  
switch (value.length) {
   case "7":
       return value.subString (0,3) + "-" + subString (3);
   case "10":
       return value.subString (0,3) + "-" + subString (3,3) + "-" + subString (6);
   case "11":
       return value.subString (0,1) + "-" + subString (1,3) + "-" + subString (4,3) + "-" + subString (7);
       default:
       return value;
  
}
else {
return value;
}
}
};
// 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);

//my code   
var replacer = function (key, value) {
  
}
  
  
// store string in local storage
localStorage.setItem(this.keyContacts, storageString);
},
clearContacts: function() {
localStorage.setItem(this.keyContacts, "");
}
};

Explanation / Answer

// Parse the text. Values that look like ISO date strings will // be converted to Date objects. myData = json_parse(text, function (key, value) { var a; if (typeof value === 'string') { a = /^(d{4})-(d{2})-(d{2})T(d{2}):(d{2}):(d{2}(?:.d*)?)Z$/.exec(value); if (a) { return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6])); } } return value; }); /*members "", """, "/", "\", at, b, call, charAt, f, fromCharCode, hasOwnProperty, message, n, name, push, r, t, text */ var json_parse = (function () { // This is a function that can parse a JSON text, producing a JavaScript // data structure. It is a simple, recursive descent parser. It does not use // eval or regular expressions, so it can be used as a model for implementing // a JSON parser in other languages. // We are defining the function inside of another function to avoid creating // global variables. var at, // The index of the current character ch, // The current character escapee = { '"': '"', '\': '\', '/': '/', b: '', f: ' ', n: ' ', r: ' ', t: ' ' }, text, error = function (m) { // Call error when something is wrong. throw { name: 'SyntaxError', message: m, at: at, text: text }; }, next = function (c) { // If a c parameter is provided, verify that it matches the current character. if (c && c !== ch) { error("Expected '" + c + "' instead of '" + ch + "'"); } // Get the next character. When there are no more characters, // return the empty string. ch = text.charAt(at); at += 1; return ch; }, number = function () { // Parse a number value. var number, string = ''; if (ch === '-') { string = '-'; next('-'); } while (ch >= '0' && ch = '0' && ch = '0' && ch
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