Why do I get the statement: "Indice.push is not s Function"?

Asked

Viewed 75 times

0

I created a simple javascript code that adds the index of the highest values to a list, however I don’t understand why I can’t use the function. push() in this code:

Notice the line 12

1    var list=[1,7,8,9,3,6,9];
2    var contador=0;
3    var primeiro=list[0];
4    var indice=[];
5    function maior(){
6        while (contador<list.length){
7            if (primeiro<list[contador]){
8                primeiro=list[contador];
9                indice=list[contador];
10           }
11           else if (primeiro==list[contador]){
12               indice.push(contador);
13           }
14           contador=contador+1;
15           console.log("indice: #"+contador+" numero:"+list[contador-1]);
16       }
17       console.log("O maior número é: "+primeiro);
18       console.log("Os maiores numeros estão na posição: "+indice);
19   }  
20   console.log("A lista tem: "+list.length+" números");
21   maior();

I know other ways to write it and solve the problem, but I want to understand why I get the message from that function. push() cannot be used, so I can improve my knowledge. Any other tips will also be welcome.

  • 2

    What line 9 does?

  • If the comparison number (first), is the largest, it adds the index of that number to the list; however I turned it into another variable instead of adding that number to the list, thus losing the list function and the method. push!!!

  • Thank you very much!

1 answer

0


You are assigning the variable indice on line 9 an integer value, you have to add the value with push on line 9 as well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.