Are string saved to arrays permanently saved? - javascript

I'm an 11-year-old learning how to code Javascript, I recently started 1 and a half months ago. I am making a login/register system and am trying to make the register part now. I was wondering if adding a string/number/boolean to an array stays there forever (even after refresh). I would've used cookies for this, but it is not safe.
I am currently using HTML5, CSS3, Javascript, and eventually will be using Jquery. NOTE: The time period does not have to be forever. Does any know the answer to this, please reply?
P.S. ( iff you mention PHP for this there would be no point, I don't understand it yet. :/ )

The answer is no. Note that anytime you reload a browser page, your JavaScript also reloads with the page.
If you want you data to persist, use a database.

a string or whatever you have in an array will not stay in it forever simply because an array is a data structure which is not persistent. Since you have it in your intention to use more advanced technologies in the near future and you are not comfortable with the cookie because of the obvious security risks. I suggest you use a localStorage to implement the registration feature for your app. Of course you can achieve the same with a sessionStorage, however whatever you save in a sessionStorage will be wiped off when you close the tab which can be handy in the implementation of you login and logout feature for now. Whenever you upgrade the applications to more sophisticated technologies you will have a better way to handle those features.
PS: it might be a little advanced for you right now but when you are more comfortable writing Javascript maybe you will like to checkout IndexDB then NodeJs very much later.
#cheers #dontRelent

If you want to create login functionality, you will have to start learning some server-side technologies.
When I was finished (kind of) with learning JavaScript i moved on to NodeJS and MongoDB. NodeJS allows you use JavaScript on the server-side and MongoDB is a database program where you could store data.
Using those two you would create a user model, store it and then check if the user is logged in and etc.
There are many choices to go with when learning back-end, I went with NodeJS because as I already knew JavaScript it was easier to use the same language for the back-end. That and it was part of the Udemy course I had.
Good luck.

It depends on where you're storing that array. If you want to make a functional login/register on a web-page, you will likely need to use a database such as PostgreSQL.
However, if you want to just create a simple program to run on the command line, the username or password strings you enter in will not remain in memory if you terminate the program. If you terminate your program, it will delete the strings added to your array.

Related

Persisting data with js for a small web project without a database

I recently started developing my first (rather crappy) webpage. It is for my commune that just displays who has to clean the kitchen on a given day and a picture of him. Now I would also like to make a little rating system, where you can give a rating between 1 and 5 starts. Is there any way to persist the rating without using a database? Could I, for example, just save it to a textfile that i read to and write from?
edit:
The website is hosted on an apache server
The short answer is no.
To store anything on the server, you're going to need to learn a server-side language, like PHP, C#, or Java. Any server framework will already have stuff built in for working with a database, so if you go and reinvent your own, you'll just be creating more work for yourself. If I were you, I'd start with some simple tutorials for your server language of choice.
Also consider that even a simple rating system isn't as trivial as you might think. Some things you may want:
Authentication: should everyone in the world be able to access your site?
Permissions: can anyone update any rating?

Back-end solution for pure-Javascript site with link sharing

I'm developer-beginner and I would like to do simple card-sorting site.
hundreds of cards with plain text
no log-ins, it must be as simple for user as possible
few dozens users per day
final state of sorting should be saved under unique link, like site.com/q56we1cfuz4 or site.com/link.php?unique=q56we1cfuz4
User sorts cards as he/she wishes and the result is saved into browser sessions storage (few kb of data in JSON or so). So far, so good.
Question:
As I know only Javascript:
If I need to share the state of the page (data in session storage) with some unique link, is something like Firebase.com good solution for such kind of back-end? Or some simple DB with help od Node.js?
In order to "catch" the link when someone click at unique URL (site.com/link.php?unique=q56we1cfuz4), I still need some server-side script which will ask the DB, like PHP header redirect, right?
Your questions are a little fuzzy, no problem tho. You are just getting into web dev, so there's a lot to wrap your head around and all of the options can be pretty confusing. Some people will complain about opinionated answers, and I'm not going to claim to be objective here, but here are a few tips that I think will get you pointed in a better direction...
Firstly, yes - firebase is a good solution for you to try working with. Aside from the fact that it will give you the db/storage features you need, it's a realtime db, which will give you a lot more than just storage in the long run, and will better equip you for the future web development trends. The firebase API is (mostly) designed to work asynchronously, so from a javascript perspective, it falls right in line with the kind of code you'll end up learning to write.
Re: the other aspect of your question - server-side - check out nodeJS. It's basically a server-side javascript platform that will allow you to use the same skills you're learning to write client-side code for the server. Also check out expressJS, a nodeJS package that provides you the http-server, and allows you to handle dynamic urls, etc. - the bits you were thinking about when you made a reference to PHP.
Hopefully this will help you focus on a few specific tools to familiarize yourself with as you learn web development, rather than having to struggle with everything from new languages, platforms, and way too many libraries, frameworks and techniques to wrap your head around. Good luck!

Is a database required for a "quiz" type of game?

I don't know much about databases, I've been asking a few questions about them lately to get a better understanding but I'm still a bit confused about what does and doesn't need one.
I'm making a simple application using HTML/CSS/JavaScript, it has a few quizzes and "tutorials" targeted towards children. I don't want the next tutorial/quiz to be unlocked until the previous one is completed.
So for that would I need a database so that it "saves" when one is completed? I don't need to save scores or anything like that, they just get to move on once they get a passing score.
Any other requirements such as saving to a profile or needing to persist between sessions (e.g. changing of device)?
Browsers have localStorage APIs now which allow you to save a lot of the data (and keep it for a set duration of time). There are also good'ol'fashioned cookies which allow you save pieces of information as well.
Keep in mind that both of the above mandate the user use the same browser and allow these mechanisms. Obviously using "private"/"incognito" browsing would also affect saving status.
It's up to what you feel the requirements are.
EDIT Just saw your mention of a mobile app. If you're planning on allowing the experience to transcend devices, you'll need a database. otherwise, you'll be relying heavily on if they use cross-device sync (like Chrome and Firefox do with bookmarks, passwords, etc.)
If you don't mind that people can do a "view source" on the webpage or use every browsers' developer tools to find out the answers or move on to the next tutorial or quiz, then you can use cookies to store the user's status. Or you can use the preferable Web Storage API.
You might want to look at Firebase. Using just simple JavaScript on the web browser, you can have users with logins (or just allow them to login via Facebook or other services) very easily. And then you can store and retrieve data very easily as well, like quizzes, tutorials and results. This way nobody can see the answers even if they're adept at analyzing the webpage.
When you don't use database, before any check, you have to load all data in your static page.
So My sloution: store students situation in a cookie. On each page check cookie status and then use Jquery remove() to remove (Client-side) those parts of page that he/she can not access.
EDIT
This wont work when JavaScript is disabled.
There seems to be a lot of ideas but no clarifying on the database subject.
TL;DR is: No.
Now for the specifics. A database is nothing more than a way to store information. While traditional "SQL" databases (it is pronounced "Sequel" as in "My Sequel" for MySQL) have concepts of tables, where you define columns with items to store and saves each row with its value, much like an Excel file, some databases like Redis store key-value pairs and others lide MongoDB store JavaScript Objects.
You can store information in the source code (As Variables possibly) or in a file. A database is a way to organize that information.
With that said, in your case, you probably need a backend or an API. An API is basically a means of communication with a server through AJAX (JavaScript in the browser asks for stuff). That would be your way to retrieve information from the server as needed, so that users wouldn't see the answers before they answer.
With that out of the way, there are some options. FireBase (As noted on other answer) and AppBase are easy ways to integrate this concept with little effort. But they tie you and your information to their system, and they are mostly targeting more resource intensive apps.
Since you are using JS and seem to be enjoying your learning experience, I would suggest you consider suing NodeJS and defining the data as either a JSON file or a variable in JS. You keep working on your problem but add options and get to learn some stuff.
If you decide to integrate a database and possibly do some neat stuff, you have most of the groundwork done already.
If NodeJS picks your interest, Mean.IO and KrakenJS are, in my opinion, the best places to start, though they may both seem overkill in your specific case.
Do consider though: A database is just a small possible piece in a puzzle, and it's mostly a horrible way to name some of the software that tries to organize your information. Consider first if you need to organize information, and what and how do you need to organize, then start thinking if databases are the best way to organize it.

Persist JavaScript values between pages

I am trying to create a quiz on a web-page using HTML and JavaScript. Each question (having 4 options to choose from) is a seperate html file but uses the same JavaScript file to evaluate whether the choice entered is right or wrong.
However, I’m struggling to find a way to keep track of the number of correct answers provided by the users to calculate his/her score.
Is there a method to create static or static-like variables in JavaScript? Solutions involving cookies are also appreciated.
One possible solution is to load the HTML from your question file into a parent page, but have the whole quiz on that one page. That way, you still get the flexibility of having different pages for each question, but to the browser they are all one page. In addition, any variables created or traked in Javascript will persist throughout the entire quiz.
This is fairly easy to do with Jquery's .load function. You have a div in the middle of your page which loads its content from whichever HTML page you would have navigated to.
Do keep in mind that it is trivially easy for me to go into Javascript and change the number of correct answers. In fact, that problem exists for any client side solution. Should this be for more than fun, and accuracy is important, you may want to send results back to your server after each question. That way, no matter how you implement the quiz, results are persisted back on your end.
Have a look at http://www.electrictoolbox.com/jquery-cookies/
This allows you to easily set and read cookies.
You can keep the data in cookie. however an user may change the cookie and produce better result. Using session is a better choice in this scenario. because whatever you store in client side is unsafe.
However It is better to get the questions in json format with xhr and display them in browser and keep the track in memory
If you are developing a HTML5 application you may wish to investigate DOM storage facilities such as localStorage and sessionStorage: https://developer.mozilla.org/en-US/docs/DOM/Storage
Here are some useful resources / info:
http://ejohn.org/blog/dom-storage/
http://viralpatel.net/blogs/introduction-html5-domstorage-api-example/
http://msdn.microsoft.com/en-us/library/cc197062(v=vs.85).aspx
These days I would focus on making this a one-page app rather than relying on page loads. This not only has the advantage of solving your problem, it also means a more responsive, faster experience for the user.
If you must use page transitions, and you're happy to work with modern browsers only, look into localStorage. Far easier to use and more flexible than cookies - it works just like a serialised JavaSCript object.

WebSite Javascript Search Engine

I'm looking for any javascript library that i can use to search content on my website, i have came across quiet a few but mostly they require the use of a database to store indexes for optimizing search queries, but i only need a database free search engine built in with javascript. can anyone direct me to the right location(url) where i can download it and install it on my website, which is build on a cakephp framework. Was thinking of a search engine that could index every new page a include in my website maybe once a night and then when i search it should output the search results on the separate page and with links to the actual pages where the keyword was taken from.
Happy new year and have a splendid year ahead..
I bet your mistaken about JavaScript. In order to search, you will be needing records coming from the users which will be stored in the database so basically, you will be dealing with server side languages not JavaScript. JavaScript is only used for client-side which doesn't have to do anything with the database (Not unless your using Node.js).
Both JavaScript and the browsers have come a long way. You could use Lunr or search-index. Both can run in the browser. For search-index you use localStorage as the index. That means your data is stored per domain, in the browser. Nothing to install, nothing to maintain. And low server requirement since it's all happening on the client side.
Lunr is more mature and quicker to get up to speed, but search-index is maybe more feature rich?

Categories