नीचे मेरा कोड है जिसे मैं उपयोगकर्ता इनपुट के आधार पर फ़िल्टर करना चाहता हूं। उदाहरण के लिए यदि उपयोगकर्ता 3 इनपुट करता है तो 3 और उससे अधिक की स्टार रेटिंग वाले सभी होटलों को प्रदर्शित किया जाना चाहिए। मुझे नहीं पता कि यह कैसे करना है।

class Hotel {
constructor(name, stars, distance, wifi, pool, price){
this.name = name;
this.stars = stars;
this.distance = distance;
    this.wifi = wifi;
    this.pool = pool;
    this.price = price;
 }
}

var hotels=[];
hotels.push(new Hotel("The Grand", "5","0.5","yes","no","190"));
hotels.push(new Hotel("The Plaza", "4","1","yes","yes","70") );
hotels.push(new Hotel("The Lord Miliburn", "4","5","yes","no","65") );
hotels.push(new Hotel("The Grange", "3","1","yes","no","57") );
hotels.push(new Hotel("The Windmill", "1","10","no","no","5") );
hotels.push(new Hotel("The Excel", "3","0.5","yes","no","56") );
hotels.push(new Hotel("The Ritz", "2","5","yes","no","14") );
hotels.push(new Hotel("The Victoria", "4","0.5","yes","no","80") );
hotels.push(new Hotel("Pheonix House", "4","1","yes","No","72") );
hotels.push(new Hotel("The Lodge", "2","1","no","no","25") );
hotels.push(new Hotel("The Sanctum", "5","2","yes","yes","180") );


const userStar = prompt("What would you like the minimum hotel star rating, 
 to be?");
  const matchingStars = hotels.filter(function(hotels){


   if(hotels.stars>=4){
   return true;

 }
return false;

 })
console.log(matchingStars);
0
user9575743 30 मार्च 2018, 19:21

2 जवाब

सबसे बढ़िया उत्तर

आपके पास filter कॉलबैक के अंदर userStar तक पहुंच है:

const userStar = prompt("What would you like the minimum hotel star rating, to be?");
const matchingStars = hotels.filter(function(hotel){
   return hotel.stars >= userStar;
});
console.log(matchingStars);

डेमोफिडल

0
u_mulder 30 मार्च 2018, 16:27
userStar = parseInt (userStar)

स्ट्रिंग को पूर्णांक में बदलने के लिए, इस कोड को जोड़ें। क्योंकि इनपुट डिफ़ॉल्ट रूप से एक स्ट्रिंग लौटा रहा है .. और तुलना करें:

hotels.stars >= userStar
0
ken 30 मार्च 2018, 16:28