JS FundamentalS: Exercise - Basic syntax, conditional statements and loops. Vacation.
4. Vacation
You are given a group of people, type of the group, and day of the week they are going to stay. Based on that information calculate how much they have to pay and print that price on the console. Use the table below. In each cell is the price for a single person. The output should look like that:
"Total price: {price}". The price should be formatted to the second decimal point.
|
Friday |
Saturday |
Sunday |
Students |
8.45 |
9.80 |
10.46 |
Business |
10.90 |
15.60 |
16 |
Regular |
15 |
20 |
22.50 |
There are also discounts based on some conditions:
- Students – if the group is bigger than or equal to 30 people you should reduce the total price by 15%
- Business – if the group is bigger than or equal to 100 people 10 of them can stay for free.
- Regular – if the group is bigger than or equal 10 and less than or equal to 20 reduce the total price by 5%
You should reduce the prices in that EXACT order
Examples
Input |
Output |
30, "Students", "Sunday" |
Total price: 266.73 |
40, "Regular", "Saturday" |
Total price: 800.00 |
Моето решение ми дава само 75/100 в Judge и не мога да разбера къде бъркам, цикля вече 2 часа над него. Решавам гo само с вложени проверки, но ми се искаше и с тези switch-ове да мине. Помогнете при желание.
function solve(people, typeOfpeople, dayOfWeek) {
let price = 0;
switch (dayOfWeek) {
case 'Friday':
switch (typeOfpeople) {
case 'Students': price = 8.45;
case 'Business': price = 10.90;
case 'Regular': price = 15;
}; break;
case 'Saturday':
switch (typeOfpeople) {
case 'Students': price = 9.80; break;
case 'Business': price = 15.60; break;
case 'Regular': price = 20; break;
}; break;
case 'Sunday':
switch (typeOfpeople) {
case 'Students': price = 10.46; break;
case 'Business': price = 16; break;
case 'Regular': price = 22.50; break;
}; break;
}
let totalPrice = people * price;
if (typeOfpeople === 'Students' && people >= 30) {
totalPrice = totalPrice * 0.85;
} else if (typeOfpeople === 'Business' && people >= 100) {
totalPrice = totalPrice - (10 * price)
} else if (typeOfpeople === 'Regular' && people >= 10 && people <= 20 ) {
totalPrice = totalPrice * 0.95
}
console.log(`Total price: ${totalPrice.toFixed(2)}`);
}