"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(`
Initial value: ${initValue}
`); document.write(`Depreciation Rate: ${depreciationPercent}
`); document.write(`Car Age: ${carAge}
`); let newValue = initValue; for (var i = 0; i <= carAge; i++){ document.write(`Year ${i}: ${newValue.toFixed(2)}
`); if (depreciationPercent > 0){ newValue -= newValue * (depreciationPercent/100); } }