Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Решение с if-else:
- function hotelRoom(input) {
- let mount = input[0];
- let nights = Number(input[1]);
- let apartment = 0;
- let studio = 0;
- if (mount === "May" || mount === "October") {
- apartment = 65;
- studio = 50;
- if (nights > 7 && nights <= 14) {
- studio *= 0.95;
- } else if (nights > 14) {
- studio *= 0.7;
- }
- } else if (mount === "June" || mount === "September") {
- apartment = 68.7;
- studio = 75.2;
- if (nights > 14) {
- studio *= 0.8;
- }
- } else if (mount === "July" || mount === "August") {
- apartment = 77;
- studio = 76;
- }
- if (nights > 14) {
- apartment *= 0.9;
- }
- console.log(`Apartment: ${(apartment * nights).toFixed(2)} lv.`);
- console.log(`Studio: ${(studio * nights).toFixed(2)} lv.`);
- }
- Решение със switch:
- function hotelRoom(input) {
- let mount = input[0];
- let nights = Number(input[1]);
- let apartment = 0;
- let studio = 0;
- switch (mount) {
- case "May":
- case "October":
- apartment = 65;
- studio = 50;
- if (nights > 7 && nights <= 14) {
- studio *= 0.95;
- } else if (nights > 14) {
- studio *= 0.7;
- }
- break;
- case "June":
- case "September":
- apartment = 68.7;
- studio = 75.2;
- if (nights > 14) {
- studio *= 0.8;
- }
- break;
- case "July":
- case "August":
- apartment = 77;
- studio = 76;
- break;
- }
- if (nights > 14) {
- apartment *= 0.9;
- }
- console.log(`Apartment: ${(apartment * nights).toFixed(2)} lv.`);
- console.log(`Studio: ${(studio * nights).toFixed(2)} lv.`);
- }
- РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
- function hotelRoom(input) {
- let mount = input[0];
- let nights = Number(input[1]);
- let apartment = (mount === "May" || mount === "October") ? 65 * (nights > 14 ? 0.9 : 1) :
- (mount === "June" || mount === "September") ? 68.7 * (nights > 14 ? 0.9 : 1) :
- (mount === "July" || mount === "August") ? 77 * (nights > 14 ? 0.9 : 1) : 0;
- let studio = (mount === "May" || mount === "October") ? 50 * (nights > 7 && nights <= 14 ? 0.95 : nights > 14 ? 0.7 : 1) :
- (mount === "June" || mount === "September") ? 75.2 * (nights > 14 ? 0.8 : 1) :
- (mount === "July" || mount === "August") ? 76 : 0;
- console.log(`Apartment: ${(apartment * nights).toFixed(2)} lv.`);
- console.log(`Studio: ${(studio * nights).toFixed(2)} lv.`);
- }
Add Comment
Please, Sign In to add comment