/    Sign up×
Community /Pin to ProfileBookmark

Something missing in my Javascript

Hello!
I made this program where user enters two whole numbers and program prints out cubes in given interval. Then I had to upgrade it for example, user enters number 4 and 5,–>my program prints “no cubes in given interval”. Now I upgraded it but it does not work properly, if i enter 1 and 99, it should print cubes in that interval, but it prints “No cubes in given interval”. I think there is some mistake but I am too beginner to find it out by myself.

[code]<!DOCTYPE html>
<html>
<body>

<button onclick=”myFunction()”>Try it</button>

<script>

function myFunction()
{
var m, n;

m = parseInt(prompt(“Enter first whole number m= “));
n = parseInt(prompt(“Enter second whole number n= “));
if(m>n) {
var a = n;
n=m;
m=a;

var c = parseInt(Math.cbrt(m));
if(c*c*c == m) {
alert(c);
}
c++;
while(c*c*c<=n) {
alert(c*c*c);
c++;
}
} else {

alert(“No cubes in given interval”);

}
}

</script>
</body>
</html>[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@rootOct 15.2018 — Please note that its [code] not &lt;code&gt; in the markup highlighter.

As for your script, suggest you go back to basics and start with what you know works and make incremental changes so that you pinpoint the part where the script fails.
Copy linkTweet thisAlerts:
@DeeGeeOct 20.2018 — > @juuchijs#1596803 while(c*c*c<=n) {

> alert(c*c*c);

> c++;

> }


I the above loop should'nt you have

while(c <= n) ?

Also, after c++ should'nt you have c = parseInt(Math.cbrt(c)) ?
Copy linkTweet thisAlerts:
@DeeGeeOct 22.2018 — Use this code, it works:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Cubes Roots</title>

<script>
function DoCubeRoots() {
var m, n, a, c ;
var flag = 0;

m = parseInt(prompt("Enter first whole number m = "));
n = parseInt(prompt("Enter first whole number n = "));

if( m > n ) {
a = n; n = m; m = a;
}

c = parseInt(Math.cbrt(m));

if( c*c*c == m) {
alert(c);
flag = 1;
}
c++;
while(c*c*c <= n) {
alert(c + " is the cube root of " + c*c*c);
c++;
flag = 1;
}
if( flag == 0 )
alert("No Cubes in the given Interval");

}
</script>

<head>

<body onLoad="DoCubeRoots();">

</body>

</html>
×

Success!

Help @juuchijs spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...