JavaScript crashes while squaring number

  • Java
  • Thread starter mindauggas
  • Start date
  • Tags
    Javascript
In summary: For (var i=100; i<151; i++){ var square = i*i; console.log(square);}BTW. Most people prefer to use the increment operator "i++", which does the same as thing "i=i+1". I used the "i=i+1" for clarity, but either "i++" or "i+=1" will work the same way.
  • #1
mindauggas
127
0
Is this a legit code for printing out squares of numbers betwean 100 and 150 ? Seems to crash everytime.

for (var i=100; i<151; i*i)
{
var square = i;
console.log(square);
}

Need help :)
 
Technology news on Phys.org
  • #2
mindauggas said:
Is this a legit code for printing out squares of numbers betwean 100 and 150 ? Seems to crash everytime.

for (var i=100; i<151; i*i)
{
var square = i;
console.log(square);
}

Need help :)

No, the upper limit on your loop is a function of the loop variable, which doesn't make sense, and you have no code IN the loop that is actually squaring anything.
 
  • #3
The problem is not really the upper limit (i<151) but the lack of an increment for the loop variable.

Your "increment" seems to be "i*i", but that's not assigned to anything, so it makes no changes to "i".

If you had written "i=i*i" then it still wouldn't work, but at least it wouldn't crash (presumably due to the infinite loop).

You need to increment "i" within the loop, and assign "i*i" to your variable "square" within the loop body.
 
Last edited:
  • #4
uart said:
You need to increment "i" within the loop, and assign "i*i" to your variable "square" within the loop body.

The general syntax of the for loop might be expressed like:

for ( start; stop; increment) {
//do stuff
}


By "You need to increment "i" within the loop" you mean i should increment within the body of the statement?

Like:

for (var i=100; i<151; i=square)
{
var square = i*i;
console.log(square);
}


I guess not. Since I get the error: "TypeError: Property 'eval' of object [object Window] is not a function".

I didn't know what to put into the "increment" part of the for loop - so I assigned square (or tried to assign, if it is not allowed).

I would very much appreciate clarifications :)
 
Last edited:
  • #5
mindauggas said:
The general syntax of the for loop might be expressed like:

for ( start; stop; increment) {
//do stuff
}


By "You need to increment "i" within the loop" you mean i should increment within the body of the statement?

Like:

for (var i=100; i<151; i=square)
{
var square = i*i;
console.log(square);
}

Ok it's just your "increment" that's wrong now. The increment is extremely important. The loop variable "i" must be increased each time the loop is executed otherwise it will never reach 151 and therefore the loop with never end. (this is why your program is "hanging").

Try
Code:
for (var i=100; i<151; i=i+1)
{
	var square = i*i;
	console.log(square);
}

BTW. Most people prefer to use the increment operator "i++", which does the same as thing "i=i+1". I used the "i=i+1" for clarity, but either "i++" or "i+=1" will work the same way.
 
Last edited:

1. Why does JavaScript crash while squaring a number?

JavaScript may crash while squaring a number if the number being squared is too large or if there is an error in the code, such as a typo or an incorrect variable assignment.

2. How can I prevent JavaScript from crashing while squaring a number?

To prevent JavaScript from crashing while squaring a number, make sure to check that the number being squared is within a reasonable range and that the code is free of errors. You can also use try-catch blocks to handle potential errors.

3. Is there a specific error message that appears when JavaScript crashes while squaring a number?

The error message that appears when JavaScript crashes while squaring a number may vary depending on the browser and the code being used. Common error messages include "RangeError: Maximum call stack size exceeded" and "TypeError: Cannot read property 'length' of undefined."

4. Can I use a different programming language to square numbers instead of JavaScript?

Yes, there are many programming languages that can be used to square numbers, such as Python, Java, and C++. However, the specific method for squaring numbers may differ between languages.

5. How can I fix a JavaScript crash while squaring a number?

To fix a JavaScript crash while squaring a number, you can try debugging the code to identify and fix any errors. You can also break the code into smaller chunks or use a different approach to square the number. If the number being squared is too large, you may need to find a different solution for your problem.

Similar threads

  • Programming and Computer Science
Replies
5
Views
365
  • Programming and Computer Science
Replies
9
Views
995
  • Programming and Computer Science
Replies
1
Views
909
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
974
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
528
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
Back
Top