8

So che ci sono un sacco di domande relative a questo errore e ho controllato la maggior parte di loro e nessuno mi aiuta a risolvere il mio problema. (Che sembra così facile da mettere a punto ...)Javascript Uncaught TypeError: Impossibile leggere la proprietà '0' di undefined

Ho un array (che è vuoto AAT prima):

var words = []; 

E la mia funzione hasLetter, controlla se troviamo una lettera (oggetto) in array (che chiamo qui: d) words.

function hasLetter(letter,d){ 

// if words[0] not null should return object of letter "a", here we getting 
// the index of the letter (since ascii of "a" is 97, I substract 97) 
var ascii = letter.charCodeAt(0)-97; 

//Trying to not get an error with this but still creates an err 
if(typeof d[ascii ] !== "undefined" && d[ascii ] !== null && d[ascii ].length > 0){ 
    if(d[ascii].letter == letter){ 
     return true; 
    } 
} 
return false; } 

e ho una funzione chiamata addLetter che controlla se hasLetter restituisce true/false e quindi crea o no di conseguenza un nuovo nodo.

function addLetter(letter,d){ 
var ascii = letter.charCodeAt(0)-97; 
if(!hasLetter(letter,d)){ 
    document.write("This letter" + letter + " hasn't been found in words."); 
    d[ascii] = new Node(letter); 
} 
    document.write("This letter " + letter + " already exists in words."); 
    document.write(d[ascii].letter); 

}

e se provo:

addLetter("a",words); 

restituisce:

Uncaught TypeError: Cannot read property '0' of undefined 

non so cosa fare per dire "se è indefinita allora don guardate dentro o qualcosa del genere ...

Grazie

+0

Se si desidera trovare una lettera in serie, quindi (words.indexOf (lettera)> 0) restituirà se la lettera è presente o meno. – mohamedrias

+0

Non è possibile eseguire 'hasLetter (" a ", words []);', dovrebbe essere 'hasLetter (" a ", words);' – theonlygusti

+0

@ mohamedrias no non lo farà. – theonlygusti

risposta

9

L'errore è qui:

hasLetter("a",words[]); 

Stai passando il primo articolo di words, anziché l'array.

Invece, passare la matrice alla funzione:

hasLetter("a",words); 

Problema risolto!


Ecco una ripartizione di ciò che il problema era:

Sto indovinando nel tuo browser (Chrome genera un errore diverso), words[] == words[0], in modo che quando si chiama hasLetter("a",words[]);, in realtà si sta chiamando hasLetter("a",words[0]);. Quindi, in sostanza, stai passando il primo elemento di parole alla tua funzione, non l'array nel suo complesso.

Ovviamente, poiché words è solo un array vuoto, words[0] è undefined. Pertanto, la vostra chiamata di funzione è in realtà:

hasLetter("a", undefined); 

il che significa che, quando si tenta di accedere d[ascii], in realtà si sta tentando di accedere undefined[0], da qui l'errore.

+0

Sì, esatto, ho fallito la mia chiamata haLetter aggiungendo "[]", ma il mio errore in realtà proveniva da un'altra funzione (bene è venuto da questa, ma dopo aver chiamato un altro). Creerò un nuovo thread (o dovrei aggiornare questo?) Ma grazie per aver spiegato questo, mi ha aiutato :) –

+0

OK, sì, questo ha senso; Ero confuso dall'errore che stavi ottenendo, mi sarei aspettato che fosse più simile a "Inaspettato]". Comunque grazie! Se voti questa risposta, avrò 2k di rettifica! – theonlygusti

2

Non c'è errore quando uso il codice,

ma sto chiamando il metodo hasLetter in questo modo:

hasLetter("a",words); 
0

Non credo che sia necessario il [] nella chiamata di funzione. Qual è l'output di hasLetter("a",words);?

Problemi correlati