I’ve wrote a code to solve a problem for a course I’m doing, it looks good to me but keeps giving me the same error code, I’ve tried a few different ways and still get the same thing.
This is the scenario;
It’s time to see if we can host our party!
Before the party is confirmed, the shareholders need two criteria to be met:
At least 5 people must be in attendance
At least £100 must be generated via pay-what-you-want ticket sales
The isPartyViable function will be called with an array of guest objects. Each of those objects has a paidForTicket property with a number representing how much they are willing to pay for their ticket.
It should return a boolean value of false if not enough people are attending or attendees don’t spend enough money on tickets, and true if enough people are attending and they spend enough money on tickets.
This is my code;
function isPartyViable(guests) {
let totalMoney = 0;
let totalGuests = 0;
for (let key in guests){
totalMoney += guests[key].paidForTickets
totalGuests += 1
}
if (totalMoney >= 100 && totalGuests >= 5 ){
return true
}
return false
}
This is the feedback I’m getting;
3 Passing
1 Failing
Returns false when not enough guests are in attendance
✓ Well done!
Returns false when not enough guests are in attendance, even if they raise sufficient funds
✓ Well done!
Returns false when not enough money is raised from ticket sales
✓ Well done!
Returns true when there are enough guests and enough money is raised
✕ AssertionError: expected false to equal true
2
Answers
You didn’t include an "else" after the if statement, so the function would just always return false no matter what
Your code doesn’t work correctly because the
for...in
loop is not an async function.So your function returns the
boolean
value before calculating all thetotalMoney
and numbers of guests.I have added an example code below.
reduce
is a javascript function ofArray
.Anyway, I’ll add another code.