New Member #2 - splitTheBill
Closed this issue · 10 comments
Write a function to work out who is owed what when a group of friends all pay different amounts toward a bill.
- The function should take one parameter: an object with two or more name-value pairs which represent the members of the group and the amount spent by each.
- The function should return an object with the same names, showing how much money the members should pay or receive.
Further points:
- The values should be positive numbers if the person should receive money from the group, negative numbers if they owe money to the group.
- If value is a decimal, round to two decimal places.
Example
3 friends go out together: A pays £20, B pays £15, and C pays £10 toward the bill. The function should return an object showing that A should receive £5, B should receive £0, and C should pay £5.
var group = {
A: 20,
B: 15,
C: 10
}
splitTheBill(group) // returns {A: 5, B: 0, C: -5}
function splitTheBill(x) {
var sum = 0;
var num = 0;
for(var k in x){
if(x.hasOwnProperty(k)){
sum += x[k]; num++;
}
};
var avg = sum/num;
var owes = new Object();
for(k in x){ owes[k] = Math.round( (x[k] - avg + 0.00001) * 100) / 100 };
return owes;
}
Hi @TOAST565 , here is my answer. The "+0.00001" part is something I found on StackOverflow to prevent rounding errors. I can upload the js file directly tho, it works fine (I tested it on my command-line on node.js and on codewars).
Nice - the point of this is mainly for new members to practise using github, so go ahead and push the JS file in a new folder to codingforeveryone/js, ideally with a README.md doc with my instructions copied in.
The code looks good, I'll have a closer look later. Nice find on the '+0.00001' - rounding errors are such a pain.
@franzmoro
P.S. Helpful guide on git here.
And if you can make a new 'New Member #3' problem to replace this one that would be great - just make a new issue with your instructions. That way the next new member also has something to solve :)
@franzmoro
Hey, just a nudge to remind you to make an issue for 'New Member #3' in case you forgot.
Otherwise the next person who comes along has no problem to do.
Cheers :)
Hi Owen. I'm sorry, i've been very busy in the past days and today i just
got nose surgery. I will get it done soon though, no worries.
Francesco
Il 17/Nov/2015 21:35, "Owen TM" notifications@github.com ha scritto:
@franzmoro https://github.com/franzmoro
Hey, just a nudge to remind you to make an issue for 'New Member #3
#3' in case you forgot.
Otherwise the next person who comes along has no problem to do.
Cheers :)—
Reply to this email directly or view it on GitHub
#32 (comment)
.
@TOAST565
Hey, this is a great idea for newcomers to git, I guess the only problem is that we don't have enough problems! Maybe we could encourage anyone to post issues (prefaced with the 'New Member problem #") with simple problems to solve, then we can hopefully bypass the bottleneck?
I think the readme says that anyone can and should create problems. The new
member problems are only to ensure that there is always a very simple
problem for the next new member.
Please feel free to post some problems as issues, easy or hard.
On Nov 18, 2015 10:18 PM, "Andrew MacMurray" notifications@github.com
wrote:
@TOAST565 https://github.com/toast565
Hey, this is a great idea for newcomers to git, I guess the only problem
is that we don't have enough problems! Maybe we could encourage anyone to
post issues (prefaced with the 'New Member problem #") with simple problems
to solve, then we can hopefully bypass the bottleneck?—
Reply to this email directly or view it on GitHub
#32 (comment)
.
Also the new member problem should be set straight away. It is supposed to
be a simple problem which shouldnt take any time to prepare. This repo is
potentially very useful, it would be great to get it back on track.
On Nov 18, 2015 10:18 PM, "Andrew MacMurray" notifications@github.com
wrote:
@TOAST565 https://github.com/toast565
Hey, this is a great idea for newcomers to git, I guess the only problem
is that we don't have enough problems! Maybe we could encourage anyone to
post issues (prefaced with the 'New Member problem #") with simple problems
to solve, then we can hopefully bypass the bottleneck?—
Reply to this email directly or view it on GitHub
#32 (comment)
.
@JMurphyWeb Sure, will post an issue. Didn't see your pull request to update the readme before, it looks good will have a go adding to/editing it
Closing this issue as it has been solved and the new member problem is up. But agree with @JMurphyWeb, anyone can and should post new member problems - don't think we can have too many!