In: Computer Science
<!DOCTYPE html>
<html>
<body>
<!-- replace the text below with your name!-->
<!-- -->
<!-- -->
<title> name </title>
<script>
//
// Write a function that calculates the amount of money a person
would earn over
// a period of years if his or her salary is one penny the first
day, two pennies
// the second day, and continues to double each day. The program
should ask the
// user for the number of years and call the function which will
return the total
// money earned in dollars and cents, not pennies. Assume there are
365 days
// in a year.
//
function totalEarned(years) {
/////////////////////////////////////////////////////////////////////////////////
// Insert your code between here and the next comment block. Do not
alter //
// any code in any other part of this file. //
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// Insert your code between here and the previous comment block. Do
not alter //
// any code in any other part of this file. //
/////////////////////////////////////////////////////////////////////////////////
}
var yearsWorked = parseInt(prompt('How many years will you work
for pennies a day? '));
alert('Over a total of ' + yearsWorked + ', you will have earned $'
+ totalEarned(yearsWorked));
</script>
</body>
</html>
<html>
<body>
<!-- replace the text below with your name!-->
<!-- -->
<!-- -->
<title> name </title>
<script>
//
// Write a function that calculates the amount of money a person would earn over
// a period of years if his or her salary is one penny the first day, two pennies
// the second day, and continues to double each day. The program should ask the
// user for the number of years and call the function which will return the total
// money earned in dollars and cents, not pennies. Assume there are 365 days
// in a year.
//
function totalEarned(years) {
/////////////////////////////////////////////////////////////////////////////////
// Insert your code between here and the next comment block. Do not alter //
// any code in any other part of this file. //
/////////////////////////////////////////////////////////////////////////////////
var total = 0; // total money earned is set to 0
var salary = 0.01; // initial salary is 1 penny
for (var i = 0; i < years * 365; i++) { // iterate for total number of days
total += salary; // add salary to total money earned
salary *= 2; // double the salary after each day
}
return total; // return the total amount of money earned
/////////////////////////////////////////////////////////////////////////////////
// Insert your code between here and the previous comment block. Do not alter //
// any code in any other part of this file. //
/////////////////////////////////////////////////////////////////////////////////
}
var yearsWorked = parseInt(prompt('How many years will you work for pennies a day? '));
alert('Over a total of ' + yearsWorked + ', you will have earned $' + totalEarned(yearsWorked));
</script>
</body>
</html>