Week 7 Section 6

.map() vs .every() Objects, JSON localStorage debbugger;

by Larry Adams

HTML

<h3>Array.prototype.every() vs Array.prototype.map()</h3>
<ul>
    <li>.map() <strong>returns a new Array</strong> of objects created by taking some action on the original item.</li>
    <li>.every() <strong>returns a boolean</strong> - true if every element in this array satisfies the provided testing function. An important difference with .every() is that the test function may not always be called for every element in the array. Once the testing function returns false for any element, no more array elements are iterated. Therefore, the testing function should usually have no side effects.</li>
</ul>

<h3>Objects</h3>
<ul>
    <li><strong>key: </strong>value</li>
    <li>JSON</li>
    <li>localStorage</li>
</ul>
<h3>debugger;</h3>
<h3>function prototypes</h3>

JavaScript

"use strict";
//debugger;
var numbers = [1, 2, -3, 4, 5, 6, 7, 8, 9];

// anything less than 5 will be set to 5
for(var i = 0; i < numbers.length; i++){
    if(numbers[i] < 5){
        numbers[i] = 5;
    }
}
//console.log(numbers);

var newNumbers = numbers.map(function(value, index, arr){
    if(value < 5){
        return 5;
    }
    return value;
});
//console.log(newNumbers);

var arePositive = numbers.every(function(value, index, arr){
    if(value >= 0){
        return true;
    }
    return false;
});
console.log(arePositive);

var person = {
    fname: "JaZahn", 
    lname: "Clevenger",
    email: "[email protected]"
}
//console.log(person);
person.phone = "6175551234";
//console.log(person);

for(var key in person){
    var value = person[key];
    //console.log("<strong>" + key + ": </strong>" + value);
}

//localStorage.setItem("person", JSON.stringify(person));
var newperson = localStorage.getItem("person");
//console.log(newperson); // that's a string
newperson = JSON.parse(newperson);
//console.log(newperson); // that's an object