// find elements
var button = $("button")
// handle click and add class
button.on("click", () => {
var value = $("#inpValue").val();
var cubeRoot = quickCubeRoot(value);
console.log(cubeRoot);
$("#spanCubeRoot").text(cubeRoot);
})
function quickCubeRoot(num) {
var cubes_10 = {
'0': 0,
'1': 1,
'8': 8,
'27': 7,
'64': 4,
'125': 5,
'216': 6,
'343': 3,
'512': 2,
'729': 9
};
// get last 3 numbers and the remaining numbers
var arr = num.toString().split('');
var last = arr.slice(-3);
var first = parseInt(arr.slice(0, -3).join(''));
// answer will be stored here
var lastDigit = 0, firstDigit = 0, index = 0;
// get last digit of cube root
for (var i in cubes_10) {
if (index === parseInt(last[last.length-1])) { lastDigit = cubes_10[i]; }
index++;
}
// get first digit of cube root
index = 0;
for (var i in cubes_10) {
if (parseInt(i) <= first) { firstDigit = index; }
index++;
}
// return cube root answer
return firstDigit + '' + lastDigit;
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.