One Hypotenuse to rule them all

This code demo was published on .

It’s JS Naked Day!

I’m participating in JS Naked Day with the hope of helping to promote the rule of least power. This means that your browsing experience on this website during the 50 hours that make up JS Naked Day should be identical to one where you have disabled JavaScript in your browser.

This is an excellent exercise in making sure there is a clear separation of concerns between HTML for markup, CSS for styling, and JavaScript for interactivity. I highly recommend trying it out and participating yourself!

Here’s a fun little demo of some math I found interesting.

If a right triangle has two side-lengths both equal to √2 / 2, what is the length of its hypotenuse?

We’ll build a function to calculate the length of the hypotenuse given the length of the triangle’s other two sides.

For this, we’ll use a bit of algebra you’re probably quite familiar with:

Pythagorean Theorum

a² + b² = c²

Where c represents the hypotenuse, and a and b the other two sides.

Let’s put everything together and calculate this with some JavaScript:

const side = Math.sqrt(2) / 2

// We’re looking for `c` in the equation:
// a² + b² = c²
const calculateHypotenuse = (a, b) => {
	// Start with getting the square of the sides,
	// `a` and `b`
	const a2 = Math.pow(a, 2)
	const b2 = Math.pow(b, 2)

	// Then, to isolate `c`, take the square root of
	// both sides of the equation
	return Math.sqrt(a2 + b2)
}

const hypotenuse = calculateHypotenuse(side, side)

Which gives us… 1

Interesting! 🤔

You can also send an anonymous reply (using Quill and Comment Parade).