Get Incremental Number from two loops - javascript

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);
}
}

Related

How to Diplay block of numbers of given input numbers

I want to display a block of numbers of the input with input times
basic web pages
var j= "";
var n=prompt("Enter the size of the box");
for(var i=1; i<=n; i++)
{
document.write(i +"<br>");
}
if the given input is 5 the output should be as follows:
o/p
12345
12345
12345
12345
12345
prompt() method only returns a string. So, first you need to convert the string to a number using parseInt()
var num = prompt('Enter the size of the box: ');
num = parseInt(num);
for(let i = 1; i <= num; i++) {
for(let j = 1; j <= num; j++) {
document.write(j+' ');
}
document.write('</br>');
}
var j= "";
var n=prompt("Enter the size of the box");
var x = [ ...Array(n*1+1).keys() ].join('').substring(1);
for(var i=1; i<=n; i++)
{
document.write(x +"<br>");
}
This will solve your problem .
var j= "";
var n=prompt("Enter the size of the box");
for(var i=1; i<=n; i++)
{
for(var j = 1; j<=n ; j++ ){
document.write(j);
}
document.write("<br>");
}
I guess you are new to programming and this example wants to teach you nested loops.
You'll have to split up your task to the smallest possible steps and think about what is happening in your loops. The best way is to work "inside-out".
So first you think about what is a single step you want to do: printing a number.
After that you'll think about how often you want to print it in a row.
After that you'll think about how you want to separate the rows.
In the final step you'll think about how many rows you want to print.
The code above will work for you now, but if you don't think about it, you'll fail at the next similar problem.
Give a man a fish and you'll feed him for a day. Teach a man to fish, and you'll feed him for a lifetime! ;-)

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

Adding zeros to integer in child loop?

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++)

Javascript WW2 Flag Stars

I'm having trouble and have been stuck on this program for a while. Basically I need to use for loops in order to make the stars in a flag on the webpage. The order is 48 stars going 8 across and 6 down. My pseudocode is
class WW2Flag
main()
//Declarations//
str j
str x
for j = 1 to 6
for x = 1 to 8 step 1
output "x "
endfor
output break
endClass
My program that I have currently is
var row = "&#10029";
var x = 48;
for (x=0;x<8;x+1){
document.getElementById("stars").innerHTML += row;
if (x==8){
break;
document.write("&#10029");
}
else {
j = 0;j<6;j + 1;
document.write("&#10029");
}
x++;
}
You just need to nest your column loop within your row loop, like this.
var stars = [], star = '&#10029';
for (var i=0; i<6; i++) {
for (var j=0; j<8; j++) {
stars.push(star);
}
stars.push('<br />\n');
}
document.getElementById('stars').innerHTML = stars.join('');
<div id="stars"></div>

JavaScript for output 6 random numbers

I'd like my output has 6 random numbers but my code only displays 4 numbers, and the numbers are the same.
var x = Math.floor(Math.random()*41);
for(var i=0; i<7; i++){
document.write(x + "</br>");
i++;
}
Could someone please help?
You need to move .random() inside your loop. Also, remove i++ from your loop, because that is already done by for().
Demo:
Script:
for( var i=0; i<6; i++ ){
var x = Math.floor(Math.random()*41);
document.write(x + "</br>");
}
You don't need the i++ inside the form, it's 6 not 7 and you need to calculate the random numbers inside the for loop.
for(var i=0; i<6; i++){
var x = Math.floor(Math.random()*41);
document.write(x + "</br>");
}
Why are you incrementing i twice? Also, if all the numbers are going to be random, you need to execute the Math function inside of the loop.
for(var i = 0; i < 6; i++) {
document.write(Math.floor(Math.random()*41) + "<br>");
}
You don't need to increment your counter twice. Also, the random() needs to be inside your loop.
var x = Math.floor(Math.random()*41);
for(var i=0; i<6; i++){
document.write(x + "</br>");
x = Math.floor(Math.random()*41);
}
you are assigning the var x with a random number before starting the loop, it remains same throughout the execution
try placing it inside the loop,also about incrementing the i, you are doing it in the for loop already so you do not need to do it inside the loop
var x = Math.floor(Math.random()*41);//place it inside the loop
for(var i=0; i<7; i++){
document.write(x + "</br>");
i++;//remove this
}

Categories

Resources