Return correct value from recursive indexOf in JavaScript?


A computer pattern or idea called recursion is present in many programming languages, including JavaScript. It is a feature used to build a function that repeatedly calls itself, but with a smaller input each time, until the intended outcome of the code is realized.

In this article, we will return the correct value from a recursive indexof() function in JavaScript. Let’s dive into the article for a better understanding.

Accurate value from the recursive indexof() function

The position of the first occurrence of a value in a string is returned by the indexOf() method. -1 is returned by the indexOf() method if the value cannot be found. The indexof() is a case sensitive.

Syntax

Following is the syntax for indexof()

indexOf(searchElement)
indexOf(searchElement, fromIndex)

Let’s look into the following examples for getting more idea on returning the correct value from a recursive indexof() in JavaScript.

Example

In the following example, we are running the script for getting the value of the recursive indexof(). In this case, we are going to get the index of element "c."

<!DOCTYPE html>
<html>
   <body style="background-color:#EAFAF1">
      <script>
         function search(array, value) {
            if (array[0] === value) {
               return 0;
            }
            if (array.length === 0) {
               return -1;
            }
            const result = search(array.slice(1), value);
            return result === -1 ? -1 : result + 1;
         }
         document.write("The indexof c was:" +
         search(["a", "b", "c", "d"], "c"),
         );
      </script>
   </body>
</html>

When the script gets executed, it will generate an output consisting of an index value "c" that is displayed on the webpage.

Example

Considering the following example, where we are running the script by declaring an array, getting the indexof() element "Vespa," which was not declared in the array, and returning the value of recursive indexof().

<!DOCTYPE html>
<html>
   <body style="background-color:#E8DAEF">
      <p id="tutorial"></p>
      <script>
         function search(arr, value, i = 0) {
            if (i >= arr.length) return -1;
            if (arr[i] === value) return i;
            return search(arr, value, i + 1);
         }
         let array = ["Raj", "Kamal", "Vikram", "Ravi"];
         document.getElementById("tutorial").innerHTML="The indexof Vespa was: " + (search(array, "Vespa"))
      </script>
   </body>
</html>

On running the above script, the output window will pop up, displaying the index of "Vespa" as "-1" displayed on the webpage. It was shown as "-1" because the value "Vespa" was not found for the index.

Example

Execute the below script and observe how we are going to get the correct value of recursive indexof().

<!DOCTYPE html>
<html>
   <body style="background-color:#CCCCFF">
      <p id="tutorial"></p>
      <script>
         const array = ['Bheem', 'Nayak', 'Ravi','Dora']
         const result = array.indexOf('Ravi');
         document.getElementById("tutorial").innerHTML=("The indexof() of required value was:" + result)
      </script>
   </body>
</html>
When the script gets executed, it will generate an output displaying the indexof() of the required value is "2," which gets displayed on the webpage.

Updated on: 21-Apr-2023

239 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements