Categories
JavaScript Answers

How to make a number a percentage with JavaScript?

Sometimes, we want to make a number a percentage with JavaScript.

In this article, we’ll look at how to make a number a percentage with JavaScript.

How to make a number a percentage with JavaScript?

To make a number a percentage with JavaScript, we can get the quotient of 2 numbers and multiply that by 100.

For instance, we write

const number1 = 4.98;
const number2 = 5.98;

console.log(Math.floor((number1 / number2) * 100));

to divide number1 by number2 and multiply the quotient by 100.

Then we call Math.floor on the product to get the floor of it.

Conclusion

To make a number a percentage with JavaScript, we can get the quotient of 2 numbers and multiply that by 100.

Categories
JavaScript Answers

How to get 30 days prior to current date with JavaScript?

Sometimes, we want to get 30 days prior to current date with JavaScript.

In this article, we’ll look at how to get 30 days prior to current date with JavaScript.

How to get 30 days prior to current date with JavaScript?

To get 30 days prior to current date with JavaScript, we can use the setDate and getDate methods.

For instance, we write

const today = new Date();
const priorDate = new Date(new Date().setDate(today.getDate() - 30));

console.log(today);
console.log(priorDate);

to get the day of the month of today‘s date with getDate.

Then we subtract 30 from it and use the difference as the argument for setDate.

Then we copy the new date value by passing the returned Date object to the Date constructor.

Conclusion

To get 30 days prior to current date with JavaScript, we can use the setDate and getDate methods.

Categories
React Answers

How to simulate a button click in Jest and JavaScript?

Sometimes, we want to simulate a button click in Jest and JavaScript.

In this article, we’ll look at how to simulate a button click in Jest and JavaScript.

How to simulate a button click in Jest and JavaScript?

To simulate a button click in Jest and JavaScript, we call the Enzyme simulate method.

For instance, we write

import React from "react";
import { shallow } from "enzyme";
import Button from "./Button";

describe("Test Button component", () => {
  it("Test click event", () => {
    const mockCallBack = jest.fn();

    const button = shallow(<Button onClick={mockCallBack}>Ok!</Button>);
    button.find("button").simulate("click");
    expect(mockCallBack.mock.calls.length).toEqual(1);
  });
});

to call shallow to shallow mount the Button component.

Then we call find to find the button element and call simulate with 'click' to simulate a click on it.

Finally, we use expect to check what we expect.

Conclusion

To simulate a button click in Jest and JavaScript, we call the Enzyme simulate method.

Categories
JavaScript Answers

How to check if an array has duplicate values with JavaScript?

Sometimes, we want to check if an array has duplicate values with JavaScript.

In this article, we’ll look at how to check if an array has duplicate values with JavaScript.

How to check if an array has duplicate values with JavaScript?

To check if an array has duplicate values with JavaScript, we can use sets.

For instance, we write

const hasDuplicates = (array) => {
  return new Set(array).size !== array.length;
};

to convert array to a set with the Set constructor.

Then we check that its size isn’t the same as the array‘s length to see if it has any duplicates.

If it has duplicate values, then the set created from array won’t have the same size as the original array.

Conclusion

To check if an array has duplicate values with JavaScript, we can use sets.

Categories
JavaScript Answers

How to copy to clipboard in Node.js?

Sometimes, we want to copy to clipboard in Node.js.

In this article, we’ll look at how to copy to clipboard in Node.js.

How to copy to clipboard in Node.js?

To copy to clipboard in Node.js, we use the clipboardy library.

To install it, we run

npm install clipboardy

Then we write

const clipboardy = require('clipboardy');

clipboardy.writeSync('hello world');

clipboardy.readSync();

to call clipboardy.writeSync to write 'hello world' to the clipboard.

And we call readSync to read the content of the clipboard.

Conclusion

To copy to clipboard in Node.js, we use the clipboardy library.