Categories
JavaScript Answers

How to create multidimensional array with JavaScript?

Sometimes, we want to create multidimensional array with JavaScript.

In this article, we’ll look at how to create multidimensional array with JavaScript.

How to create multidimensional array with JavaScript?

To create multidimensional array with JavaScript, we can use a for loop.

For instance, we write

let row = 20;
let column = 10;
let f = new Array();

for (i = 0; i < row; i++) {
  f[i] = new Array();
  for (j = 0; j < column; j++) {
    f[i][j] = 0;
  }
}

to create a nested for loop that adds the nested arrays to the f array with the outer loop.

In the inner loop, we fill the nested arrays with 0’s with

for (j = 0; j < column; j++) {
  f[i][j] = 0;
}

Conclusion

To create multidimensional array with JavaScript, we can use a for loop.

Categories
JavaScript Answers

How to execute shell command in JavaScript?

Sometimes, we want to execute shell command in JavaScript.

In this article, we’ll look at how to execute shell command in JavaScript.

How to execute shell command in JavaScript?

To execute shell command in JavaScript, we can use the exec function.

For instance, we write

const { exec } = require("child_process");

exec("cat *.js bad_file | wc -l", (error, stdout, stderr) => {
  console.log("stdout: " + stdout);
  console.log("stderr: " + stderr);
  if (error !== null) {
    console.log("exec error: " + error);
  }
});

to call the child_process module’s exec function.

We call it with the command we want to run in a string.

The 2nd argument is a callback that gets the output from the command.

The stdout variable has the standard output output.

And stderr has the standard error output.

error has the errors is there’s any.

Conclusion

To execute shell command in JavaScript, we can use the exec function.

Categories
JavaScript Answers

How to round to two decimal places with JavaScript?

Sometimes, we want to round to two decimal places with JavaScript.

In this article, we’ll look at how to round to two decimal places with JavaScript.

How to round to two decimal places with JavaScript?

To round to two decimal places with JavaScript, we can use toFixed and the + operator.

For instance, we write

const r = +discount.toFixed(2)

to round the discount number to 2 decimal places by first calling toFixed with 2 to convert it to a string that has discount rounded to 2 decimal places.

And then we add + in front of it to convert the rounded number string back to a number.

Conclusion

To round to two decimal places with JavaScript, we can use toFixed and the + operator.

Categories
JavaScript Answers

How to cache an image in JavaScript?

Sometimes, we want to cache an image in JavaScript.

In this article, we’ll look at how to cache an image in JavaScript.

How to cache an image in JavaScript?

To cache an image in JavaScript, we can use the lazysizes library.

For instance, we write

<script src="lazysizes.min.js" async></script>

to add the script tag for the library.

Then we add

<img data-src="images/flower3.png" class="lazyload" alt="" />

to add an img element with the lazyload class.

This will make the lazysizes library cache the image when loaded.

Conclusion

To cache an image in JavaScript, we can use the lazysizes library.

Categories
JavaScript Answers

How to make multiple left-hand assignments with JavaScript?

Sometimes, we want to make multiple left-hand assignments with JavaScript.

In this article, we’ll look at how to make multiple left-hand assignments with JavaScript.

How to make multiple left-hand assignments with JavaScript?

To make multiple left-hand assignments with JavaScript, we first declare all the variables.

And then we can chain the assignments with =.

For instance, we write

let v1, v2, v3;
v1 = v2 = v3 = 200;

to define the v1, v2 and v3 variables with let.

Then we assign 200 to v3.

Then we assign v3 to v2.

And then we assign v2 to v1 in the last line.

Using let v1, v2, v3; lets us make sure that all the variables are block scoped.

Conclusion

To make multiple left-hand assignments with JavaScript, we first declare all the variables.