3 ms·
let distance = hypotenuseLength(reIndexedX, reIndexedY); function hypotenuseLength(x, y) { return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
by nayuki 5y ago
let distance = hypotenuseLength(reIndexedX, reIndexedY);
function hypotenuseLength(x, y) {
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}
The above can be replaced with:
let distance = Math.hypot(reIndexedX, reIndexedY);
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- wizzwizz4 5y agoIt's a tutorial, so there's a benefit to writing out the separate function in the article, but given that it's “in JavaScript” I agree with your suggestion.
- bryanbraun 5y agoHaha, yeah I discovered this after I wrote it. Probably worth going back and updating.