Creating a Javascript system to calculate income for my Jazz Band

Hello wonderful people of FreeCodeCamp!
I’m Luca. I’m 23. I am a musician, producer and Composer based in London. I got into coding only some weeks ago and I find amazing the source of material here on FreeBootCamp and generally online!

Hopefully some incredible people will help both me and my band to solve this coding/mathematical/logical problem.

I play in a Jazz quartet (we are 4 musicians, var numberMusicians = 4), we play regularly in venues and we charge all together £ 58.50/per hour in total (const hourlyFee = 58.50).
I usually split equally the amount earned between everybody (totalAmountEarned / numberMusicians) so that everybody gets the same share.
Now, everything was alright until something happened on the last gig:
The piano player left an hour before, leaving us to play without him.
Fairly enough, that means that we have to split the money that he is not receiving for the last hour when he didn’t play between me and the other musicians.

I am trying to write an algorithm into JavaScript so that for the future time, if something similar happens, for example, two, or tree or any number of people leave, on different times, I can just pop in the variables in the code and get the results.
The question is, how much should everybody get from the total amount, considering the time that each one of us played singularly? And… how to write it into code?

Here is what I’ve written in Javascript. It’s the first time that I’m actually applying it in real life, and I have very little experience in writing it, so I apologise for the confusional syntax!

It would be great if somebody gave me some tips on the code so that it works! My eyes are crossing, I start to have a bit of an headache, but I’m as stubborn as my good old grandpa! I want to learn how to do it!
I am pretty sure I’ messing up a lot on writing the actual syntax, but maybe quite a lot of other things are completely wrong…


const hourlyFee = 58.50;
var numberMusicians = 4;
var hoursPlayed = 3;
var hoursPlayedBySingleMusician = hoursPlayed;
var hoursPlayedBySingleMusicianWhoLeft = 2;

var timePlayedRate = function() {
return hoursPlayedBySingleMusician / hoursPlayed;

var timePlayedRateIfSomeoneLeftBefore = function() {
return hoursPlayedBySingleMusicianWhoLeft / hoursPlayed;

var totalAmount = function() {
return hourlyFee * hoursPlayed;

var amountPerPerson = function() {
totalAmount / numberMusicians;

var timeNotPlayedRateIfSomeoneLeftBefore = function() {
return timePlayedRate - hoursPlayedBySingleMusicianWhoLeft;

var earningRate = function() {
return timePlayedRate + timeNotPlayedRateIfSomeoneLeftBefore;

function musicianPayment() {
return amountPerPerson * earningRate;


1 Like

I think a more flexible way would be an array of hours worked, like:

var hoursWorked = [3, 3, 3, 2];

Then you could loop through it and get the fee for each musician or sum it to get the whole fee.

Putting it in an array would be much more flexible. What if the piano player had to play an hour solo before the gig (done this at weddings) and the bass player was 45 minutes late? Putting this all in an array would be very flexible. You could have a function that just prints out the pay for each musician. If you wanted it could be an array of arrays to include names or instruments:

var hoursWorked = [
          ["piano", 4 ],
          ["sax", 3],
          ["bass", 2.25],
          ["drums", 3] 

You could make some very pretty output with that.

The fee for each musician would be the totalFee X (theirHours / totalHours).

Just a thought.

1 Like

Building upon this excellent suggestion, I offer this for you to have a look at

const hoursWorked = [3, 3, 3, 2];
const lengthOfGig = Math.max.apply(null, hoursWorked);
const hourlyRate = 58.5;
const totalPay = hourlyRate * lengthOfGig;
const totalHoursWorked = hoursWorked.reduce(function(a, b) { return a + b; }, 0);
const valueOfEachHour = totalPay/totalHoursWorked;
const payForEachPerson = {return Math.round((100 * a * valueOfEachHour))/100;})
const checkTotal = payForEachPerson.reduce(function(a, b) { return a + b; }, 0);

I’m sure it can be improved, but it’s a start

Thanks guys for the awsome tips!
Writing an array as a base for further development is a great idea!
I’m a total noob yet, so I hope to ask you some clarifications on a couple lines of code that you guys have written.

@r1chard5mith I like the way you wrote the code, looks clean and direct. A couple of dumb questions, tough.

When you say “const lengthOfGig = Math.max.apply(null, hoursWorked)”, what does it do speifically? Does it return the highest number in the array, meaning the total lenght of the gig?

When you wrote “const totalHoursWorked = hoursWorked.reduce(function(a, b) { return a + b; }, 0);” what does it do? Can you explain it?

Thanks for helping out!

Hi - no worries…

const lengthOfGig = Math.max.apply(null, hoursWorked)

Yes, this finds the biggest element in the array - the Math.max function usually takes two numbers and returns the biggest but used like this it can take an array and do the same.

const totalHoursWorked = hoursWorked.reduce(function(a, b) { return a + b; }, 0);

This adds up the elements of the array. When you do a ‘reduce’ it’s going to do some sort of operation to each element of the array and end up with a single result. In this case we’re telling it to add them together (add each number to the running total)

I really good place to look these sort of things up is the Mozilla Developer Network, try searching for eg; “mdn slice” or “mdn math max” - they tend to be the clearest explanations imo.

Any other questions, feel free.

I have a question for the OP, to simplify things, lets say you are in a Duet.

You have a 2 hour gig, and 1 of you plays a solo set for 1 hour, then the 2nd hour you play together.

The band has a fixed rate of $58.50/hour regardless of how many from the band are playing.

Gig Information
2 hrs * 58.50/hour = $117 total invoiced by the band
Band Member 1 = 2 hours
Band Menber 2 = 1 hour

Scenario A
Total is 3 band-man-hours
$117/3 = $37/band-man-hours
Band member 1 gets $74
Band member 2 gets $37

Scenario B
First hour is played by Band member 1 who gets the whole $58.50 for that hour
Second hour is split by both members so each gets $29.25 per hour
Band Member 1 gets $87.75
Band Member 2 gets $29.25

There is a slight difference in pay rate, depending on how you want to split up the hours. The suggestions so far use Scenario A. IF you want to use Scenario B you’ll need to do it a little differently.

That’s an interesting point. Of course Scenario A is easier to calculate (BM1 gets 2/3 and BM2 gets 1/3) but perhaps Scenario B (BM1 gets 3/4 and BM2 gets 1/4) is more fair because BM1 gets the full amount for the first hour instead of having to give 1/12 of the pay to BM2 who wasn’t even there.

Of course, this gets even more complicated if you have multiple overlaps of musicians. From my earlier hypothetical of:

What if the piano player had to play an hour solo before the gig (done this at weddings) and the bass player was 45 minutes late?

I imagine, to do it write you’d have to have an array of objectsof two elements, an “hours played” and an array of musicians who played that time slot. So, from the above example, it might look like this:

var payRoster = [
          { hours: 1, players: ["piano"] },                              // piano was by himself first hour
          { hours: 0.75, players:  ["piano", "sax", "drums"] },          // band played 45 mins w/o bass
          { hours: 2.25, players:  ["piano", "bass", "sax", "drums"] },  // band finished git together

Then you could go through each time slot and figure how much money was earned in that time slot (hourlyRate*payRoster[i].hours) and then divide that equally among each of the musicians that was there in that time slot. You’d have to make an array to contain the pay for each musician.

Of course, another way to handle it is just to have the bass give his money to the piano players left hand for the 45 minutes it covered for him. :wink:

1 Like