Jenelee Alba
Jenelee Alba

Reputation: 29

IndexOf Javascript alternative

I'm trying to do an indexof function. but we're not allowed to use it, or any build-in function in javascript. so i create my own. I upload it in our tester but there's only one condition that I didn't met. Thanks!

here's my code :

function indexofmyversion(searchChar,index) {
var a="";
for(var i=0; i<=searchChar.length; i++){
   if(searchChar[i] == index){
       return i;
    } 
} return -1; }

Tester Picture Result

Upvotes: 1

Views: 2562

Answers (1)

Darlyn
Darlyn

Reputation: 4938

The test failed due to fact that the searching should start from index 4 , simply add another parameter as required

function indexofmyversion(searchChar,index, fromIndex) {
var a="";
for(var i=fromIndex; i<searchChar.length; i++){
   if(searchChar[i] == index){
       return i;
    } 
} return -1; }

Upvotes: 5

Related Questions