// Here's the explanation of the challenge -- when you're done reading it,
// click on `main.js` above to get started with the challenge!
// Have you ever sat by the road and counted the cars as they pass by?
// Sometimes coincidences happen and you see three yellow cars in a row.
// Let's use code to keep track of the cars!
// ## Description
// In this challenge, you will write a `longestRun` function that receives a
// list (array) as an argument. Each element in the list is a string
// containing a color. The list represents the colors of the different cars
// you see running down the road.
// For example, if you see a red car, then two blue cars and then a yellow
// car, your function would be invoked like:
// longestRun(["red", "blue", "blue", "yellow"])
// Your function should return the name of the color that has the longest run.
// That is, the color that appears the most times in a row, next to itself,
// without other colors in between.
// For example, if you see two blue cars, then four red cars,
// and then three blue cars, the red cars are the ones that had the longest
// run, as there was a run of four red cars. Even if there were more blue
// cars _in total_, the longest **uninterrupted** run was that of the red cars.
// You can assume that the list will not be empty, and that a single color
// appears the most times in a row.
/* Check out README.md for instructions! */
function longestRun(colors) {
/* Your code goes here! */
/* Press [▶ Run] to see your code in action! */
/* You'll see the output of the tests below the button */
}
/* Test runner (do not modify this line!) */
const expect = require("./expect")(longestRun)
/* Test cases (remember to add more!) */
// only one car
expect("red", ["red"])
// a long run
expect("blue", ["red", "blue", "blue", "yellow"])
// your test case here!
// expect(expectedColor, colors)
/* Test runner (do not modify this!) */
module.exports = (run) => function expect(expected, ...inputs) {
const stringify = (value) => {
const serialised = JSON.stringify(value, undefined, 2)
|| "undefined"
const lines = serialised.split('\n')
for (i = 1; i < lines.length; i++) {
lines[i] = "\t" + lines[i]
}
return lines.join('\n')
}
const serialisedInputs = inputs
.map(stringify)
.join(", ")
const actual = run(...inputs)
if (expected !== actual) {
console.log(
` ❌ ${run.name}(${serialisedInputs})\n\n` +
`\texpected: ${stringify(expected)}\n` +
`\tbut instead got: ${stringify(actual)}\n\n`
)
} else {
console.log(
` ✅ ${run.name}(${serialisedInputs}) === ${stringify(actual)}\n`
)
}
}