Need help with Javascript!

Need help with Javascript!
0.0 0

#1

This is part of my code what i would like it to do is display the hotels that matches the users input. for example, if the user enters ‘3’ then it should display all hotels with a star rating of 3 and higher. But when i run it, regardless of what number i input it displays all the hotels.

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;
  }
    else if (hotels.stars>=3){
	return true;
}
  else if (hotels.stars>=2){
	return true;

}
  else if (hotels.stars>=1){
    	return true;

}
	return false;

})
console.log(matchingStars);

#2

Where is the code for the Hotel function? Without seeing the Hotel function, I can only assume the second argument of the Hotel function is the stars rating. Your current filter callback logic will keep an object where the stars value is greater or equal to 1. You assign a the user enter value from the prompt to userStar, but you are not using userStar in your filter’s callback function to make a decision of whether to return true or false.

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#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;
}
}


#4

My reply above still stands.


#5

ive tried this below, not quite sure if this is what you meant.

if(userStar>=4){
return true;

}


#6

Think about what the filter callback function is trying to do for you. You only want to return true (to keep the object) if the value of the current object of the hotels array has a stars property value greater or equal to the value entered via the prompt. The above if statement will return true (and keep the object) if the value entered via the prompt is greater or equal to 4.


#7

Appreciate the help, but i still dont understand what i need to change.


#8

The variable which holdd the vale entered via the prompt is named userStar, so your filter’s callback function should return true if the current element of the hotels array has a stars value greater or equal to userStar. See if you can translate this to an if statement.


#9

i tried but did not succeed :frowning:


#10

I tried this and it worked for me …


const  matchStars =(rating) => {
 return  hotels.map((x)=>{
 return  x.stars >= rating ?  x.name: false;
}
).filter(x => {
  return x.length > 0;
});

};

matchStars(5);

#11

@JohnL3 I was trying to let the OP figure it out without giving a solution just yet. It is fine you provided a solution, but please blur it out next time (as I did this time for you), to prevent spoiling it for the OP.

Thanks


#12

sorry randelidawson … can i still blur it ???


#13

I did it for you. In the future you use [spoiler] and [/spoiler] tags.