JavaScript Coding Question – 1

  • Here, in the above code the line const result = […new Set(arr)] removes duplicate values from the array arr using a Set, which only allows unique values.
  • new Set(arr): Creates a new Set from the array. The Set will only keep unique values.
  • [...new Set(arr)]: The spread operator ... is used to convert the Set back into an array.

Finding number of Elements of an Array which falls between two values.

To find how many elements of an array fall between two values ‘a’ and ‘b’, we can use the ‘filter’ method. This method allows us to iterate over the array and apply a condition to each element, returning a new array with the elements that meet the condition. We can get the count of elements by using the length.

How to move current focus based on the data-testId in ReactJS with TypeScript.

To set the focus on an element with a specific ‘data-testid’ , we can use the ‘useEffect’ hook to find the element and then call its ‘focus’ method.

  • We are checking mainMenuElement ia an instance of ‘HTMLElement’ or not and it tells TypeScript that ‘mainMenuElement’ has the ‘focus’ method.
  • The ‘tabIndex=”0″ attribute is added to ensure that the ‘div’ can receive focus.