Skip to content

Instantly share code, notes, and snippets.

Created January 17, 2016 16:53
Show Gist options
  • Save anonymous/d867f54cfa3c0fa1e09c to your computer and use it in GitHub Desktop.
Save anonymous/d867f54cfa3c0fa1e09c to your computer and use it in GitHub Desktop.
https://repl.it/BY0v/179 created by ariley
/*
The Array Hotel - Instructions
In this exercise, we'll be using an array to manage the occupancy
of a hotel called The Array Hotel.
Each room in the hotel will be represented with a "vacant"
or "occupied" string in the array. The index of the element
is the room number. For example, for a hotel with an
occupancy array of ["occupied", "vacant", "occupied", "vacant"],
we can determine that Rooms 0 and 2 are occupied
and Rooms 1 and 3 are free.
Your job is to write a function called findVacantRooms
to help the owner of the hotel find which rooms are
vacant. return an array of the room numbers that are vacant.
If all rooms are occupied, return "No Vacancy";
**************************/
function findVacantRooms(rooms) {
var length = rooms.length;
// var i = 0;
var arr = [];
for (i=0; i<length; i++) {
var roomvacancy = rooms[i];
if (roomvacancy === "vacant") {
var index = rooms.indexOf("vacant",i);
if (index > 0){
arr.push(index);
}
}
}
if (arr.length > 0){
return arr;
}else{
return "No vacancy";
}
}
/******** Tests *******/
// console.log(findVacantRooms(["occupied", "vacant", "occupied", "vacant"]));
// should return [1,3]
console.log(findVacantRooms(["occupied", "occupied", "occupied"]));
// // should return "No Vacancy"
Native Browser JavaScript
>>> No vacancy
@ariley
Copy link

ariley commented Jan 17, 2016

First version of The Array Hotel from Fullstack Academy Javascript session.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment