Categories
JavaScript Answers

How to Fix the ‘nodemon command is not recognized in terminal for node js server’ Error?

To fix the ‘nodemon command is not recognized in terminal for node js server’ error, we can install nodemon locally or install it locally and add a script into package.json to run the local version.

To install it locally, we can run:

npm install -g nodemon

with NPM or:

yarn global add nodemon

with Yarn.

And to add a local version, we run:

npm install nodemon

with NPM or:

yarn add nodemon

with Yarn.

Then we add:

"serve": "nodemon server.js"

into the 'scripts' second of package.json .

And then we run:

npm run serve

With Yarn, adding to the 'scripts' section is optional, and we can just run:

yarn run nodemon server.js

If we did add the script to package.json , we run:

yarn run serve
Categories
JavaScript Answers

How to Reorder Arrays with JavaScript?

We can reorder arrays with JavaScript with destructuring.

For instance, we can write:

const swapPositions = (array, a, b) => {
  [array[a], array[b]] = [array[b], array[a]]
}

const array = [1, 2, 3, 4, 5];
swapPositions(array, 0, 1);
console.log(array)

We create the swapPositions array with the array to reorder, and the a and b parameters which are the indexes of array items to swap.

In the function body, we swap the position of the elements with indexes a and b by putting them both in an array and then destructuring them.

This will swap the 2 array items in place.

Therefore, when we call swapPositions with array , 0, and 1, we get:

[2, 1, 3, 4, 5]

as a result.

Categories
JavaScript Answers

How to Detect When the Mouse Leaves the Window with JavaScript?

To detect when the mouse leaves the window with JavaScript, we can add the mouseleave event to document .

For instance, we can write:

document.addEventListener("mouseleave", (event) => {  
  if (event.clientY <= 0 || event.clientX <= 0 || (event.clientX >= window.innerWidth || event.clientY >= window.innerHeight)) {  
    console.log("I'm out");  
  }  
});

We add a mouseleave event listener with the addEventListener method.

Then in the event listener, we check if one of the following conditions are met:

  • event.clientY is less than or equal to 0
  • event.clientX is less than or equal to 0
  • event.clientX is bigger than or equal to window.innerWidth
  • event.clientY is bigger than or equal to window.innerHeight

If any of the conditions are true , then we know the mouse is out of the browser tab.

And therefore, “I'm out" is logged.

Categories
JavaScript Answers

How to Convert HH:MM:SS Time String to Seconds Only in JavaScript?

We can convert a HH:MM:SS time string to seconds only in JavaScript by converting the hours and minutes to seconds.

And then add the converted values to the seconds’ portion of the time.

For instance, we can write:

const hms = '02:04:33';
const [hours, minutes, seconds] = hms.split(':');
const totalSeconds = (+hours) * 60 * 60 + (+minutes) * 60 + (+seconds);
console.log(totalSeconds);

We have the hms string in HH:MM:SS format.

Then we split by the colons with the split method.

Next, we get the hours and convert it to a number with the + operator and multiply by 60* 60 to convert that to seconds.

Then we add that to the minutes converted to a number and multiplied by 60 to convert the minutes to seconds.

And then we add both to the seconds to get the totalSeconds .

As a result, totalSeconds is 7473.

Categories
JavaScript Answers

How to Get Trig Methods to Accept Degrees Instead of Radians in JavaScript?

To get the Math.sin, Math.cos, and Math.tan methods to use degrees instead of radians in JavaScript, we can convert the degree value accepted by Math.sin , Math.cos or Math.tan to radians.

For instance, we can write:

const toRadians = (angle) => {
  return angle * (Math.PI / 180);
}
console.log(Math.sin(toRadians(45)))
console.log(Math.cos(toRadians(45)))
console.log(Math.tan(toRadians(45)))

to create the toRadians function that converts the angle in degrees to radians by multiplying angle in degrees by Math.PI / 180 .

Then we call Math.sin , Math.cos , and Math.tan with toRadians(45) , which converts 45 degrees to radians.

And so we get:

0.7071067811865475
0.7071067811865476
0.9999999999999999

from the console log.