Loading...
beginner
Problem 03 • Step 01
Find the Bug
Here is a function that picks a random word from a list:
function randomChoice(list) {
var index = Math.round(Math.random() * list.length);
return list[index];
}
It looks right. It runs. Most of the time, it works.
But there is a bug.
Your task
Write tests in tests.js that expose the bug. You cannot edit the function. You can only write tests.
Since randomChoice is random, you cannot check for a specific return value. But you can check properties that should always be true. This approach is called property-based testing: instead of checking specific outputs, you check properties that must always hold.
- The result should never be
undefined - The result should always be an item from the list
The bug is rare. It only triggers about 5% of the time. To catch it reliably, run the function many times in a loop. A starter loop is already in tests.js. Add your checks inside it.