Javascript How to scale Array, prevent errors from undefined - javascript

I am currently creating a quiz app in meteor. One of the key variables Im using is called currentQuestions which keeps track of the questionnumber of each quiz the user has taken. It is currently an array which updates (in a Meteor session) everytime the user clicks the next or back button on a particular quiz. for example:
currentQuestion = Session.get('currentQuestion') || [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
This works fine for a small amount of quizzes. What would you suggest however when I want to have a large amount of quizzes? Currently when I create more quizzes than I have previously defined in the currenQuestion Array the app crashes.
Possible solution I have thought of
(Ugly solutions) Create a for loop that fills up the currentQuestion array with a large amount of zero's at startup.
Insert a object into the mongoDB database that contains the currentquestion with the id of the quiz that's taken (so getting rid of the array entirely)
Hope you can help.

Option 2 is definitely the right one:
If you don't need to keep track of scores between sessions, you can just use a client-side-only database and store objects as you suggest and it will be only a couple of lines of new code for a much more flexible outcome.
This would then be very easy to convert into a persistent store as well: either add sign-in with Meteor's accounts package, add the current userId to each quiz object, and then only publish the quiz objects with the same userId as the logged-in user. Alternatively, you could even use the amplify package to store results between sessions in a given browser if you wanted to avoid user login entirely (you'd have to pull existing results out again on startup, but this is very easy).
The result would have all the reactivity of a Session variable built in, and would avoid the need to populate an array with an arbitrary and changing number of zeros - just check whether the a document with the given quiz Id is in the collection or not, and if not then create it.

You'll need to get rid of the array entirely. If you are going to keep track between sessions then you'll need to go with something like mongoDB. You could created something like a progress or completion model which can hold info about the quizzes they have taken and progress for each. Once you have that stores those by some userId related key.
Your dirty solution is not the worst since you'll know at start how many quizzes you'll have and can allocate the array then.

Related

Is there a way to import prewritten JSON data into MongoDB when a new user is created but also have it tied to the new user?

I'm building a studying tool that essentially consists of multiple flashcards (in the thousands). I would like to know if there is a unique way or method where the prewritten flashcards can be duplicated and tied to every user who signs up? Or perhaps having the data prewritten inside a JSON file and then upon user creation having it imported into MongoDB with a userID field that the data becomes unique to every individual user. The reason for this is because the flashcards will be using a spaced repetition algorithm and as a result I can't have one users results be carried over globally for every user. It would need to be unique to that user.
Thanks in advance for any help!
So far I've built the flashcard model and tied it to the algorithm however it updates globally across all users. Im havign a bit of trouble with breaking down some of the logic to make it unique to each user.

array or object which one is adapt for live chat using node js

I am learning about nodejs + express + socketio. I am making live chat for customer. I have successfully get user list in Admin page. currently Admin page show all user sending message but I need to show only selected user message from user list.
In this case I need one array or object which have all details about chat session but in my case details are coming from different events so I confused how to push the value.
When I search about array and object I found Arrays have order, objects don't but the statement is confusing me. upto now I can use object but I could not felt any difference between object and array except syntax.
Here I put two questions
Which one right for handle private chat (Array or object)?
How to push the data uniformly from different events for example after triggering three events my object or array should like below
chat={"name":"Bilal","mail":"test#test.com","socketid":"asgd","to":"John","message":"Hello!","ip":"192.168.1.4","time":"timestamp"};
Okay so first let me tell you there is a lot you still need to read up about nodejs and programming in general to start with this project or it will confuse you.
Coming back to your question, the distinction between array and object in terms of having order is that - In simple terms think of "having order" as being able to number the items and retrieve them using an index. This is possible with arrays but not with objects. For example you have an array a and object o, a[5] will give you the 6th element of a but o[5] doesn't make any sense. An object just holds pieces together in no specific order.
IMHO its best to communicate between methods using objects especially in nodejs. You also have the flexibility to use other complex structures when working with objects.

How to store user actions in MeteorJs using MongoDB?

I'm using Meteor JS for a project so inherently I'm using MongoDB. I'm storing a user's check in and out actions. I'm currently storing them as individual docs in the collection. Each action contains 3 fields; in or out, time of action and userid. Is the best way to go though? Should I just have one doc per members and then store each action in an array? Is there another way? I anticipate several hundred members, but hopefully several thousands of members in the future. Thanks.
From experience, I can say that storing records instead of arrays is a better choice in the long run.
As far as Meteor is concerned, its reactivity handles collection records, but not individual fields in arrays. In other words, if one element gets added to the checkins array of a user object, the entire user object needs to be synchronized with the clients. If you store records instead, only the newly added record will be sent by the publication.
As far as MongoDB is concerned, there is a document size limit of 16MB. Not sure how frequent your checkins and checkouts are, but if you store them in an array, you might run into that limitation at some point.
Records are also easier to access than arrays.
For more details, see MongoDB data modeling and Database modeling in Bulletproof Meteor.

Parse.com - How to relate whole WordList for all the users in parse.com

I am making a quiz game and using Parse.com as a backend , I have a WordListSet class with wordId, wordName and isWordUsed. isWordUsed is going to set true or false for the particular user if they have already used that word in the quiz
Now I dont know how to relate whole WordListSet to the UserClass, I did read docs but couldnt understanding the relation mapping thing.
Basically, What I am trying to achieve is say WordListSet has 100 Words for the quiz now whenever the new user is created it will have the whole WordListSet , as user keep on playing I want to keep on setting isWordUser to true so that it cant be reused again.
I need to have mapping with each user otherwise anyuser will once set the isWordUsed to true then other users wont be able to retrieve that word
Use an array they are much simpler than Relations and good for 100 relations.
Add an array to your PFUser subclass and then simply store pointers to the word object here's the Parse docs on one-to-many with Arrays: https://parse.com/docs/relations_guide#onetomany

Get pusherapp members count in channel without counting self

Pusherapp counts yourself when you subscribe to get a members count. Is there any way to not include yourself?
I have a preview view with a members in channel count. If I join the room in another tab I would like that number to go up.
The problem with getting the count, then minus one is that when you actually do join the number won't go up. It also counts other members who are looking at the preview and not where I would actually intentionally subscribe them. Any ideas?
There are 3 possible methods:
You can work out who you are in the members collection by comparing it to the members.me object. See: http://pusher.com/docs/client_api_guide/client_presence_channels#members_me
You can query Pusher's Web API to get a count of users. See:
http://pusher.com/docs/rest_api#method-get-users
Each user will have an ID so you can work out which one is the current user using that.
You could register a Presence WebHook so that your server is notified whenever a user joins or leaves a channel. In this scenario you may need to maintain your own count, but you can get the initial value using method 2, above.
There is no way right now to simply query the members and say "exclude me" as part of that query. So the only solution is to get the full collection (on the client or server) and implement some custom counting logic.

Categories