Advertisement
Spocoman

09. Fruit or Vegetable

Dec 19th, 2021 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function fruitOrVegetable(input) {
  2.     let product = input[0];
  3.  
  4.     if (product == "banana" || product == "apple" || product == "kiwi" || product == "cherry" || product == "lemon" || product == "grapes") {
  5.         console.log("fruit");
  6.     } else if (product == "tomato" || product == "cucumber" || product == "pepper" || product == "carrot") {
  7.         console.log("vegetable");
  8.     } else {
  9.         console.log("unknown");
  10.     }
  11. }
  12.  
  13. Второ решение(switch):
  14.  
  15. function fruitOrVegetable(input) {
  16.     let product = input[0];
  17.  
  18.     switch (product) {
  19.         case "banana":
  20.         case "apple":
  21.         case "kiwi":
  22.         case "cherry":
  23.         case "lemon":
  24.         case "grapes":
  25.             console.log("fruit");
  26.             break;
  27.         case "tomato":
  28.         case "cucumber":
  29.         case "pepper":
  30.         case "carrot":
  31.             console.log("vegetable");
  32.             break;
  33.         default:
  34.             console.log("unknown");
  35.             break;
  36.     }
  37. }
  38.  
  39. Решение с масив:
  40.  
  41. function fruitOrVegetable(input) {
  42.     let product = input[0];
  43.     let vegetable = ["tomato", "cucumber", "pepper", "carrot"];
  44.     let fruit = ["banana", "apple", "kiwi", "cherry", "lemon", "grapes"];
  45.  
  46.     console.log(fruit.includes(product) ? "fruit" : vegetable.includes(product) ? "vegetable" : "unknown");
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement