← Back to challenges

Sun Loungers

JavaScriptHardconditionsloopsstrings

Instructions

A long stretch of beach is represented by a string of two characters 0 - free, 1 - occupied. Due to recent restrictions, a new person cannot take place next to another. There has to be one free place between two people lounging on the beach. Create a function to compute how many new people at most can settle in on the given beach.

Examples

sunLoungers("10001") ➞ 1
// Can take place in the middle.

sunLoungers("00101") ➞ 1
// Can take place on the left.

sunLoungers("0") ➞ 1
// Can take one place.

sunLoungers("000") ➞ 2
// Can take places on the left and on the right.

Notes

N/A

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.