I want to print the root of all numbers to 9999. How do I tell the program to skip the numbers that don't have a round root? Here's the code
let i=1;
for (i===1;i>=1 && i <10000;i++){
let b = Math.sqrt(i);
console.log(`${i} = ${b}`);
}
I want to print the root of all numbers to 9999. How do I tell the program to skip the numbers that don't have a round root? Here's the code
let i=1;
for (i===1;i>=1 && i <10000;i++){
let b = Math.sqrt(i);
console.log(`${i} = ${b}`);
}
You can check if both the int
value and the original value are same.
let i=1;
for (i=1;i>=1 && i <10000;i++){
let b = Math.sqrt(i);
if (Math.trunc(b) == b)
console.log(`${i} = ${b}`);
}
Instead of Math.trunc(b)
, you can use either of the following:
Math.round(b)
Math.floor(b)
Math.ceil(b)
parseInt(b, 10)
You don't need to iterate and test every number up to 10000. You can directly compute powers of two:
var count = 0, i = 0;
while (count < 10000) {
i++;
var b = i * i;
console.log(`${i} = ${b}`);
count = b;
}
Or as mentioned in comments you can do it elegantly with for-loop:
for (let i = 1; i*i < 10000; i++) {
console.log(`${i*i} = ${i}`);
}