JavaScript Algorithm: Cats and a Mouse

Erica N
Erica N
Dec 26, 2019 · 4 min read

For today’s algorithm, we are going to create a function called catAndMouse that will take in three integers: x, y, and z as inputs.

There are two cats, cat A and cat B, and a mouse. They are at various positions on a line. One of the cats wants the mouse so they each attempt to get to the mouse first. We are going to assume the mouse doesn’t move and cats are moving at equal speed. Given the starting positions of the mouse and the cats, the purpose of this function is to output which cat gets to the mouse first.

If cat A reaches the mouse first, the function will output Cat A. If cat B reaches the mouse first, the function will output Cat B. If both cats catch the mouse at the same time, then neither will get the mouse. The function will output Mouse C. The mouse gets to escape while the two cats argue with each other.

Let’s use an example:

let x = 2; // starting position of cat A
let y = 6; // starting position of cat B
let z = 3; // position of mouse

Given the position of each animal, we are going to focus on the distance between cat A and the mouse and cat B and the mouse.

If we subtract the distance between the mouse and cat A, 3 — 2 = 1, we see that cat A is 1 distance (measurement units don’t matter here) away from the mouse.

If we look at the distance between the mouse and cat B, 3 — 6 = -3, if we ignore negatives here we see that cat B is 3 distances away from the mouse.

With this information, we see that cat A is closer to the mouse. Cat A will reach the mouse first. Therefore, the function will output cat A.

Let’s use another example:

let x = 2; // starting position of cat A
let y = 4; // starting position of cat B
let z = 3; // position of mouse

This example is the same as the previous one but the only value I changed is the starting position for cat B. From our previous example, we know that cat A is 1 distance away from the mouse. If we calculate the distance from cat B and the mouse, 3 — 4 = -1. Looking at the answer as an absolute value, the distance is also 1. Because both cat A and cat B reach the mouse at the same time, the mouse escapes and the function will output Mouse C.

Let’s turn this into code:

let catAPos = Math.abs(z - x);
let catBPos = Math.abs(z - y);

The catAPos variable is what will hold the distance between cat A and the mouse. We subtract x, cat A’s position from z, the mouse’s position. We use the Math.abs() method which outputs the absolute value of the answer.

The catBPos variable is the same as the catAPos variable but the difference is this variable holds the distance between cat B, y and the mouse, z.

Now we compare values:

if (catAPos < catBPos) {
return "Cat A";
} else if (catBPos < catAPos) {
return "Cat B";
} else {
return "Mouse C";
}

In our if-statement, our first condition checks if the distance between cat A and the mouse is less than the distance between cat B and the mouse. If that is true, cat A caught the mouse first. The function will output Cat A.

If the distance between cat B and the mouse is less than the distance between cat A and the mouse, that means cat B caught the mouse first. The function will output Cat B.

If neither is true then that means both cats reached the mouse at the same time. The cats run into each other and the mouse escapes. The function will output Mouse C.

That concludes our algorithm and function. Here is the rest of the code:

function catAndMouse(x, y, z) {
let catAPos = Math.abs(z - x);
let catBPos = Math.abs(z - y);
if (catAPos < catBPos) {
return "Cat A";
} else if (catBPos < catAPos) {
return "Cat B";
} else {
return "Mouse C";
}
}

JavaScript in Plain English

Learn the web's most important programming language.

Erica N

Written by

Erica N

An illustrator that solves easy programming algorithms while writing about humorous articles about nothing.

JavaScript in Plain English

Learn the web's most important programming language.

More From Medium

More from JavaScript in Plain English

More from JavaScript in Plain English

More from JavaScript in Plain English

32 funny Code Comments that people actually wrote

10.2K

More from JavaScript in Plain English

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade