Categories
JavaScript Answers

How to store a function with jQuery data() method?

Sometimes, we want to store a function with jQuery data() method.

In this article, we’ll look at how to store a function with jQuery data() method.

How to store a function with jQuery data() method?

To store a function with jQuery data() method, we can create custom events and set our function as the event handler function.

For instance, we write:

<div id='foo'>

</div>

to add a div.

Then we write:

$('#foo').on('say', (e, arg) => {
  console.log('Foo says', arg);
});

$('#foo').trigger('say', 'hello');

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

Then we call on with the event name and event handler function respectively.

We get the value we triggered the event with from the arg parameter.

Then we trigger the event with the trigger function with the event name and data we want to send with our event.

Conclusion

To store a function with jQuery data() method, we can create custom events and set our function as the event handler function.

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.