Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to add £150 every 15 car sales

I need to create a script that prompts the user to enter the number of cars they have sold. For every 1 car sold , they get £100 commission. For every 15 cars sold, they receive a bonus of £150.

The bonus should kick in again at car 30, 45, 60 etc. So, if 16 cars are sold, the answer should be £1750. If 30 cars are sold, then the answer should be £3300. But the actual answer is £3150. Its not calculating the bonus correctly.

Any help would be great,

thanks

var buildList = " ";

var pay = 0;

function print(message) {
    var carList = document.getElementById("carList");
    carList.innerHTML = message;
}


var howManyCarsSold = parseInt(prompt("How many cars sold?"));
if( howManyCarsSold < 15 ) {
    pay += (howManyCarsSold * 100);
} else if (howManyCarsSold > 15) {
    pay += (howManyCarsSold * 100) + 150;
}
var totalPay = pay;
buildList += "<h2>Total cars Sold: " + howManyCarsSold + "</h2>";
buildList += "<h3>Total pay: " + "£" + totalPay + "</h3>";
print(buildList);

2 Answers

Hey there :)

You are sort of on the right track, but we can do this without conditionals. Let's look at some of the math statements that were learned earlier on in the course, as these will be very useful for your code.

First of all, one of the variables we need is 'how many lots of 15 cars sold'. So as a starting point, to get this you could simply divide the total by 15 like this.

var howMany15sSold = howManyCarsSold / 15;

This is a great starting point but we need to make sure that the howMany15sSold variable will be a whole number, and any other decimals are rounded DOWN (so that when the howManyCarsSold variable is between 0 and 14, howMany15sSold = 0, when between 15 and 30, howMany15sSold = 1, etc...

So we can use Math.floor() for this task.

var howMany15sSold = Math.floor(howManyCarsSold / 15);

So now you have how many lots of 15 were sold. You can then do something like the below to get your output. I've added comments so you can see exactly how this code works.

// This is the starting pay.
var pay = 0;

/* This will show the pay for number of cars sold
 * and bonus pay for each 15 sold.
 */

var payPerCarSale   = 100;
var bonusFor15Sales = 150;

// This shows how many cars were sold
var howManyCarsSold = parseInt(prompt("How many cars sold?"));

// For each multiple of 15, this var will increase by 1.
var howMany15sSold  = Math.floor(howManyCarsSold / 15);

// These contain the total price for sale and total bonus
var totalForSales   = howManyCarsSold * payPerCarSale;
var totalBonusSales = howMany15sSold * bonusFor15Sales;

// Now add to get your total
var pay = totalForSales + totalBonusSales;

document.write(pay);

Remember, never be afraid to create more variables to store information. If I have tried to do this in a few lines it would look very intimidating, but breaking it up like this helps you see the flow of your code. :)

Maximillian Fox, thank you so much, man! That worked perfectly. I'm not 100% sure on how this line of code works though

// For each multiple of 15, this var will increase by 1.
var howMany15sSold  = Math.floor(howManyCarsSold / 15);

Essentially this line of code will take the number of cars that were sold and divide that by 15. Next, it will round that value down to get rid of any decimal points

So here's a look at the function in action. We do the stuff on the inside of the brackets first (dividing by 15) then we do the rest, applying Math.floor() to the result of the division.

Apologies in advance for the formatting... hard to get things to line up in here.

Start number   ->    divide by 15    ->    use Math.floor()    ->    output
        1                  0.0666666                  0                      0
        2                 0.13333333                  0                      0
        ...
        14                0.93333333                 0                      0
        15                 1                           1                       1
        16                 1.06666666                 1                       1
        ...                        
        29                1.933333333                1                       1
        30                2                          2                      2
        31                 2.03333333                 2                      2
        ...             
        44                2.93333333                 2                      2
        45               3                          3                      3 
        46                3.03333333                 3                     3

etc.

So essentially, you put a number in, it will tell you how many 15s will go into that number, without that ugly decimal point ruining your fun :)