Open In App

How to search an element without using any loops in Node.js ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The setInterval() method repeats or re-schedules the given function at every given time-interval. It is somewhat like window.setInterval() Method of JavaScript API, however, a string of code can’t be passed to get it executed.

Syntax:

setInterval(timerFunction, millisecondsTime);

Parameter: It accepts two parameters which are mentioned above and described below:

  • timerFunction <function>: It is the function to be executed.
  • millisecondsTime <Time>: It indicates a period of time between each execution.

The clearInterval() method is used to stop the next schedule code execution. It is somewhat like window.clearInterval() Method of JavaScript API, however a string of code can’t be passed to get it executed.

Syntax:

clearInterval(intervalVar);

Parameter: It accepts a single parameter which is mentioned above and described below:

  • intervalVar <function>: It is the function name that is needed to stop executing for next interval.

Examples:

Input: Array = [ 46, 55, 2, 100, 0, 500 ]
Search Element = 0

Output: Element 0 found at index 4


Input: Array = [8, 9, 2, 7, 18, 5, 25]
Search Element = 500

Output: Element 500 not found in Array.

Approach: The sorting requires visiting each element and then performing some operations, which requires for loop to visit those elements.

Now here, we can use setInterval() method to visit all those elements and perform those operations.

The below code illustrates the above approach in JavaScript Language.

Example 1: File Name: Index.js




// Node.js program to search an element 
// without using any loops provided Array
  
const arr = [46, 55, 2, 100, 0, 500];
const l = arr.length;
var j = 0;
  
// Element to Search
var srchElement = 0;
  
// setInterval for looping purpose 
var myVar1 = setInterval(myTimer1, 1);
  
function myTimer1() {
    if (arr[j] == srchElement) {
  
        // Clear interval as required 
        // element is found 
        clearInterval(myVar1);
  
        // Printing found element
        console.log("Element", srchElement, 
            "found at index", arr.indexOf(arr[j]));
    }
  
    j++;
  
    if (j == l) {
        // Clear interval as element
        // not found in array
        clearInterval(myVar1);
  
        // Printing that element not found
        console.log("Element", srchElement, 
                    "not found in Array.");
    }


Run index.js file either on the online compiler or follow the following:

node index.js

Output:

Element 0 found at index 4

Example 2: File Name: index.js




// Node.js program to search an element 
// without using any loops provided Array
  
const arr = [8, 9, 2, 7, 18, 5, 25];
const l = arr.length;
var j = 0;
  
// Element to Search
var srchElement = 50;
  
// setInterval for looping purpose 
var myVar1 = setInterval(myTimer1, 1);
  
function myTimer1() {
  
    if (arr[j] == srchElement) {
          
        // Clear interval as required
        // element is found 
        clearInterval(myVar1);
  
        // Printing found element
        console.log("Element", srchElement, 
            "found at index", arr.indexOf(arr[j]));
    }
  
    j++;
  
    if (j == l) {
  
        // clear interval as element not
        // found in array
        clearInterval(myVar1);
  
        // Printing that element not found
        console.log("Element", srchElement, 
                "not found in Array.");
    }


Run index.js file either on the online compiler or follow the following:

node index.js

Output:

Element 50 not found in Array.



Last Updated : 09 Sep, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads