display the elements of a matrix a with elements form 1 to 100 that are divisible by 7 using for loop and if function in javascript

Here's the code to display the elements of a matrix 'a' with elements from 1 to 100 that are divisible by 7 using a for loop and if statement in javascript:

index.tsx
let a = new Array(10).fill().map(() => new Array(10).fill(0)); // create a 10x10 matrix and initialize all elements to 0

for (let i = 0; i < 10; i++) { // iterate through each row
  for (let j = 0; j < 10; j++) { // iterate through each column
    let num = i * 10 + j + 1; // calculate the value of the matrix element
    if (num % 7 == 0) { // check if the value is divisible by 7
      a[i][j] = num; // assign the value to the matrix element
    }
  }
}

console.log(a); // display the matrix elements that are divisible by 7
531 chars
13 lines

In this code, we first create a 10x10 matrix 'a' with all elements initialized to 0. We then use a nested for loop to iterate through each row and column of the matrix. We calculate the value of each matrix element using the formula i * 10 + j + 1 (since javascript is 0-indexed). We then use an if statement to check if the value is divisible by 7 using the modulus operator %. If the value is divisible by 7, we assign it to the corresponding matrix element. Finally, we display the matrix by logging it to the console.

gistlibby LogSnag