Skip to content
Advertisement

I’m trying to add my total together from a table, and have no idea how to go about it [closed]

i’m trying to add my total together from this code, and dont know how to go about it. here is the script i’ve been working on, where their money earned doubles everyday.

<script>
    // asks for the days you worked
    var daysworked = parseFloat(prompt("daysworked", "100"));
//equation and table for the days worked
    for (var days = 1; days <= daysworked; days++) {
        document.write("<tr><td>"+ days +"</td>");

        // here is specifically the equation and how i output it 
        document.write("<td>" + "$" + ( Math.pow(2 , days - 1)*0.01) +"</td></tr>");

How should i add up the total amount of money made?

Advertisement

Answer

Maybe this will give you a hint:

var earningsPerDay = 77;
var totalEarnings = 0;

for (var days = 1; days <= daysworked; days++) {
    totalEarnings = totalEarnings + earningsPerDay;

    document.write("<tr><td>"+ days +"</td>");

    document.write("<td>" + "$" + totalEarnings +"</td></tr>");
}

UPDATE

After your explanation, here is a better answer:

var totalEarnings = 0;

for (var days = 1; days <= daysworked; days++) {
    todaysEarnings = Math.pow(2 , days - 1) * 0.01;
    totalEarnings = totalEarnings + todaysEarnings;

    document.write("<tr><td>"+ days +"</td>");

    document.write("<td>" + "$" + todaysEarnings +"</td></tr>");
}
document.write("<tr><td>" + "Total: $" + totalEarnings +"</td></tr>");

or:

document.write("<td>" + "$" + todaysEarnings +"</td><td>" + totalEarnings + "</td></tr>");
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement