RPeraltaJr
11/4/2019 - 4:27 PM

JavaScript Notes

/* 
| ------------------------------------------
| spread operator
| ------------------------------------------
*/
let parts = ['Shoulders', 'Knees']; 
let lyrics = ['Head', ...parts, 'and', 'Toes']; // insert 'parts' array into 'lyrics' array
console.log(lyrics); // ["Head", "Shoulders", "Knees", "and", "Toes"]

/* 
| ------------------------------------------
| loop through array
| ------------------------------------------
*/
let cars = ['Toyota', 'Honda', 'Nissan'];
cars.map((car) => { 
	console.log(car);
});

/* 
| ------------------------------------------
| convert the elements of an array into a string
| ------------------------------------------
*/
let colors = ['Green', 'Red', 'Blue'];
console.log(colors.join());

/* 
| ------------------------------------------
| iterate through JSON object
| ------------------------------------------
*/
let data = {
  "employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
  ]
};

let obj = data.employees;
obj.map((employee) => {
	console.log(`${employee.firstName} ${employee.lastName}`);
});

/* 
| ------------------------------------------
| Check for Palindromes
| Source: https://www.freecodecamp.org/news/two-ways-to-check-for-palindromes-in-javascript-64fea8191fd7/
| ------------------------------------------
*/

function palindrome(str) {
  let reverseStr = str.toLowerCase().split('').reverse().join(''); 
  return reverseStr === str;
}
palindrome('eye');