Created
August 22, 2017 00:53
-
-
Save tysonwolker/d91e843038d999e4b8f57fbfb8208f09 to your computer and use it in GitHub Desktop.
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
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" | |
} | |
}; | |
function statement(customer, movies) { | |
let totalAmount = 0; | |
let frequentRenterPoints = 0; | |
let result = `Rental Record for ${customer.name}\n`; | |
for (let r of customer.rentals) { | |
debugger; | |
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; | |
} | |
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