I want to do a for loop that is looping 50 times but I need the code block inside the loop to run just on multiples of nine(9,18,27,36..)
How can I do it?
for(var i=0; i<450; i+=9) {
...
}
Alternatively, for better readability:
for(var nines = 0, loop_counter=0; loop_counter<50; loop_counter += 1, nines = loop_counter*9) {
...
}
Something like this:
for(var i = 0; i < 50; i++) {
if (i % 9 == 0) {
//code block here
}
}
for(var i = 0; i < 50; i++) {
if (i % 9 == 0) {
console.log(i);
}
}
fiddle
for(var i = 0; i < 450; i += 9) {
console.log(i);
}
fiddle
for (var i = 1; i <= 50; ++i) {
(function(multipleOfNine) {
// Do something with multipleOfNine
}(i * 9));
}
I interpreted your question to mean you want to loop over the first 50 multiples of nine. If what you meant was you want just the multiples of nine under 50, then use EnterSB's answer.
Record which iteration of the loop you are in (simplest way is to initialize a variable outside the loop to 0 and then increment it every time you go through the loop) and then use Modulo to check if it's divisible by 9. e.g. x=i%9. If x is 0 then i's a multiple of 9.
Related
I need to show in console the numbers from 1 to 20 without an array..that s what i did:
function oneToTwenty() {
for (i = 1; i <= 20; i++) {
print i;
}
}
console.log(i);
what is wrong?
The console.log(i); should be inside the for loop:
for (i = 1; i <= 20; i++) {
console.log(i);
}
there's no such thing as "print". use console.log instead of print.
You also have an extra closing "}", which doesn't make sense.
for (i = 1; i <= 20; i++) {
console.log(i);
}
You need to put your console.log inside the loop. Otherwise, you firstly do the looping and then try to log the non-existent i.
Also, note that you have to declare i, if you have not done it, using var or let.
for (let i = 1; i <= 20; i++) {
console.log(i);
}
trying to brush up on some basic javascript and I'm wondering how you would solve this. I found a way but it's pretty ugly and I'd appreciate some more experienced eyes letting me know what they thing. Basically, just iterate over the numbers 1-100 and print all even numbers in groups of five on each line. So line one would be 2,4,5,8,10, line 2 is 12,14,16,18,20. Here is what I have so far:
var counter = 0;
var line=[];
for(var i = 0; i<=100; i++){
if(i%2==0){
if(line.length <5){
line[counter] = i;
counter++
}else{
console.log(line);
line=[];
counter=0;
line[counter] =i;
}
}
}
console.log(line);
Thanks so much!
console.log(2,4,6,8,10);
console.log(12,14,16,18,20);
console.log(22,24,26,28,30);
console.log(32,34,36,38,40);
console.log(42,44,46,48,50);
console.log(52,54,56,58,60);
console.log(62,64,66,68,70);
console.log(72,74,76,78,80);
console.log(82,84,86,88,90);
console.log(92,94,96,98,100);
With most hypothetical programming problems, it's most worthwhile to address their lack of rooting in reality and do what is simplest — reality is sure to prove to be much more complicated.
As for learning exercises, those typically work best when you solve them yourself.
The problems that I see with the code is that you are looping from 0 instead of 1, and that you are not increasing the counter in the else block.
Fixing that you get:
var counter = 0;
var line=[];
for (var i = 1; i <= 100; i++) {
if (i % 2 == 0){
if (line.length < 5) {
line[counter] = i;
counter++
} else {
console.log(line);
line=[];
counter = 0;
line[counter] = i;
counter++
}
}
}
console.log(line);
Instead of looping from 1 to 100 and checking if the number is even, just loop from 2 to 100 in steps of two. You don't need the counter at all, you can push the items into the array. Instead of repeating the code that adds an item to the array in the if and else blocks you can just do it once after.
With those simplifications you get:
var line=[];
for (var i = 2; i <= 100; i += 2) {
if (line.length == 5) {
console.log(line);
line=[];
}
line.push(i);
}
console.log(line);
var line = [];
//you are uninterested in odd numbers, so increment the counter by 2
for(var i = 0; i <= 1000; i = i + 2) {
line.push(i);
if((i + 2) % 5 === 0) { //every 5 lines print result
console.log(line);
line = []
}
}
<script>
var counter = 0;
var line = [];
for (var i = 2; i <= 100; i += 2) {
line[counter] = i;
counter++;
if (line.length == 5) {
console.log(line);
counter = 0;
line = [];
}
}
</script>
Same as yours, but use a loop incrementing by 2
I am trying to write a jQuery that will find the index of a specific value within a 7x7 2D array.
So if the value I am looking for is 0 then I need the function to search the 2D array and once it finds 0 it stores the index of the two indexes.
This is what I have so far, but it returns "0 0" (the initial values set to the variable.
Here is a jsFiddle and the function I have so far:
http://jsfiddle.net/31pj8ydz/1/
$(document).ready( function() {
var items = [[1,2,3,4,5,6,7],
[1,2,3,4,5,6,7],
[1,2,3,0,5,6,7],
[1,2,3,4,5,6,7],
[1,2,3,4,5,6,7],
[1,2,3,4,5,6,7],
[1,2,3,4,5,6,7]];
var row = 0;
var line = 0;
for (i = 0; i < 7; ++i) {
for (j = 0; i < 7; ++i) {
if (items[i, j] == '0,') {
row = i;
line = j;
}
}
}
$('.text').text(row + ' ' + line);
});
HTML:
<p class="text"></p>
Your if statement is comparing
if (items[i, j] == '0,')
Accessing is wrong, you should use [i][j].
And your array has values:
[1,2,3,4,5,6,7]
....
Your value '0,' is a string, which will not match numeric values inside the array, meaning that your row and line won't change.
First, you are accessing your array wrong. To access a 2D array, you use the format items[i][j].
Second, your array doesn't contain the value '0'. It doesn't contain any strings. So the row and line variables are never changed.
You should change your if statement to look like this:
if(items[i][j] == 0) {
Notice it is searching for the number 0, not the string '0'.
You access your array with the wrong way. Please just try this one:
items[i][j]
When we have a multidimensional array we access the an element of the array, using array[firstDimensionIndex][secondDimensionIndex]...[nthDimensionIndex].
That being said, you should change the condition in your if statement:
if( items[i][j] === 0 )
Please notice that I have removed the , you had after 0. It isn't needed. Also I have removed the ''. We don't need them also.
There are following problems in the code
1) items[i,j] should be items[i][j].
2) You are comparing it with '0,' it should be 0 or '0', if you are not concerned about type.
3) In your inner for loop you should be incrementing j and testing j as exit condition.
Change your for loop like bellow and it will work
for (i = 0; i < 7; i++) {
for (j = 0; j < 7; j++) {
if (items[i][j] == '0') {
row = i;
line = j;
}
}
}
DEMO
Note:-
1) Better to use === at the place of ==, it checks for type also. As you see with 0=='0' gives true.
2) Better to say i < items.length and j<items[i].length instead of hard-coding it as 7.
var foo;
items.forEach(function(arr, i) {
arr.forEach(function(val, j) {
if (!val) { //0 coerces to false
foo = [i, j];
}
}
}
Here foo will be the last instance of 0 in the 2D array.
You are doing loop wrong
On place of
for (i = 0; i < 7; ++i) {
for (j = 0; i < 7; ++i) {
if (items[i, j] == '0,') {
row = i;
line = j;
}
}
}
use this
for (i = 0; i < 7; i++) {
for (j = 0; j < 7; j++) {
if (items[i][j] == 0) {
row = i;
line = j;
}
}
}
Here is the demo
looks like you are still learning how to program. But here is an algorithm I've made. Analyze it and compare to your code ;)
var itens = [[1,2,3,4,5,6,7],
[1,2,3,4,5,6,7],
[1,2,3,0,5,6,7],
[1,2,3,4,5,6,7],
[1,2,3,4,5,6,7],
[1,2,3,4,5,6,7],
[1,2,3,4,5,6,7]];
var row = null;
var collumn = null;
for (var i = 0; i < itens.length; i++) {
for (var j = 0; j < itens[i].length; j++) {
if (itens[i][j] == 0) {
row = i;
collumn = j;
}
}
}
console.log(row, collumn);
So I was thinking something like
number = 1;
maxnum = 32;
cat = true;
if (cat == true){
number + 1;
}
something along those lines, but I don't know how to implement that into making a constant changing line of number from 1 to 32; On the console.log.
maxnum = 32;
for(var i=0; i<maxnum; i++}{
console.log(i);
}
Use this:
var num = 1;
var maxnum = 32;
for(var i = num; i <= maxnum; i++){
console.log(i);
}
use the variable, i, in the for loop as a counter. Each round through the loop, 'i' will increment by one. CodeAcademy will give you some good practice and basic info on for loops. Eloquent Javascript is for more in depth study on the javascript language.
You can use a 'for' loop:
var maxnum = 32;
for (var i=1; i<maxnum; ++i) {
console.log(i);
}
You need to use a loop in your answer, either a for loop or a while loop.
If you change your if statement to a while loop, for example, and you can do this as follows.
number = 1;
maxnum = 32;
cat = true;
// Change "if" to "while" to make it a loop
while (cat == true){
// Add the following line to print:
console.log(number);
// Update the "cat" variable:
cat = (number < 32);
// Make sure this line uses assignment:
number += 1;
}
However, a for loop is a much cleaner solution. See the other answers posted here for examples of how to use a for loop.
var i = 1,
maxnum = 32;
while (i <= maxnum){
console.log(i++);
}
I am not sure how to phrase this, so please re-title this question if it doesn't make sense. Anyways, this is what I am trying to do.
I have a variable with the length of 9.
And then I have another variable with the length of 3.
How do I write a loop that iterates through all 9 but starts over every third time?
For example: I have this,
x = 3;
l = 9;
for ( var i = 0; i < l; i++)
{
console.log(i + 1);
}
output = 1,2,3,4,5,6,7,8,9
The output I want to create
output = 1,2,3,1,2,3,1,2,3
I was thinking there might be away to do this with an if statement or possibly modulus, but wasn't quite sure how to implement it. What would be a good way to do this? Thanks in advance.
Embrace the modulus:
function expand(length, loop_length) {
for (var i = 0; i < length; i++) {
console.log(i % loop_length + 1);
}
}
expand(9, 3) // => 1, 2, 3, 1, 2, 3, 1, 2, 3
x = 3;
l = 9;
for ( var i = 0; i < l; i++)
{
console.log(i % x + 1);
}
output = 1,2,3,1,2,3,1,2,3
See it in action here: http://jsfiddle.net/BgBGL/
If you want to loop from a min value to a max value a specific number of times, the easiest way is just to have 2 loops, like this:
var min = 1, max = 3, times = 3;
for (var i = 0; i < times; i++) {
for (var j = min; j <= max; j++) {
console.log(j);
}
}
Or if you want to fix total length of the sequence, then yes, you can do it with a single loop and a little bit of math:
var min = 1, max = 3, totalLength = 9;
for (var i = 0; i < totalLength; i++) {
console.log((i % (max - min + 1)) + min);
}
For that case, this works:
x = 3;
l = 9;
for ( var i = 0; i < l; i++)
{
var num=(i %(x+1)) || 1;
console.log(num);
}
You could go mad with following syntax:
var i = 0;
do {
console.log(i++ % 3 + 1);
} while (i < 9);
Alternative, you could define 3 and 9 as a variable also.
I took an advantage of the fact that calling i++ will display old variable and increases it by 1 after, so I saved some bits!
See: fiddle example
x = 3;
l = 9;
for ( var i = 0; i <= l; i++)
{
for ( var j = 1; j <= x; j++)
{
console.log(j);
}
}