Categories
JavaScript Answers

How to write an if statement within a JavaScript object when setting an attribute?

Sometimes, we want to write an if statement within a JavaScript object when setting an attribute.

In this article, we’ll look at how to write an if statement within a JavaScript object when setting an attribute.

How to write an if statement within a JavaScript object when setting an attribute?

To write an if statement within a JavaScript object when setting an attribute, we can use the ternary operator.

For instance, we write:

const testBoolean = true;

const object = {
  attributeOne: "attributeOne",
  attributeTwo: testBoolean ? "attributeTwo" : "attributeTwoToo"
}
console.log(object)

We set testBoolean to true.

And we set object.attributeTwi to 'attributeTwo' is testBoolean is true and 'attributeTwoToo' otherwise.

As a result, we see that object is {attributeOne: 'attributeOne', attributeTwo: 'attributeTwo'}.

Conclusion

To write an if statement within a JavaScript object when setting an attribute, we can use the ternary operator.

Categories
JavaScript Answers

How to chain selectors in jQuery and JavaScript?

Sometimes, we want to chain selectors in jQuery and JavaScript.

In this article, we’ll look at how to chain selectors in jQuery and JavaScript.

How to chain selectors in jQuery and JavaScript?

To chain selectors in jQuery and JavaScript, we can use the find method.

For instance, we write:

<div>
  <p>
    foo
  </p>
  <p>
    bar
  </p>
  <p>
    baz
  </p>
</div>

to add a div with p elements inside.

Then we write:

console.log($('div').find(':last').text())

to select the div with $('div').

Then we call find(':last') to find the last child of the div.

And we call text to get the text content of the last child of the div.

Therefore, we see that 'baz' is logged.

Conclusion

To chain selectors in jQuery and JavaScript, we can use the find method.

Categories
JavaScript Answers

How to find the location closest location in (lat, long) that is closes to the given (lat, long) position with JavaScript?

Sometimes, we want to find the location closest location in (lat, long) that is closes to the given (lat, long) position with JavaScript.

In this article, we’ll look at how to find the location closest location in (lat, long) that is closes to the given (lat, long) position with JavaScript.

How to find the location closest location in (lat, long) that is closes to the given (lat, long) position with JavaScript?

To find the location closest location in (lat, long) that is closes to the given (lat, long) position with JavaScript, we can use the Haversine function.

For instance, we write:

const deg2rad = (deg) => {
  return deg * (Math.PI / 180)
}

const getDistanceFromLatLonInKm = (lat1, lon1, lat2, lon2) => {
  const R = 6371;
  const dLat = deg2rad(lat2 - lat1);
  const dLon = deg2rad(lon2 - lon1);
  const a =
    Math.sin(dLat / 2) ** 2 +
    (Math.cos(deg2rad(lat1)) ** 2) *
    (Math.sin(dLon / 2) ** 2);
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  const d = R * c;
  return d;
}

const cities = [
  ["city1", 10, 50],
  ["city2", 40, 60],
  ["city3", 25, 10],
  ["city4", 2, 2]
];

const [closestCity] = cities.sort((c1, c2) => {
  const [, lat1, lon1] = c1
  const [, lat2, lon2] = c2
  return getDistanceFromLatLonInKm(0, 0, lat1, lon1) - getDistanceFromLatLonInKm(0, 0, lat2, lon2)
})
console.log(closestCity)

We define the deg2rad function to convert degrees to radians.

Then we define the getDistanceFromLatLonInKm to calculate the distance between 2 locations with the Haversine function.

We convert the latitude and longitude differences to radians.

Then we calculate a with the latitude, latitude difference and longitude difference with part of the Haversine formula.

Next, we calculate c with a, which is another part of the Haversine formula.

And then we put them together with R * c and returning it.

Then we sort the cities array by the distance closest to (0, 0) with sort and get the closest one with destructuring.

Therefore, closestCity is ['city4', 2, 2].

Conclusion

To find the location closest location in (lat, long) that is closes to the given (lat, long) position with JavaScript, we can use the Haversine function.

Categories
JavaScript Answers

How to check if a user has webcam or not using JavaScript?

Sometimes, we want to check if a user has webcam or not using JavaScript.

In this article, we’ll look at how to check if a user has webcam or not using JavaScript.

How to check if a user has webcam or not using JavaScript?

To check if a user has webcam or not using JavaScript, we can use the navigation.getUserMedia method.

For instance, we write:

navigator.getUserMedia({
  video: true
}, () => {
  console.log('has webcam')
}, () => {
  console.log('no webcam')
});

to call it with { video: true } and callbacks that’s run if a webcam is present and if the webcam isn’t present respectively.

Therefore, if the user’s device has a webcam, then 'has webcam' is logged.

Otherwise, 'no webcam' is logged.

Conclusion

To check if a user has webcam or not using JavaScript, we can use the navigation.getUserMedia method.

Categories
JavaScript Answers

How to add a popup description Box using onmouseover with JavaScript?

Sometimes, we want to add a popup description Box using onmouseover with JavaScript.

In this article, we’ll look at how to add a popup description Box using onmouseover with JavaScript.

How to add a popup description Box using onmouseover with JavaScript?

To add a popup description Box using onmouseover with JavaScript, we can set the display CSS property of the popup in the onmouseover method of its parent element.

For instance, we write:

<div id="parent">
  <div id="popup" style="display: none">description text here</div>
</div>

to add the parent with the popup div inside.

Then we write:

#parent {
  width: 200px;
  height: 200px
}

#parent #popup {
  display: none;
}

#parent:hover #popup {
  display: block;
}

to add some styles to the divs.

We hide the popup div by default.

Next, we write:

const e = document.getElementById('parent');
e.onmouseover = () => {
  document.getElementById('popup').style.display = 'block';
}
e.onmouseout = () => {
  document.getElementById('popup').style.display = 'none';
}

to select the parent div with document.getElementById.

Then we set e.onmouseover to a function that sets the display CSS property of the popup div to 'block'.

Likewise, we set e.onmouseout to a function that sets the display CSS property of the popup div to 'none'.

Conclusion

To add a popup description Box using onmouseover with JavaScript, we can set the display CSS property of the popup in the onmouseover method of its parent element.