Created
October 20, 2017 02:21
-
-
Save tysonwolker/e421011100aab5ba80ff1f4f2bec38e3 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/vifuguhide
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Video Store</title> | |
</head> | |
<body> | |
<h1>Rental Record for <em>martin</em></h1> | |
<table> | |
<tr><td>Ran</td><td>3.5</td></tr> | |
<tr><td>Trois Couleurs: Bleu</td><td>2</td></tr> | |
</table> | |
<p>Amount owed is <em>5.5</em></p> | |
<p>You earned <em>2</em> frequent renter points</p> | |
<script id="jsbin-javascript"> | |
"use strict"; | |
function statement(customer, movies) { | |
var totalAmount = 0; | |
var frequentRenterPoints = 0; | |
var result = "Rental Record for " + customer.name + "\n"; | |
var _iteratorNormalCompletion = true; | |
var _didIteratorError = false; | |
var _iteratorError = undefined; | |
try { | |
for (var _iterator = customer.rentals[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | |
var r = _step.value; | |
var movie = movies[r.movieID]; | |
var thisAmount = 0; | |
// determine amount for each movie | |
switch (movie.code) { | |
case "regular": | |
thisAmount = 2; | |
if (r.days > 2) { | |
thisAmount += (r.days - 2) * 1.5; | |
} | |
break; | |
case "new": | |
thisAmount = r.days * 3; | |
break; | |
case "childrens": | |
thisAmount = 1.5; | |
if (r.days > 3) { | |
thisAmount += (r.days - 3) * 1.5; | |
} | |
break; | |
} | |
//add frequent renter points | |
frequentRenterPoints++; | |
// add bonus for a two day new release rental | |
if (movie.code === "new" && r.days > 2) frequentRenterPoints++; | |
//print figures for this rental | |
result += "\t" + movie.title + "\t" + thisAmount + "\n"; | |
totalAmount += thisAmount; | |
} | |
// add footer lines | |
} catch (err) { | |
_didIteratorError = true; | |
_iteratorError = err; | |
} finally { | |
try { | |
if (!_iteratorNormalCompletion && _iterator["return"]) { | |
_iterator["return"](); | |
} | |
} finally { | |
if (_didIteratorError) { | |
throw _iteratorError; | |
} | |
} | |
} | |
result += "Amount owed is " + totalAmount + "\n"; | |
result += "You earned " + frequentRenterPoints + " frequent renter points\n"; | |
return result; | |
} | |
var customer = { | |
"name": "martin", | |
"rentals": [{ | |
"movieID": "F001", | |
"days": 3 | |
}, { | |
"movieID": "F002", | |
"days": 1 | |
}] | |
}; | |
var movies = { | |
"F001": { | |
"title": "Ran", | |
"code": "regular" | |
}, | |
"F002": { | |
"title": "Trois Couleurs: Bleu", | |
"code": "regular" | |
} | |
}; | |
var result = statement(customer, movies); | |
console.log(result); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript"> function statement(customer, movies) { | |
let totalAmount = 0; | |
let frequentRenterPoints = 0; | |
let result = `Rental Record for ${customer.name}\n`; | |
for (let r of customer.rentals) { | |
let movie = movies[r.movieID]; | |
let thisAmount = 0; | |
// determine amount for each movie | |
switch (movie.code) { | |
case "regular": | |
thisAmount = 2; | |
if (r.days > 2) { | |
thisAmount += (r.days - 2) * 1.5; | |
} | |
break; | |
case "new": | |
thisAmount = r.days * 3; | |
break; | |
case "childrens": | |
thisAmount = 1.5; | |
if (r.days > 3) { | |
thisAmount += (r.days - 3) * 1.5; | |
} | |
break; | |
} | |
//add frequent renter points | |
frequentRenterPoints++; | |
// add bonus for a two day new release rental | |
if(movie.code === "new" && r.days > 2) frequentRenterPoints++; | |
//print figures for this rental | |
result += `\t${movie.title}\t${thisAmount}\n` ; | |
totalAmount += thisAmount; | |
} | |
// add footer lines | |
result += `Amount owed is ${totalAmount}\n`; | |
result += `You earned ${frequentRenterPoints} frequent renter points\n`; | |
return result; | |
} | |
const customer = { | |
"name": "martin", | |
"rentals": [ | |
{ | |
"movieID": "F001", | |
"days": 3 | |
}, | |
{ | |
"movieID": "F002", | |
"days": 1 | |
}, | |
] | |
}; | |
const movies = { | |
"F001": { | |
"title": "Ran", | |
"code": "regular" | |
}, | |
"F002": { | |
"title": "Trois Couleurs: Bleu", | |
"code": "regular" | |
} | |
}; | |
const result = statement(customer, movies); | |
console.log(result);</script></body> | |
</html> |
This file contains 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
"use strict"; | |
function statement(customer, movies) { | |
var totalAmount = 0; | |
var frequentRenterPoints = 0; | |
var result = "Rental Record for " + customer.name + "\n"; | |
var _iteratorNormalCompletion = true; | |
var _didIteratorError = false; | |
var _iteratorError = undefined; | |
try { | |
for (var _iterator = customer.rentals[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | |
var r = _step.value; | |
var movie = movies[r.movieID]; | |
var thisAmount = 0; | |
// determine amount for each movie | |
switch (movie.code) { | |
case "regular": | |
thisAmount = 2; | |
if (r.days > 2) { | |
thisAmount += (r.days - 2) * 1.5; | |
} | |
break; | |
case "new": | |
thisAmount = r.days * 3; | |
break; | |
case "childrens": | |
thisAmount = 1.5; | |
if (r.days > 3) { | |
thisAmount += (r.days - 3) * 1.5; | |
} | |
break; | |
} | |
//add frequent renter points | |
frequentRenterPoints++; | |
// add bonus for a two day new release rental | |
if (movie.code === "new" && r.days > 2) frequentRenterPoints++; | |
//print figures for this rental | |
result += "\t" + movie.title + "\t" + thisAmount + "\n"; | |
totalAmount += thisAmount; | |
} | |
// add footer lines | |
} catch (err) { | |
_didIteratorError = true; | |
_iteratorError = err; | |
} finally { | |
try { | |
if (!_iteratorNormalCompletion && _iterator["return"]) { | |
_iterator["return"](); | |
} | |
} finally { | |
if (_didIteratorError) { | |
throw _iteratorError; | |
} | |
} | |
} | |
result += "Amount owed is " + totalAmount + "\n"; | |
result += "You earned " + frequentRenterPoints + " frequent renter points\n"; | |
return result; | |
} | |
var customer = { | |
"name": "martin", | |
"rentals": [{ | |
"movieID": "F001", | |
"days": 3 | |
}, { | |
"movieID": "F002", | |
"days": 1 | |
}] | |
}; | |
var movies = { | |
"F001": { | |
"title": "Ran", | |
"code": "regular" | |
}, | |
"F002": { | |
"title": "Trois Couleurs: Bleu", | |
"code": "regular" | |
} | |
}; | |
var result = statement(customer, movies); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment