Sometimes I think that I can't code, I can only google. (c)me
I'm working on a database of immigration ways to all countries that called immi.earth. It's a huge project and for some reason, I made a decision to create a service that helps you to know how to move to the USA (at first, then I'll add other countries). About reasons, I'll write later.
There will be a quiz, after that you will know your way.
Today I decided to document my code process. If you want to know about the reasons to document your any process, you can watch this video (I really recommend to watch this video).
My goal for today was to create switching questions and to get user's answer for the quiz.
To be honest, I was stuck for some days and didn't realize how to make a little quiz despite a lot of guides on the Internet, but I pulled yourself together and start working on it.
1. I did a little research on how people create quizzes. Here are two links that helped me: first, second. I analyzed these codes and understood how my code will work. I wrote down into my notebook code logic.
2. When I started to code, an idea to write code tips for me came to my mind.
First code tip: firstly, write all functions that will be in your code and describe all these functions in comments.
Second code tip: don't forget to write comments.
I did it. I wrote all functions that I needed today.
3. I created an array of fake questions.
4. Added elements and onclick event to buttons on the html file.
5. I back to the js file. So, one of the rules of coding (and life) is to break big goals (tasks) into small easily pieces.
I needed questions to disappear and new questions appear when I click on the buttons.
At first, I decided to show just first array element.
6. The code didn't work. :c
When your code doesn't work, check your code for typos. I'm very inattentive and 800000000% of my code errors because of my inattentive (no).
So, did you notice this mistake?
7. It works, yeah! My life is becoming better, haha (no). (yes)
8. I wanted the questions to slide. I changed nextQuestion() and called this function in answer(). It didn't work. I don't know why, but it stopped on the second question. If you know why — tell me. Also, I improved the name of the function (here was a typo).
9. I changed n+1 to n++.
And it worked!!!!!!! But, when all the questions ended, "undefined" appeared. So, I needed to make a limit for button clicks.
10. I do some actions randomly when don't know how to do something. So, when I have a lot of tabs on my browser and want to close some of them at the same time, I choose the tabs with Shift. Then I try to guess which of the shortcuts close chosen tabs... Usually, I open a new tab and open something else, but I always find needed keys combination without googling!
This time I randomly tried "while" and it didn't work.
11. I understood why it didn't work and changed the symbol > on <.
After that, it worked, but when I clicked a button, the program showed the last question.
12. I understood that "while" doesn't my friend and replace it on "if".
It started to work! But there was a problem, the second question appeared after two button clicks (I still don't how to improve it). So, it didn't matter, because I achieved the goal — questions replaced each other.
13. The next goal was to get value from buttons to filter visas. I came back to the HTML file and added values.
Don't pay attention that 0 is false and 1 is true.
14. I wrote the wrong function. Cool!
15. I googled how to get value or id and found this. I made something with the code.
It didn't work.
16. I am never afraid when something doesn't work. So, I decided to make buttonValue simplified and compare id. It didn't work.
17. I googled again and found this amazing question on StackOverflow. I improved my code.
It looks much better.
AND IT WORKSSSS!
18. But I needed to get value, not id.
19. The next step is to collect answers. I created an array allAnswers to collect all answers and push them in the function answer(). Then I showed answers in the console.
That is all, I did what I planned! Thanks for reading!