49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
"use strict";
|
|
|
|
//Calc resale value of a car
|
|
// Take init value of car
|
|
// Take amount of yearly depreciation as a percentage
|
|
// take age of car in years
|
|
// Use a loop to depreciate a car year by year
|
|
// Write out each year's value
|
|
// Return errors if input is invalid
|
|
let invalid = true;
|
|
let initValue;;
|
|
let depreciationPercent;;
|
|
let carAge;
|
|
|
|
while (invalid){
|
|
initValue = parseFloat(
|
|
prompt("What was the initial value of the car? "));
|
|
console.log("initValue: " + initValue);
|
|
|
|
depreciationPercent = parseFloat(
|
|
prompt("What is the year depreciation rate (percentage) of the car? "));
|
|
console.log("depreciationPercent: " + depreciationPercent);
|
|
|
|
carAge = parseFloat(
|
|
prompt("What is the age of the car in years?"));
|
|
console.log("carAge: " + carAge);
|
|
|
|
if ((isNaN(initValue) || initValue < 0) ||
|
|
(isNaN(depreciationPercent) || depreciationPercent < 0) ||
|
|
(isNaN(carAge) || carAge < 0)) {
|
|
alert("You must use only 0 or positive numbers!!!\n Try again.");
|
|
}
|
|
else{
|
|
invalid = false;
|
|
}
|
|
}
|
|
|
|
document.write(`<p>Initial value: ${initValue}</p>`);
|
|
document.write(`<p>Depreciation Rate: ${depreciationPercent}</p>`);
|
|
document.write(`<p>Car Age: ${carAge}</p>`);
|
|
let newValue = initValue;
|
|
|
|
for (var i = 0; i <= carAge; i++){
|
|
document.write(`<p> Year ${i}: ${newValue.toFixed(2)} </p>`);
|
|
if (depreciationPercent > 0){
|
|
newValue -= newValue * (depreciationPercent/100);
|
|
}
|
|
}
|