Created
August 17, 2018 12:31
-
-
Save inooid/6efcb11a43b95cec81545aeab446947c to your computer and use it in GitHub Desktop.
Programming Basics - Session 6 (Exercise answers)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// EXERCISE 3: | |
// ---------------------------------------------------------------------------------------------- / | |
// 3.1 | |
// ---------------------------------------------------------------------------------------------- / | |
const alphabet = 'abcdefghijklmnopqrstuvwxyz'; | |
const createProduct = (title, price) => { | |
return [title, price]; | |
}; | |
let productList = []; | |
for (let i = 0; i < 5; i++) { | |
const character = alphabet[i]; | |
const title = 'Product ' + character; // or: const title = `Product ${character}`; | |
const price = (i + 1) * 10; | |
const product = createProduct(title, price); | |
productList.push(product); | |
} | |
console.log(productList); | |
// ---------------------------------------------------------------------------------------------- / | |
// 3.2 | |
// ---------------------------------------------------------------------------------------------- / | |
const startsAndEndsWith = (text, character) => { | |
return text.startWith(character) && text.endsWith(character); | |
}; | |
console.log(startsAndEndsWith('hello world', 'h')); | |
console.log(startsAndEndsWith('billy bob', 'b')); | |
// - BONUS POINTS: ------------------------------------------------------------------------------ / | |
const startsAndEndsWith = (text, character) => { | |
const lowercasedInput = text.toLowerCase(); | |
const lowercasedCharacter = character.toLowerCase(); | |
return ( | |
lowercasedInput.startWith(lowercasedCharacter) && lowercasedInput.endsWith(lowercasedCharacter) | |
); | |
}; | |
console.log(startsAndEndsWith("Bobby's Blob", 'b')); | |
console.log(startsAndEndsWith('perryP', 'p')); | |
console.log(startsAndEndsWith('bloB', 'B')); | |
// ---------------------------------------------------------------------------------------------- / | |
// 3.3 | |
// ---------------------------------------------------------------------------------------------- / | |
const order1 = [ | |
['Awesome Product A', 25], | |
['Awesome Product B', 10], | |
['Awesome Product C', 12], | |
]; | |
const order2 = [ | |
['Awesome Product A', 25], | |
['Awesome Product C', 12], | |
['Awesome Product C', 12], | |
['Awesome Product C', 12], | |
['Awesome Product D', 8], | |
]; | |
let totalToCharge = 0; | |
const getPriceForOrder = order => { | |
let total = 0; | |
for (let i = 0; i < order.length; i++) { | |
const product = order[i]; | |
const productPrice = product[1]; | |
total += productPrice; | |
} | |
return total; | |
}; | |
totalToCharge = getPriceForOrder(order1) + getPriceForOrder(order2); | |
console.log(totalToCharge); | |
// - BONUS POINTS: ------------------------------------------------------------------------------ / | |
function getTotalFromOrders(orders) { | |
let totalForAllOrders = 0; | |
// add calculation here | |
for (let i = 0; i < orders.length; i++) { | |
const order = orders[i]; | |
totalForAllOrders = totalForAllOrders + getPriceForOrder(order); | |
} | |
return totalForAllOrders; | |
} | |
console.log(getTotalFromOrders([order1, order2])); | |
// ---------------------------------------------------------------------------------------------- / | |
// 3.4 | |
// ---------------------------------------------------------------------------------------------- / | |
const toUpperCase = input => { | |
let output = ''; | |
for (let i = 0; i < input.length; i++) { | |
const character = input[i]; | |
output += character.toUpperCase(); | |
} | |
return output; | |
}; | |
console.log(toUpperCase('Boyd')); | |
console.log(toUpperCase('you')); | |
console.log(toUpperCase('deserve')); | |
console.log(toUpperCase('a')); | |
console.log(toUpperCase('break')); | |
// ---------------------------------------------------------------------------------------------- / | |
// 3.5 | |
// ---------------------------------------------------------------------------------------------- / | |
const reverse = arr => { | |
let newArray = []; | |
const lastIndex = arr.length - 1; | |
for (let i = lastIndex; i >= 0; i--) { | |
const item = arr[i]; | |
newArray.push(item); | |
} | |
return newArray; | |
}; | |
console.log(reverse([1, 2, 3, 4])); | |
console.log(reverse([])); | |
console.log(reverse(['Kreeft', 'Nijlpaard', 'Ezel'])); | |
// ---------------------------------------------------------------------------------------------- / | |
// 3.6 | |
// ---------------------------------------------------------------------------------------------- / | |
const order99 = [ | |
['Awesome Product A', 25], | |
['Awesome Product C', 12], | |
['Awesome Product C', 12], | |
['Awesome Product C', 12], | |
['Awesome Product D', 8], | |
['Awesome Product X', 999], | |
]; | |
function sum(numbers) { | |
let total = 0; | |
for (let i = 0; i < numbers.length; i++) { | |
const number = numbers[i]; | |
total += number; | |
} | |
return total; | |
} | |
const average = numbers => { | |
return sum(numbers) / numbers.length; | |
}; | |
const averagePriceOfOrder = order => { | |
let prices = []; | |
for (let i = 0; i < order.length; i++) { | |
const product = order[i]; | |
const productPrice = product[1]; | |
prices.push(productPrice); | |
} | |
return average(prices); | |
}; | |
const orderAveragePrice = averagePriceOfOrder(order99); | |
console.log(orderAveragePrice); | |
// EXERCISE 4: | |
// ---------------------------------------------------------------------------------------------- / | |
// 4 | |
// ---------------------------------------------------------------------------------------------- / | |
let isPoweredOn = false; | |
function pressPowerButton() { | |
if (isPoweredOn) { | |
console.log('Turning off the machine...'); | |
isPoweredOn = false; | |
} else { | |
console.log('Turning on the machine...'); | |
isPoweredOn = true; | |
} | |
} | |
console.log(isPoweredOn); | |
pressPowerButton(); | |
console.log(isPoweredOn); | |
pressPowerButton(); | |
console.log(isPoweredOn); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment