Unable to run this code, tried using if else statement inside for loop. Want to skip q6:q10 data. Please help. Using it in Google Appscript for google spreadsheet
{ var data1=[];;
for(var i=0; i<dataLen; i++)
for (q = 0;q<20;if (q=5, q+=4; else q++)
{
data1[q]=data[i][q];
}
ss.appendRow(data1);
flag="true";
The format and lack of context makes this question very confusing, but a loop like this solves your stated problem
var data1=[];
for(var i=0; i<dataLen; i++){
for(q = 0; q < 20;q++){
if(q == 7){
q = 10;
}
data1[q] = data[i][q];
}
}
If you would like to use 2 for loops, it would look like
var data1=[];
for(var i=0; i<dataLen; i++){
for(q = 0; q < 7;q++){
data1[q] = data[i][q];
}
}
for(var i=0; i<dataLen; i++){
for(q = 10; q < 20;q++){
data1[q] = data[i][q];
}
}
I don't know what you really want, but I think you want something like this :
For(var i = 0; i<dataLen; i++){
if(i%10 == 6||i%10 == 7||i%10 == 8||i%10 == 9||i%10 == 0){
} else {
data1[i]=data[q][i];
ss.appendRow(data1);
flag = "true";
}
}
Related
I'm trying to use for loop inside for loop but it doesn't working i also tried while loop but...
int[] numArray = new int[10] {1,2,2,3,3,4,4,5,5,9};
List<Int32> uNum = new List<Int32>();
/*Random rnd = new Random();
for (int i = 0; i < numArray.Length; i++)
{
int randomNumber = rnd.Next(0, 10);
numArray[i] = randomNumber;
}*/
for (int i = 0; i < numArray.Length; i++)
{
if (numArray[i] != numArray[i])
{
for (int j = 0; j < numArray.Length-1; j++)
{
if (numArray[i] != numArray[j])
{
uNum.Add(numArray[i]);
}
}
}
}
You have an error in this line:
if (numArray[i] != numArray[i])
This condition will always return False because a number is always equal to itself.
Do something like this:
for (int i=0; i<numArray.Length; i++)
{
int j;
for (j=0; i<numArray.Length; j++){
if (i != j){
if (numArray[i] != numArray[j])
{
uNum.Add(numArray[i]);
}
}
}
}
int[] numArray = new int[10];
List<Int32> uNum = new List<Int32>();
Random rnd = new Random();
for (int i = 0; i < numArray.Length; i++)
{
int randomNumber = rnd.Next(0, 10);
numArray[i] = randomNumber;
}
for (int i = 0; i < numArray.Length; i++)
{
int num = numArray[i];
int count = 0;
for (int j = 0; j < numArray.Length; j++)
{
if (numArray[j] == num)
{
count++;
}
}
if (count == 1)
{
uNum.Add(num);
}
}
I want to make a falling sand simulation using cellular automata, but when I update it, nothing happens, and when I want to do a line of diffrent material using lineDrawing() this material appear in random cells. This is update code:
void update()
{
for (int i = verticalNumberOfCells - 1; i > 0; i--)
{
for (int j = 0; j < horizontalNumberOfCells; j++)
{
world[j][i].update(false);
}
}
for (int y = verticalNumberOfCells - 1; y > 0; y--)
{
for (int x = 0; x < horizontalNumberOfCells; x++)
{
if (world[x][y].hasMoved) continue;
if (world[x][y].state == 0 && world[x][y].state == 1) continue;
if (canMove(world[x][y].state, x, y + 1))
{
move(x, y, x, y + 1);
}
}
}
}
The auxiliary functions that I use to check if the contents of a cell can change and to change the contents of a cell look like this:
boolean canMove(int state, int positionX, int positionY)
{
if (positionX < 0 || positionX >= horizontalNumberOfCells || positionY < 0 || positionY >= verticalNumberOfCells) return false;
int otherSubstance = world[positionX][positionY].state;
if (state == 5) return (otherSubstance == 4);
if (otherSubstance == 0) return true;
if (state == 2 && otherSubstance == 3 && random(1f) < 0.5f) return true;
return false;
}
void move(int fromX, int fromY, int toX, int toY)
{
Cells otherSubstance = world[toX][toY];
world[toX][toY] = world[fromX][fromY];
world[fromX][fromY] = otherSubstance;
world[fromX][fromY].hasMoved = true;
world[toX][toY].hasMoved = true;
world[fromX][fromY].velocityX = 0;
world[fromX][fromY].velocityY = 0;
if (toX > fromX)
{
world[toX][toY].velocityX = 1;
} else if (toX < fromX)
{
world[toX][toY].velocityX = -1;
} else
{
world[toX][toY].velocityX = 0;
}
if (toY > fromY)
{
world[toX][toY].velocityY = 1;
} else if (toY < fromY)
{
world[toX][toY].velocityY = -1;
} else
{
world[toX][toY].velocityY = 0;
}
}
I was able to fix this problem. The thing was, copying a cell in the move function didn't work. Here is the wrong version of the code:
Cells otherSubstance = world[toX][toY];
world[toX][toY] = world[fromX][fromY];
world[fromX][fromY] = otherSubstance;
and here is right version of the code:
int oldState = world[toX][toY].state;
world[toX][toY].state = world[fromX][fromY].state;
world[fromX][fromY].state = oldState;
Is there a simpler way to rewrite this loop? Something with less code.
Any help is appreciated.
for(int i=0; i< 50; i++){
if(i>=0 && i<10){
method(arr[0]);
}
if(i>=10 && i<20){
method(arr[1]);
}
if(i>=20 && i<30){
method(arr[2]);
}
if(i>=30 && i<40){
method(arr[3]);
}
if(i>=40 && i<50){
method(arr[4]);
}
}
You could use this approach:
for(int i = 0; i < 50; i++){
int index = i == 0 ? 0 : (int)Math.floor(i / 10);
method(arr[index]);
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < 10; j++) {
method(arr[i]);
}
}
You could use a function to increase readability:
for (int i = 0; i < arr.length; i++) {
method10(arr[i]);
}
I would probably rewrite it using the modular function and a counter. This should help reduce the code significantly.
int index;
int count = 0;
for(int i=0; i< 50; i++){
index = i%10;
if(index==0){
count++;
}
method(arr[count-1]);
}
Use else:
for (i=0; i<50: i++) {
if (i<10) {
method(arr[0]);
} else if (i<20) {
method(arr[1]);
} else if (i<30) {
method(arr[2]);
} else if (i<40) {
method(arr[3]);
} else {
method(arr[4]);
}
}
For one liner you can use short if statement. Where the FALSE checks another condition
for (i=0; i<50: i++) {
(condition) ? TRUE : FALSE
}
var selectionSort = function(array) {
var minIndex;
for(var i = 0;i <array.length;i++){
minIndex = indexOfMinimum(array,i);}
swap(array,minIndex,i);
};
where indexOfMinimum is used to find the index of min value for the subarray starting at index i. And swap is a popularly known function.
You've got a problem with curly braces, see minIndex = indexOfMinimum(array,i);}. So basically swap(array,minIndex,i); is executed only once, not in the loop body.
Your code with corrected style:
var selectionSort = function(array) {
var minIndex;
for(var i = 0; i < array.length; i++) {
minIndex = indexOfMinimum(array,i);
}
swap(array,minIndex,i);
};
What you need:
var selectionSort = function(array) {
var minIndex;
for(var i = 0; i < array.length; i++){
minIndex = indexOfMinimum(array,i);
swap(array,minIndex,i);
}
};
Well, I am dealing with sudoku solving algorithm and generation but stuck at rather simple task. I have made the check, whether a number is really fit in the position row-wise and column-wise. But what it is driving me mad is block check, ie, whether the number is really fit in the 3x3 block.
It must be simple enough but I can't really arrive at the solution. In short, I want to know the 3x3 block to which a position in matrix belongs. Here are some of the assert cases. The block no, row no and col no indexing starts from 0.
assert("x( 0, 8 ) === 2");
assert("x( 8, 8 ) === 8");
assert("x( 3, 3 ) === 4");
assert("x( 3, 7 ) === 5");
assert("x( 7, 1 ) === 6");
x( i , j ) returns the block number where i = row and j = col.
Isn't it just:
block = 3 * (i / 3) + (j / 3)
(assumes integer operations).
I would code a check, something like this (in pseudo C++)
// row = row to check
// col = column to check
// checkNum = number we are thinking of inserting
bool check(int row, int col, int checkNum)
{
int blockRow = 3 * (row/3);
int blockCol = 3 * (col/3);
for(int i = 0 ; i < 9 ; i++)
{
if(grid[row][i] == checkNum) return false; // number exists in the row.
if(grid[i][col] == checkNum) return false; // number exists in the col.
if(grid[blockRow + i/3][blockCol + i%3] == checkNum) return false; // number exists in the block.
}
return true;
}
Here is a sudoku solver in javascript. Taken from DSSudokuSolver, that I created.
The CleanElements function does something similar to what you are asking for.
CleanElements = function(comp_ary, Qsudoku){
for(i=0; i<9; i++){
for(j=0; j<9; j++){
/*if(Qsudoku[i][j] != ""){
comp_ary[i][j]=[];
}*/
for(k=0; k<9; k++){
i_index = comp_ary[i][k].indexOf(Qsudoku[i][j]);
if(i_index != -1){
comp_ary[i][k].splice(i_index, 1);
}
j_index = comp_ary[k][j].indexOf(Qsudoku[i][j]);
if(j_index != -1){
comp_ary[k][j].splice(j_index, 1);
}
}
if(i < 3){
i_min = 0;
i_max = 2;
}
else if(i < 6){
i_min = 3;
i_max = 5;
}
else{
i_min = 6;
i_max = 8;
}
if(j < 3){
j_min = 0;
j_max = 2;
}
else if(j < 6){
j_min = 3;
j_max = 5;
}
else{
j_min = 6;
j_max = 8;
}
for(i_box=i_min; i_box<=i_max; i_box++){
for(j_box=j_min; j_box<=j_max; j_box++){
index = comp_ary[i_box][j_box].indexOf(Qsudoku[i][j]);
if(index != -1){
comp_ary[i_box][j_box].splice(index, 1);
}
}
}
}
}
return comp_ary;
}
FindElements = function(comp_ary, Qsudoku){
for(i=0; i<9; i++){
for(j=0; j<9; j++){
if(comp_ary[i][j].length == 1){
if (Qsudoku[i][j] == ""){
Qsudoku[i][j] = comp_ary[i][j][0];
comp_ary[i][j] = [];
}
}
}
}
return Qsudoku;
}
IsThereNullElement = function(Qsudoku){
for(i=0; i<9; i++){
for(j=0; j<9; j++){
if(Qsudoku[i][j] == ""){
return false;
}
}
}
return true;
}
InitEmptyArray = function(){
empty_ary = Array();
for(i=0; i<9; i++){
empty_ary[i] = Array();
for(j=0; j<9; j++){
empty_ary[i][j] = Array();
for(k=0; k<9; k++){
empty_ary[i][j][k] = (k+1).toString();
}
}
}
return empty_ary;
}
DSSolve = function(Qsudoku){
comp_ary = InitEmptyArray(); //Complementary Array
window.comp_ary_old = comp_ary;
IterationMax = 5000;
while(true){
IterationMax -= 1;
comp_ary = CleanElements(comp_ary, Qsudoku);
console.log(comp_ary);
if(window.comp_ary_old == comp_ary){
//implement this.
}
else{
window.comp_ary_old = comp_ary;
}
Qsudoku = FindElements(comp_ary, Qsudoku);
//console.log(Qsudoku);
if(IsThereNullElement(Qsudoku)){
return Qsudoku;
}
if(IterationMax == 0){
return null;
}
}
}