Categories
JavaScript Answers Nodejs

How to read a 3 bytes Buffer as an integer with JavaScript and Node.js?

Sometimes, we want to read a 3 bytes Buffer as an integer with JavaScript and Node.js.

In this article, we’ll look at how to read a 3 bytes Buffer as an integer with JavaScript and Node.js.

How to read a 3 bytes Buffer as an integer with JavaScript and Node.js?

To read a 3 bytes Buffer as an integer with JavaScript and Node.js, we can use the readUintBE method.

For instance, we write:

const buffer = Buffer.from('hello world', 'utf8');
const decimal = buffer.readUIntBE(0, 3);
console.log(decimal)

to create a buffer with Buffer.from.

Then we call buffer.readUIntBE with 0 and 3 to read the 3 bytes and convert it into a decimal number.

Therefore, decimal is 6841708.

Conclusion

To read a 3 bytes Buffer as an integer with JavaScript and Node.js, we can use the readUintBE method.

Categories
JavaScript Answers

How to generate a random number between 2 values to 2 decimals places in JavaScript?

Sometimes, we want to generate a random number between 2 values to 2 decimals places in JavaScript.

In this article, we’ll look at how to generate a random number between 2 values to 2 decimals places in JavaScript.

How to generate a random number between 2 values to 2 decimals places in JavaScript?

To generate a random number between 2 values to 2 decimals places in JavaScript, we can use the Math.floor and Math.random methods.

For instance, we write:

const precision = 100;
const randomNum = Math.floor(Math.random() * (10 * precision - 1 * precision) + 1 * precision) / (1 * precision);
console.log(randomNum)

to call Math.random to generate a number between 0 and 1.

Then we multiply the returned number by (10 * precision - 1 * precision) and add by 1 * precision to return the number in the range we want multiplied by precision.

Then we divide by (1 * precision) to return the number with the precision and range we want.

Therefore, randomNum should be a random number with 2 decimal places since precision is 100.

Conclusion

To generate a random number between 2 values to 2 decimals places in JavaScript, we can use the Math.floor and Math.random methods.

Categories
JavaScript Answers

How to convert an HTML table to an array in JavaScript?

Sometimes, we want to convert an HTML table to an array in JavaScript.

In this article, we’ll look at how to convert an HTML table to an array in JavaScript.

How to convert an HTML table to an array in JavaScript?

To convert an HTML table to an array in JavaScript, we can use the spread operator and some array methods.

For instance, we add a table with:

<table id="cartGrid">
  <thead>
    <tr>
      <th>Item Description</th>
      <th>Qty</th>
      <th>Unit Price</th>
      <th>Ext Price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Old Lamp</td>
      <td>1</td>
      <td>107.00</td>
      <td>107.00</td>
    <tr>
      <td>Blue POst</td>
      <td>2</td>
      <td>7.00</td>
      <td>14.00</td>
  </tbody>
</table>

Then we write:

const table = document.querySelector('table')
const arr = [...table.rows].map(r => [...r.querySelectorAll('td, th')].map(td => td.textContent))
console.log(arr)

to get all the tr elements in a node list with table.rows.

Then we spread them into an array.

Next, we call map with a callback that select all the td’s or th’s, spread them into an array, and then call map on them to get their textContent.

As a result, arr is:

[
  [
    "Item Description",
    "Qty",
    "Unit Price",
    "Ext Price"
  ],
  [
    "Old Lamp",
    "1",
    "107.00",
    "107.00"
  ],
  [
    "Blue POst",
    "2",
    "7.00",
    "14.00"
  ]
]

Conclusion

To convert an HTML table to an array in JavaScript, we can use the spread operator and some array methods.

Categories
JavaScript Answers

How to rank array elements with JavaScript?

Sometimes, we want to rank array elements with JavaScript.

In this article, we’ll look at how to rank array elements with JavaScript.

How to rank array elements with JavaScript?

To rank array elements with JavaScript, we can use the sort method.

For instance, we write:

const arr = [79, 5, 18, 5, 32, 1, 16, 1, 82, 13];
const sorted = arr.slice().sort((a, b) => b - a)
const ranks = arr.map(v => sorted.indexOf(v) + 1);
console.log(ranks);

We call arr.slice to make a copy of arr.

Then we call sort on it to return a sorted version of arr sorted in descending order.

Then we call arr.map with a callback that returns the position of the sorted array the arr entry v is in plus 1.

Therefore, ranks is [2, 7, 4, 7, 3, 9, 5, 9, 1, 6].

Since 79 is 2nd largest, 5 is 7th largest etc.

Conclusion

To rank array elements with JavaScript, we can use the sort method.

Categories
JavaScript Answers

How to get an image natural height with JavaScript?

Sometimes, we want to get an image natural height with JavaScript.

In this article, we’ll look at how to get an image natural height with JavaScript.

How to get an image natural height with JavaScript?

To get an image natural height with JavaScript, we can use the naturalHeight property.

For instance, we write:

<img src='https://picsum.photos/200/300'>

to add an img element.

Then we write:

const image = document.querySelector('img')
console.log(image.naturalHeight)

to select the img element with querySelector.

And then we use the image.naturalHeight property to get the natural height of the image.

Conclusion

To get an image natural height with JavaScript, we can use the naturalHeight property.