Reviewing someone else's code
JavaScript · Unit 20: Testing and debugging
- Full plan
- JavaScript
- 10 exercises
You're going to use a lot of code you didn't write: from a workmate, from the internet or from an AI. Before pasting it, read it with three questions.
Does it do what the name promises? What happens with odd data? Can I test it in two lines?
function sorted(ns) {
return ns.sort();
}
console.log(sorted([3, 1, 2]));Prints
[ 1, 2, 3 ]
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Multiple choice
An AI handed you this function for sorting numbers. What's wrong with it?
2. Predict the output
What does this code print?
3. Complete the code
Complete the comparator so it sorts low to high without touching the original array.
4. Find the case that fails
longest(ns)gives back the longest name in the array, or an empty text if there isn't one. Which call does it fail on?5. Find the bug
An AI wrote this to take 20 % off a price of 250, which should end up at 200. Which line has the bug?
6. Predict the output
hasis supposed to say whether the number is in the array. What does it print?7. Complete the code
Complete it so
hasanswerstrueorfalsewithout fighting the positions.8. Put the lines in order
Put together your own
sorted(ns): it sorts low to high and leaves the array it receives exactly as it was.9. Multiple choice
An AI gave you a function and it passes the two tests you already had. What do you do?
10. Predict the output
meanshould give the average of two numbers. What do the tests print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.