Skip to content

Instantly share code, notes, and snippets.

@cplpearce
Last active March 1, 2024 17:31
Show Gist options
  • Save cplpearce/8e69b54dcdbd12a5a9a6e1d6575a2bbc to your computer and use it in GitHub Desktop.
Save cplpearce/8e69b54dcdbd12a5a9a6e1d6575a2bbc to your computer and use it in GitHub Desktop.
Kata 6 - SmartParking
const whereCanIPark = function (spots, vehicle) {
let coords = null;
let veh = [];
if (vehicle === "regular") {
veh = ["R"];
} else if (vehicle === "small") {
veh = ["R", "S"];
} else if (vehicle === "motorcycle") {
veh = ["R", "S", "M"];
} else {
console.log("Incorrect vehicle type! Please try again.");
}
// apparently you can use labels? Sourced from the nerd argument here;
// https://stackoverflow.com/questions/183161/whats-the-best-way-to-break-from-nested-loops-in-javascript
// of which the MDN Docs on label(s):
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label
rowLoop:
for (const [iR, row] of spots.entries()) {
for (const [iC, col] of row.entries()) {
if (veh.includes(col)) {
coords = [iC, iR];
break rowLoop;
} else {
continue;
}
}
}
return (coords !== null ? coords : false);
};
console.log(whereCanIPark(
[
// COLUMNS ARE X
// 0 1 2 3 4 5
['s', 's', 's', 'S', 'R', 'M'], // 0 ROWS ARE Y
['s', 'M', 's', 'S', 'r', 'M'], // 1
['s', 'M', 's', 'S', 'r', 'm'], // 2
['S', 'r', 's', 'm', 'r', 'M'], // 3
['S', 'r', 's', 'm', 'r', 'M'], // 4
['S', 'r', 'S', 'M', 'M', 'S'] // 5
],
'regular'
));
console.log(whereCanIPark(
[
['M', 'M', 'M', 'M'],
['M', 's', 'M', 'M'],
['M', 'M', 'M', 'M'],
['M', 'M', 'r', 'M']
],
'small'
));
console.log(whereCanIPark(
[
['s', 's', 's', 's', 's', 's'],
['s', 'm', 's', 'S', 'r', 's'],
['s', 'm', 's', 'S', 'r', 's'],
['S', 'r', 's', 'm', 'r', 's'],
['S', 'r', 's', 'm', 'R', 's'],
['S', 'r', 'S', 'M', 'm', 'S']
],
'motorcycle'
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment