Created
January 17, 2016 16:53
-
-
Save anonymous/d867f54cfa3c0fa1e09c to your computer and use it in GitHub Desktop.
https://repl.it/BY0v/179 created by ariley
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Native Browser JavaScript | |
>>> No vacancy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First version of The Array Hotel from Fullstack Academy Javascript session.