Adding zeros to integer in child loop? - javascript

I am doing a loop, and through each loop I would like to add a 0 to the child loop's max range.
Example:
for(var i=0;i<10;i++)
for(var n=0;n< [?] ;n++)
The child loop should be looping from 0 to 1,10,100,1000 and so on each time around it should add a 0 to the max range.
Maybe I'm just bad at math idk but how would I do this? Thanks.

Use the power of Math.pow:
for(var i = 0; i < 10; i++)
for(var n = 0; n < Math.pow(10, i); n++)

you can do something like this :
for(var i=0;i<10;i++)
for(var n=0;n<Math.pow(10,i);n++)

Related

How to explain unexepctable perfomance in js-array loop?

Lets say we have an array of 200 000 elements for example...
Now we want to iterate it in different ways and check the fastest one. I've heard that if we will save array.length in variable before loop we will reduce execution time, so i tried the code below
let sum = 0
for (let i = 0; i < arr.length; ++i) sum += arr[i]
against
let sum = 0
for (let i = 0, l = arr.length; i < l; ++i) sum += arr[i]
But i got the same result as if in both cases js reads length value just once in the very beginning.
Then i decided to check, what if during loop we will change an array, removing last element.
let sum = 0
for (let i = 0; i < arr.length; ++i) {
sum += arr[i]
if (i === 100) arr.pop()
}
against
let sum = 0
for (let i = 0, l = arr.length; i < l; ++i) {
sum += arr[i]
if (i === 100) arr.pop()
}
So i expected that second case now should work faster because in first case js inevitably should check array.length each time and i was much suprised that it is not works faster but even slower - from 10 to 15 %. For me it is unexplainable. Any ideas?
Tests: https://jsbench.me/tfkefwjuw2
The problem is that
let sum = 0
for (let i = 0, l = arr.length; i < l; ++i) {
sum += arr[i]
if (i === 100) arr.pop()
}
is now incorrect, as it loops beyond the end of the array. The sum will be NaN in the end. The correct solution would have l = arr.length - 1 so that this doesn't happen.
Now why does it becomes so slow? Because array accesses in javascript are only fast (get compiled to a fast path with pointer addressing) when the element exists. When you miss, the code will get de-optimised. Since JSbench runs the same code multiple times, so even if the deoptimisation happens only at the last of 200000 iterations, the subsequent runs will be much slower.
See this talk for details, which even explicitly spells out "Avoid out-of-bounds reads" on one slide. In general, don't use non-idiomatic code to improve performance. i < arr.length is idiomatic, and JS engines will optimise it well.

Nested Array in JavaScript

I have an array soter and a counter array. I want to get the the number of name which the count array will provide me. Is it correct ? I am a bit confused about the output. Can someone enligten me on this nested array loop in JavaScript ?
var soter = ['bp','mf','cc'],
count = [0,0,0];
for(var y = 0 ; y < soter.length; y++) {
for(var i = 0 ;i < data.SO_Ter.length; i++) {
if(data.SO_Ter[i].name == soter[y]) {
count[y]++;
That code seems correct to me, supposing the well formed object data and child SO_Ter .
So you go through the outer loop, positions 0 to 2, and for each one of them you will check that each of the items in data.SO_Ter is equal to the soter value.
If you find that value, you increment the count in 1.
Does it make sense?
To make it easier, it would be like:
for(var i = 0 ;i < data.SO_Ter.length; i++) {
if(data.SO_Ter[i].name == soter[0]) {
count[0]++;
for(var i = 0 ;i < data.SO_Ter.length; i++) {
if(data.SO_Ter[i].name == soter[1]) {
count[1]++;
for(var i = 0 ;i < data.SO_Ter.length; i++) {
if(data.SO_Ter[i].name == soter[2]) {
count[2]++;
So since you do it 3 times, you just replace those with an outer for loop.
UPDATE
count[0] represents how many times the word 'bp' has been found
count[1] represents how many times the word 'mf' has been found
count[2] represents how many times the word 'cc' has been found

Get Incremental Number from two loops

i have two simple regular for loops.
for (var i=0; i<images.xAxis.length; i++){
for (var x=0; x<images.yAxis.length; x++){
//return number from 0 to 9
imgNumber = images.xAxis.length*i+x;
addImage(imgNumber);
}
}
Well it is not returning number from 0 to 9.
instead it returning :
1
2
3
-
2
4
6
-
3
6
9
Assuming that i don't want to use helper variable like:
idx+=1;
i want to use x and i in some math expression.
Thanks!
What you are basically trying to do with those nested loops is, how to convert a number in base 3 to a number in base 10. The way to do it is:
for (var i=0; i<3; i++){
for (var x=0; x<3; x++){
//return number from 0 to 9
addImage(3*i+x);
}
}
EDIT
With your edit, the code becomes
for (var x=0; x<images.xAxis.length; x++){
for (var y=0; y<images.yAxis.length; y++){
//return number from 0 to 9
imgNumber = images.xAxis.length*i+x;
addImage(imgNumber);
}
}
You can try using:-
for (var i=0; i<3; i++){
for (var x=0; x<3; x++){
imgNumber = i * 3 + x;
addImage(imgNumber);
}
}

Looping a section of Javascript a set number of time

What do I add at the beginning and end of a section of code to repeat it 36 times before going on?
You use a for loop:
for ( var i = 0; i < 36; i++ ) {
// This will loop 36 times
}
You add a for loop:
for (var i = 0; i < 36; i++) {
}
You are looking for a for loop: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for

For loop repeating first iteration twice

I am having an issue with a javascript for loop. I am adding up the elements of an array, but for some reason my loop adds in the first entry twice! There was a similar topic on here before (http://stackoverflow.com/questions/3121670/for-loop-repeats-first-loop-twice) but the author didn't go into his resolution in detail, just that it was "somethin stupid" he did. Can anyone tell me what I'm doing stupid??
for(j=0;j<ARRAY.length;j++)
{TOTAL += ARRAY[j];}
The output is used in a HTML table and it is displaying correctly, it's just the doubled first entry that's the issue!
Any help would be greatly appreciated.
var TOTAL = 0;
for ( var j = 0, len = ARRAY.length; j < len; j++ ) {
TOTAL += ARRAY[j];
}
MDN suggest to use a variable to hold the array length. In addition check your scripts with JSLint.
make sure you declare with var...
Nice little bit of prompting debugging in there too.
for(var j=0; j < ARRAY.length; j++) {
{
TOTAL += ARRAY[j];
//alert("The count of J is now " + j);
}
Thanks for all of your help. Since my original approach hit a dead end I looked into using a function to do the trick. The following works:
Array.prototype.sum = function() {
for (var j = 0, L = this.length, sum = 0; j < L; sum += this[j++]);
return sum;
}
I then call ARRAY.sum() when creating my html table.
I found the above solution on http://www.codingforums.com/showthread.php?t=218803

Categories

Resources