Skip to the content

Reviewing someone else's code

JavaScript · Unit 20: Testing and debugging

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. 1. Multiple choice

    An AI handed you this function for sorting numbers. What's wrong with it?

  2. 2. Predict the output

    What does this code print?

  3. 3. Complete the code

    Complete the comparator so it sorts low to high without touching the original array.

  4. 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. 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. 6. Predict the output

    has is supposed to say whether the number is in the array. What does it print?

  7. 7. Complete the code

    Complete it so has answers true or false without fighting the positions.

  8. 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. 9. Multiple choice

    An AI gave you a function and it passes the two tests you already had. What do you do?

  10. 10. Predict the output

    mean should give the average of two numbers. What do the tests print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →