JavaScript crashes while squaring number

  • Context: Java 
  • Thread starter Thread starter mindauggas
  • Start date Start date
  • Tags Tags
    Javascript
Click For Summary

Discussion Overview

The discussion revolves around a JavaScript code snippet intended to print the squares of numbers between 100 and 150. Participants explore the reasons for the code crashing and suggest modifications to correct it, focusing on loop structure and variable assignment.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant questions the legitimacy of the code, noting that the loop's upper limit is based on the loop variable, which is problematic.
  • Another participant identifies the lack of an increment for the loop variable as the main issue, explaining that "i*i" does not change "i".
  • A suggestion is made to increment "i" within the loop and to assign "i*i" to a variable "square" inside the loop body.
  • Further clarification is sought regarding the increment part of the for loop, with a participant attempting to use "square" for incrementing "i".
  • Another participant emphasizes the importance of incrementing "i" correctly to avoid an infinite loop, suggesting a corrected version of the loop.
  • It is noted that using "i++" is a common practice for incrementing the loop variable, although "i=i+1" is also mentioned for clarity.

Areas of Agreement / Disagreement

Participants generally agree on the need to correctly increment the loop variable to prevent crashes, but there are different approaches suggested for how to implement this. The discussion remains unresolved regarding the best method to structure the loop.

Contextual Notes

There are unresolved issues regarding the proper assignment of variables and the structure of the for loop, as well as the implications of using different increment methods.

mindauggas
Messages
127
Reaction score
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
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.
 
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:
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:
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:

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K