Skip to content

Instantly share code, notes, and snippets.

@martinlaws
Created February 5, 2020 17:16
Show Gist options
  • Save martinlaws/102688ba635ec0e8a4b8aa3dc9c22962 to your computer and use it in GitHub Desktop.
Save martinlaws/102688ba635ec0e8a4b8aa3dc9c22962 to your computer and use it in GitHub Desktop.
// bracket pair colorizer
const arrayOfNames = ["martin", "scott", "craig", "maya"];
arrayOfNames.forEach(function(names) {
console.log(names)
})
for (let name of arrayOfNames) {
console.log(name);
}
for (let [index,name] of arrayOfNames.entries()) {
console.log(`${index:} ${name}`);
}
for (let nameIndex in arrayOfNames) {
console.log(arrayOfNames[nameIndex])
}
arrayOfNames.forEach(name => console.log(name));
const arrayOfNumbers = [1, 10, 12, 14];
function sumNumbers(numbersArray) {
let total = 0;
for (let number of numbersArray) {
total += number;
}
console.log(total);
}
sumNumbers(arrayOfNumbers)
const person = {
firstName: "Martin",
lastName: "Laws",
heightInCm: 188
};
const objectKeys = Object.keys(person);
// this is a _for of_ loop:
for (let key of objectKeys) {
console.log(`${key}: ${person[key]}`);
}
const objectValues = Object.values(person);
// this is a _for in_ loop, it returns index position for each item in the array:
for (let indexPosition in objectKeys) {
console.log(`${objectKeys[indexPosition]}: ${objectValues[indexPosition]}`);
}
// this is uses Object.entries() which returns an array of [key, value] for each key/value pair in an object:
console.log(Object.entries(person));
const person = {
firstName: 'Martin',
lastName: 'Laws',
fullName: function() {
return `${this.firstName} ${this.lastName}`;
},
heightInCm: 188,
heightInInches() {
return this.heightInCm * 2.54
}
}
console.log(person.firstName)
console.log(person.lastName)
console.log(person.heightInCm)
console.log(person.fullName())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment