Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function fruitOrVegetable(input) {
- let product = input[0];
- if (product == "banana" || product == "apple" || product == "kiwi" || product == "cherry" || product == "lemon" || product == "grapes") {
- console.log("fruit");
- } else if (product == "tomato" || product == "cucumber" || product == "pepper" || product == "carrot") {
- console.log("vegetable");
- } else {
- console.log("unknown");
- }
- }
- Второ решение(switch):
- function fruitOrVegetable(input) {
- let product = input[0];
- switch (product) {
- case "banana":
- case "apple":
- case "kiwi":
- case "cherry":
- case "lemon":
- case "grapes":
- console.log("fruit");
- break;
- case "tomato":
- case "cucumber":
- case "pepper":
- case "carrot":
- console.log("vegetable");
- break;
- default:
- console.log("unknown");
- break;
- }
- }
- Решение с масив:
- function fruitOrVegetable(input) {
- let product = input[0];
- let vegetable = ["tomato", "cucumber", "pepper", "carrot"];
- let fruit = ["banana", "apple", "kiwi", "cherry", "lemon", "grapes"];
- console.log(fruit.includes(product) ? "fruit" : vegetable.includes(product) ? "vegetable" : "unknown");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement