I am trying to serve random words to the user but only serve each one once (I am trying to build an app that serves words of graduating difficulty to students).

Đang xem: Cannot read property 'push' of undefined in js

I am trying to compare a used words array with the current array of words before sending out a word but I am returning an error when I try and push words to the used words array:

Uncaught TypeError: Cannot read property 'push' of undefined

Here is the code:

var moduleOneArray = <>;var usedModuleOneWords = <>;const showRandomWord = function(){ $.ajax({ type: 'GET', url: '/encoder/get_word/', data: {}, success: function (data) { for (i = 0; i
2 comments
share
save
hide
report
100% Upvoted
Log in or sign up to leave a comment
Log InSign Up
Sort by: best

*

level 1
· 1 yr. ago
You call the function with one parameter:

placeRandomModuleOneWord(moduleOneArray)But it wants two and the second is undefined.

const placeRandomModuleOneWord = function(moduleOneArray, usedModuleOneWords) Remove the second one from the function declaration, because within the function it will use the parameter instead of the variable defined in the global scope.

Also, this:

moduleOneArray == usedModuleOneWordswill never be true, look up comparing arrays.

You also define the two vars in global, then use the same names as function parameters, that will super confuse you and probably not work the way you intended.

2
Reply
Share
ReportSaveFollow
level 1
· 1 yr. ago

Hi! Almost sure that it's the issue here, but sorry if I appear to be wrong.

Xem thêm: Cách Sửa Lỗi Màn Hình Xoay, Sửa Lỗi Màn Hình Xoay Ngang

When you're calling the placeRandomModuleOneWord function from the showRandomWord function, you're only passing one argument. But the called function takes two of them, and since you didn't provide the second one at the call, it gets undefined.

Basically the code in the second function would look for the variables within its scope first. It will find them in the function's arguments, and take their value (the first one is the first array, and the second one is undefined, because you didn't define it at the call).

Maybe try calling it with both arguments, like *placeRandomModuleOneWord(moduleOneArray, usedModuleOneWords);*.

Or, don't assign any arguments to the second function at all (and no arguments at the function call then aswell), so the needed arrays will be get directly from the global scope.

Xem thêm: Cách Ướp Thịt Nướng Xiên Nướng Gồm Những Gì? Cách Làm Thịt Xiên Nướng Gồm Những Gì

1
Reply
Share
ReportSaveFollow

*

r/learnjavascript
This subdarkedeneurope.com is for anyone who wants to learn JavaScript or help others do so. Questions and posts about frontend development in general are welcome, as are all posts pertaining to JavaScript on the backend.
196k

Members

73

Online

Created Apr 4, 2012
Join

*
*
*
*

*

Top posts february 25th 2021Top posts of february, 2021Top posts 2021
helpdarkedeneurope.com coinsdarkedeneurope.com premium
aboutcareerspressadvertiseblogTermsContent policyPrivacy policyMod policy
Back to Top
Advertisement

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *