Java JavaScript crashes while squaring number

  • Thread starter Thread starter mindauggas
  • Start date Start date
  • Tags Tags
    Javascript
AI Thread Summary
The discussion centers on a code snippet intended to print the squares of numbers between 100 and 150, which repeatedly crashes. The original code fails due to incorrect loop syntax, specifically the lack of an increment for the loop variable "i" and the absence of proper squaring logic. Instead of incrementing "i", the code attempts to use "i*i" without assigning it to anything, leading to an infinite loop. To correct the code, it is essential to increment "i" within the loop, allowing it to eventually reach the upper limit of 151. The revised code should look like this: for (var i=100; i<151; i=i+1) { var square = i*i; console.log(square);}Alternatively, using the increment operator "i++" is recommended for simplicity. This adjustment ensures the loop terminates correctly and prints the desired squares.
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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
9
Views
1K
Replies
10
Views
3K
Replies
2
Views
1K
Replies
12
Views
4K
Replies
8
Views
2K
Back
Top