Skip to content

Instantly share code, notes, and snippets.

@martinlaws
Created January 8, 2020 22:39
Show Gist options
  • Save martinlaws/c546bf490719c80c0d1d747fbe67820b to your computer and use it in GitHub Desktop.
Save martinlaws/c546bf490719c80c0d1d747fbe67820b 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