Challenge 1
by Cath_kb
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Mocha Tests</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
</head>
<body>
<div id="mocha"></div>
<script src="https://unpkg.com/chai/chai.js"></script>
<script src="https://unpkg.com/mocha/mocha.js"></script>
<script class="mocha-init">
mocha.setup('bdd');
mocha.checkLeaks();
</script>
<script src="https://shpax.github.io/pdffiller_js_school/challenge_5/test.js"></script>
<script class="mocha-exec">
mocha.run();
</script>
</body>
</html>
JavaScript
var John = {
name: 'John Doe',
bills: [100, 40, 250],
tips: null,
finalValues: null,
calcTipsAndValues: calcTipsAndValues,
getAverageTip: getAverageTip,
}
var Sarah = {
name: 'Sarah Doe',
bills: [80, 20, 210],
tips: null,
finalValues: null,
calcTipsAndValues: calcTipsAndValues,
getAverageTip: getAverageTip,
}
function calcTipsAndValues() {
// TODO: calc tips and finalValues arrays using this context
// bill <= 50 -> 20% tip
// 50 < bill <= 200 -> 15% tip
// bill > 200 -> 10% tip
const tips = []
const finalValues = []
this.bills.map((el, i) => {
let tip = .1
if (el <= 50) {
tip = .2
} else if (el > 50 && el <=200) {
tip = .15
}
tips[i] = el*tip
finalValues[i] = el + tips[i]
})
this.tips = tips
this.finalValues = finalValues
}
function getAverageTip() {
var averageTip = null;
// TODO: get tips from this context and return averageTip
// averageTip equals to sum of all tips divided by their amount
averageTip = this.tips.reduce((sum,el) => sum + el, 0)/this.tips.length
return averageTip
}