NOTE: var
is used throughout this presentation only because Sean's doing demos in the browser console. Never let Sean catch you using var
in any other context!
var foods = ['asparagus', 'butternut squash', 'carrots', 'dates', 'endive'];
console.clear()
for (var i = 0; i < foods.length; i++) {
console.log(foods[i])
}
console.clear()
var i = foods.length
while(i--) {
console.log(foods[i])
}
console.clear()
foods.forEach( console.log )
console.clear()
function logVal (value) {
console.log(value)
}
foods.forEach( logVal )
console.clear()
foods.forEach( c => console.log(c) )
console.clear()
foods.forEach( (c, i, a) => console.log(c, i, a) )
console.clear()
var foods2 = foods.push('figs')
// console.log('foods:', foods)
// console.log('foods2:', foods2)
console.clear()
var foods3 = foods.pop()
// console.log('foods:', foods)
// console.log('foods3:', foods3)
console.clear()
for (var i = 0; i < foods.length; i++) {
foods[i] = 'chocolate-covered ' + foods[i]
}
console.log('foods:', foods)
console.clear()
var foods = ['asparagus', 'butternut squash', 'carrots', 'dates', 'endive'];
var foods4 = foods.forEach( (c, i) => foods[i] = 'chocolate-covered ' + c )
// console.log('foods:', foods)
// console.log('foods4:', foods4)
console.clear()
var foods = ['asparagus', 'butternut squash', 'carrots', 'dates', 'endive'];
var foods5 = foods.map(
(c, i) => foods[i] = 'chocolate-covered ' + c
)
console.log('foods:', foods)
console.log('foods5:', foods5)
console.clear()
var foods = ['asparagus', 'butternut squash', 'carrots', 'dates', 'endive'];
var foods6 = foods.map( (c, i) => 'chocolate-covered ' + c )
console.log('foods:', foods)
console.log('foods6:', foods6)
console.clear()
var isAllergic = food => !food.includes('a')
var foods7 = foods.filter( isAllergic )
console.log('foods:', foods)
console.log('foods7:', foods7)
console.clear()
var foods8 = foods.reduce( (r, c, i, a) => {
return r += c + ', '
}, 'Dinner tonight includes: ' )
console.log('foods:', foods)
console.log('foods8:', foods8)
console.clear()
function awardTopThree (first, second, third,...rest) {
console.log(`Bronze goes to ${third}`);
console.log(`Silver goes to ${second}`);
console.log(`Gold goes to ${first}`);
console.log(`Thank you to other competitors: ${rest}`)
}
awardTopThree('Alice', 'Bob', 'Charlie', 'Dan', 'Ed')
console.clear()
function awardTopThree (first, second, third,...rest) {
console.log(`Bronze goes to ${third}`);
console.log(`Silver goes to ${second}`);
console.log(`Gold goes to ${first}`);
console.log(`Thank you to other competitors: ${rest}`)
}
awardTopThree(...['Alice', 'Bob', 'Charlie', 'Dan', 'Ed'])