Categories
JavaScript Answers

How to Detect if a User is Scrolling with JavaScript?

To detect if a user is scrolling with JavaScript, we can watch for scroll events by adding event handlers.

For instance, we can write:

let userHasScrolled = false;  
window.onscroll = (e) => {  
  userHasScrolled = true;  
}

to add the userHasScrolled variable.

Then we set the window.onscroll property to a function that runs when we scroll.

Also, we can use the addEventListener method to add the scroll event listener.

For instance, we can write:

let userHasScrolled = false;  
window.addEventListener('scroll', (e) => {  
  userHasScrolled = true;  
})

We call window.addEventListener with 'scroll' to listen to the scroll event.

The 2nd argument is the event handler that’s run when the scroll event is emitted when we scroll.

Categories
JavaScript Answers

How to Run Code After jQuery Ajax Calls Regardless of the Result?

To run code after jQuery Ajax calls regardless of the result, we can call the always method.

For instance, we can write:

$.ajax({  
  url: 'https://yesno.wtf/api',  
  dataType: 'json',  
  type: 'GET'  
}).then((processedDataOrXHRWrapper, textStatus, xhrWrapperOrErrorThrown) => {  
  console.log('success')  
}).always(() => {  
  console.log("always run");  
});

We call ajax with an object that has the url of the request.

dataType is set to 'json' to make a JSON request.

type is set to 'GET' to make a GET request.

Then we call then with a callback to get the response of the request when it’s successful.

And then we call always with a callback that runs regardless of the request result.

Categories
JavaScript Answers

How to Declare a JavaScript Array with Objects Inside?

To declare a JavaScript array with objects inside, we can declare an empty array then use a loop to push the objects into an array.

For instance, we can write:

const n = 100;
const sample = [];
for (let i = 0; i < n; i++) {
  sample.push({});
}

to declare the sample array.

Then we use a for loop to loop from 0 to n and in the loop body, we call sample.push with an empty object to add an empty object into an array n times.

Categories
JavaScript Answers

How to Do a Case Insensitive Replace All with a JavaScript String?

Sometimes, we want to do a Case insensitive replace all with JavaScript string.

In this article, we’ll look at how to do a Case insensitive replace all with JavaScript string.

Do a Case Insensitive Replace All with a JavaScript String

To do a Case insensitive replace all with JavaScript string, we can use a regex to do the replacement.

For instance, we can write:

const str = 'This iS IIS'.replace(/is/ig, 'as');  
console.log(str)

We call replace on a string with the i and g flags to search for all instances of 'is' in a case insensitive manner.

And we replace each instance with 'as' .

The i flag lets us search for all instances of the pattern in a case-insensitive manner.

The g flag lets us search for all instances of the regex pattern.

As a result, str is 'Thas as Ias’ .

Conclusion

To do a Case insensitive replace all with JavaScript string, we can use a regex to do the replacement.

Categories
JavaScript Answers

How to Get the Element with the Highest Number of Occurrences in a JavaScript Array?

To get the element with the highest number of occurrences in a JavaScript array, we can use some array methods to get the number of occurrences of each item and sort the values to get the highest value from the list.

For instance, we can write:

const mode = (arr) => {
  return arr.sort((a, b) =>
    arr.filter(v => v === a).length -
    arr.filter(v => v === b).length
  ).pop();
}

console.log(mode(['pear', 'apple', 'orange', 'apple']));

to create the mode function that takes an array.

In the function, we call sort with a callback that takes the a and b entries from arr that are being iterated through, then we return the highest number of occurrences by using the filter method to get the number of occurrences of each item a and b .

We call filter to return an array with all a ‘s and b ‘s and get the length of it.

Then we call pop on the returned array to return the last item from the array returned by sort , which is the item in arr with the highest number of occurrences.

Therefore, the console log should log 'apple' as a result.