Categories
JavaScript Answers

How to Create an Array of All Integers Between Two Numbers, Inclusive, in JavaScript?

Sometimes, we want to create an array of all integers between two numbers, inclusive, in JavaScript.

In this article, we’ll look at how to create an array of all integers between two numbers, inclusive, in JavaScript.

Create an Array of All Integers Between Two Numbers, Inclusive, in JavaScript

To create an array of all integers between two numbers, inclusive, in JavaScript, we can use the Array.from method to create an array with a given length and includes integers between 2 numbers.

For instance, we can write:

const arr = Array.from({  
  length: 5  
}, (v, k) => k + 1)  
console.log(arr)

to call Array.from with an object with the length property to specify the length of the array.

The callback passed in as the 2nd argument returns a number that we want to include.

Therefore, arr is [1, 2, 3, 4, 5] .

Conclusion

To create an array of all integers between two numbers, inclusive, in JavaScript, we can use the Array.from method to create an array with a given length and includes integers between 2 numbers.

Categories
JavaScript Answers

How to Loop Through a Date Range with JavaScript?

Sometimes, we want to loop through a date range with JavaScript.

In this article, we’ll look at how to loop through a date range with JavaScript.

Loop Through a Date Range with JavaScript

To loop through a date range with JavaScript, we can use a while loop.

For instance, we can write:

const start = new Date("02/05/2020");  
const end = new Date("02/10/2020");  
let loop = new Date(start);  
while (loop <= end) {  
  console.log(loop);  
  const newDate = loop.setDate(loop.getDate() + 1);  
  loop = new Date(newDate);  
}

We create the start and end date objects that are the start and end dates of the date range.

loop is the variable with the start date.

We increment loop in the while loop until it’s bigger than or equal to end .

To do that, we write:

const newDate = loop.setDate(loop.getDate() + 1);  
loop = new Date(newDate);

Therefore, we see the dates in the date range being looped through and logged to the console.

Conclusion

To loop through a date range with JavaScript, we can use a while loop.

Categories
JavaScript Answers

How to Extract Elements with an Even Index from an Array?

Sometimes, we want to extract elements with an even index from an array.

In this article, we’ll look at how to extract elements with an even index from an array.

Extract Even Elements of an Array

To extract elements with an even index from an array., we can use the array filter instance method.

For instance, we can write:

const arr = [4, 5, 7, 8, 14, 45, 76];
const filtered = arr.filter((element, index) => {
  return (index % 2 === 0);
});
console.log(arr)

to call filter with a callback that has index as the 2nd parameter.

We return index % 2 === 0 to filter out all the elements that have odd indexes.

Therefore, filtered is [4, 5, 7, 8, 14, 45, 76] .

Conclusion

To extract elements with an even index from an array., we can use the array filter instance method.

Categories
JavaScript Answers

How to Add a Custom Button to the Toolbar that Calls a JavaScript Function with CKEditor?

Sometimes, we want to add a custom button to the toolbar that calls a JavaScript function with CKEditor.

In this article, we’ll look at how to add a custom button to the toolbar that calls a JavaScript function with CKEditor.

Add a Custom Button to the Toolbar that Calls a JavaScript Function with CKEditor

To add a custom button to the toolbar that calls a JavaScript function with CKEditor, we can add a command with the addCommand method.

Then we can add a method that calls the command with the addButton method.

For instance, we can add the editor with:

<script src="https://cdn.ckeditor.com/4.16.1/standard/ckeditor.js"></script>

<textarea id="container">How are you!</textarea>

Then we write:

const editor = CKEDITOR.replace('container');

editor.addCommand("mySimpleCommand", {
  exec(edt) {
    console.log(edt.getData());
  }
});

editor.ui.addButton('SuperButton', {
  label: "Click me",
  command: 'mySimpleCommand',
  toolbar: 'insert',
  icon: 'https://avatars1.githubusercontent.com/u/5500999?v=2&s=16'
});

to create the editor with:

const editor = CKEDITOR.replace('container');

Then we add a command with:

editor.addCommand("mySimpleCommand", {
  exec(edt) {
    console.log(edt.getData());
  }
});

The first argument is the name of the command.

The 2nd argument is an object with the exec method that has the code for the command.

We just call getData to get the input value of the text editor.

Next, we call ui.addButton with the button name the first argument.

Then 2nd argument has the settings for the button, which includes the command property set to the command that we want to run.

The icon is the URL for the icon.

Conclusion

To add a custom button to the toolbar that calls a JavaScript function with CKEditor, we can add a command with the addCommand method.

Then we can add a method that calls the command with the addButton method.

Categories
JavaScript Answers

How to Draw Lines in the HTML Canvas Without the Last Ones Being Faded?

Sometimes, we want to draw lines in the HTML canvas without the last ones being faded

In this article, we’ll look at how to draw lines in the HTML canvas without the last ones being faded.

Draw Lines in the HTML Canvas Without the Last Ones Being Faded

To draw lines in the HTML canvas without the last ones being faded, we can use the lineTo method and set the strokeStyle to the values we want.

For instance, we can create a canvas with:

<canvas id="myCanvas" width="400" height="400"></canvas>

Then we write:

const objCanvas = document.getElementById("myCanvas");
const objContext = objCanvas.getContext("2d");
const intGridWidth = 20;

const drawBackground = () => {
  objContext.clearRect(0, 0, objCanvas.width, objCanvas.height);
  objContext.fillStyle = 'black';
  objContext.fillRect(0, 0, objCanvas.width, objCanvas.height);
  objContext.strokeStyle = "white";
  for (let i = 1; i < objCanvas.width / intGridWidth; i++) {
    const x = Math.floor(i * intGridWidth) + 0.5;
    objContext.moveTo(x, 0);
    objContext.lineTo(x, objCanvas.height);
    objContext.stroke();
  }

  for (let i = 1; i < objCanvas.height / intGridWidth; i++) {
    const y = Math.floor(i * intGridWidth) + 0.5
    objContext.moveTo(0, y);
    objContext.lineTo(objCanvas.width, y);
    objContext.stroke();
  }

}
drawBackground();

to create the drawBackground function that clears the rectangle with:

objContext.clearRect(0, 0, objCanvas.width, objCanvas.height);

Then we set the background to black with:

objContext.fillStyle = 'black';

Then we apply the background styles with:

objContext.fillRect(0, 0, objCanvas.width, objCanvas.height);

Next, we set the line stroke style with:

objContext.strokeStyle = "white";

Next, we create the vertical grid lines with:

for (let i = 1; i < objCanvas.width / intGridWidth; i++) {
  const x = Math.floor(i * intGridWidth) + 0.5;
  objContext.moveTo(x, 0);
  objContext.lineTo(x, objCanvas.height);
  objContext.stroke();
}

We create the horizontal grid lines with:

for (let i = 1; i < objCanvas.height / intGridWidth; i++) {
  const y = Math.floor(i * intGridWidth) + 0.5
  objContext.moveTo(0, y);
  objContext.lineTo(objCanvas.width, y);
  objContext.stroke();
}

We use moveTo to move to the position we want to start drawing.

lineTo adds the line from the start and end position respectively.

stroke draws the line onto the canvas.

Conclusion

To draw lines in the HTML canvas without the last ones being faded, we can use the lineTo method and set the strokeStyle to the values we want.