Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for [old] The Nearest Square Number by Sim0000
"use strict";
function nearestSquare(number) {
let q = Math.floor(Math.sqrt(number));
let n0 = q * q;
let n1 = (q + 1) * (q + 1);
return Math.abs(n0 - number) < Math.abs(n1 - number) ? n0 : n1;
}
var assert = require('assert');
if (!global.is_checking) {
// These "asserts" are used for self-checking and not for an auto-testing
assert.equal(nearestSquare(8), 9)
assert.equal(nearestSquare(13), 16)
assert.equal(nearestSquare(24), 25)
assert.equal(nearestSquare(9876), 9801)
console.log("Coding complete? Click 'Check' to earn cool rewards!");
}
March 22, 2018
Comments: