├── README.md ├── favicon.png ├── LICENSE ├── 0_comments.js ├── statistics.html ├── reviewTest.html ├── chapter1.html ├── chapter2.html ├── chapter3.html ├── statistics.js ├── 0_comments.html ├── 2_expressions.js ├── reviewTest.js ├── chapter1.js ├── chapter2.js ├── 4_arrays.js ├── chapter3.js ├── index.js ├── 1_variables.js ├── index.html ├── 2_expressions.html ├── 5_conditionals.js ├── 6_loops.js ├── 3_strings.js ├── 3_strings.html ├── 1_variables.html ├── 4_arrays.html ├── 6_loops.html └── 5_conditionals.html /README.md: -------------------------------------------------------------------------------- 1 | # javascript-lessons 2 | Learning Javascript the easy way. 3 | -------------------------------------------------------------------------------- /favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chalarangelo/javascript-lessons/HEAD/favicon.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Angelos Chalaris 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /0_comments.js: -------------------------------------------------------------------------------- 1 | document.addEventListener('DOMContentLoaded', function(event){ 2 | // Check if saved info, otherwise administer test 3 | if (localStorage.getItem('learn-js-user') === null){ 4 | window.location="./index.html"; 5 | } 6 | else { 7 | user = JSON.parse(localStorage.getItem('learn-js-user')); 8 | if (user['videos'] == 'yes') 9 | document.getElementsByTagName('style')[0].innerHTML += 'div.videos-further{ display: block;}'; 10 | if (user['desiredJsKnowledge'] == 'full') 11 | document.getElementsByTagName('style')[0].innerHTML += 'div.js-further{ display: block;}'; 12 | } 13 | window.setTimeout(function(){ 14 | if(user['comments'] == 'available' || user['comments'] == 'restricted') 15 | user['comments'] = 'seen'; 16 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 17 | },5000); 18 | window.setTimeout(function(){ 19 | user['comments'] = 'completed'; 20 | if(user['variables'] == 'restricted') 21 | user['variables'] = 'available'; 22 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 23 | },30000); 24 | document.getElementsByTagName('style')[0].innerHTML += '.custom{background: red;}'; 25 | }); 26 | 27 | user = {}; 28 | -------------------------------------------------------------------------------- /statistics.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Statistics 6 | 7 | 8 | 9 | 25 | 26 | 27 |
 Back

LearnJS

28 |
29 |
30 |

Statistics

31 |
32 |

Below you can view your statistics for all the different tests:

33 |
34 |
35 |
36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /reviewTest.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Review Quiz 6 | 7 | 8 | 9 | 25 | 26 | 27 |
 Back

LearnJS

28 |
29 |
30 |

Review Quiz

31 |
32 |

Answer all the question below, then click on Submit to see your results.

33 | 34 |
35 | 36 |
37 |
38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /chapter1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Chapter 1 Quiz 6 | 7 | 8 | 9 | 25 | 26 | 27 |
 Back

LearnJS

28 |
29 |
30 |

Chapter 1 Quiz

31 |
32 |

Answer all the question below, then click on Submit to see your results.

33 | 34 |
35 | 36 |
37 |
38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /chapter2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Chapter 2 Quiz 6 | 7 | 8 | 9 | 25 | 26 | 27 |
 Back

LearnJS

28 |
29 |
30 |

Chapter 2 Quiz

31 |
32 |

Answer all the question below, then click on Submit to see your results.

33 | 34 |
35 | 36 |
37 |
38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /chapter3.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Chapter 3 Quiz 6 | 7 | 8 | 9 | 25 | 26 | 27 |
 Back

LearnJS

28 |
29 |
30 |

Chapter 3 Quiz

31 |
32 |

Answer all the question below, then click on Submit to see your results.

33 | 34 |
35 | 36 |
37 |
38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /statistics.js: -------------------------------------------------------------------------------- 1 | document.addEventListener('DOMContentLoaded', function(event){ 2 | // Check if saved info, otherwise administer test 3 | if (localStorage.getItem('learn-js-user') === null){ 4 | window.location="./index.html"; 5 | } 6 | else { 7 | user = JSON.parse(localStorage.getItem('learn-js-user')); 8 | } 9 | generateStatistics(); 10 | }); 11 | 12 | var user = {}; 13 | 14 | function generateStatistics(){ 15 | var htmlToRender = ''; 16 | if (user['chapter1Score'] === -1){ 17 | htmlToRender += '

Chapter 1

You have not yet taken this test!


'; 18 | } 19 | else { 20 | htmlToRender += '

Chapter 1

'; 21 | htmlToRender += '

Total score: '+user['chapter1Score']+'%

'; 22 | htmlToRender += '

Variables score: '+user['chapter1VariablesScore']+'%

'; 23 | htmlToRender += '

Expressions score: '+user['chapter1ExpressionsScore']+'%

'; 24 | } 25 | if (user['chapter2Score'] === -1){ 26 | htmlToRender += '

Chapter 2

You have not yet taken this test!


'; 27 | } 28 | else { 29 | htmlToRender += '

Chapter 2

'; 30 | htmlToRender += '

Total score: '+user['chapter2Score']+'%

'; 31 | htmlToRender += '

Strings score: '+user['chapter2StringsScore']+'%

'; 32 | htmlToRender += '

Arrays score: '+user['chapter2ArraysScore']+'%

'; 33 | } 34 | if (user['chapter3Score'] === -1){ 35 | htmlToRender += '

Chapter 3

You have not yet taken this test!


'; 36 | } 37 | else { 38 | htmlToRender += '

Chapter 3

'; 39 | htmlToRender += '

Total score: '+user['chapter3Score']+'%

'; 40 | htmlToRender += '

Conditionals score: '+user['chapter3ConditionalsScore']+'%

'; 41 | htmlToRender += '

Loops score: '+user['chapter3LoopsScore']+'%

'; 42 | } 43 | if (user['reviewScore'] === -1){ 44 | htmlToRender += '

Review

You have not yet taken this test!


'; 45 | } 46 | else { 47 | htmlToRender += '

Review

'; 48 | htmlToRender += '

Total score: '+user['reviewScore']+'%

'; 49 | htmlToRender += '

Variables score: '+user['reviewVariablesScore']+'%

'; 50 | htmlToRender += '

Expressions score: '+user['reviewExpressionsScore']+'%

'; 51 | htmlToRender += '

Strings score: '+user['reviewStringsScore']+'%

'; 52 | htmlToRender += '

Arrays score: '+user['reviewArraysScore']+'%

'; 53 | htmlToRender += '

Conditionals score: '+user['reviewConditionalsScore']+'%

'; 54 | htmlToRender += '

Loops score: '+user['reviewLoopsScore']+'%

'; 55 | } 56 | document.getElementById('statistics-view').innerHTML = htmlToRender; 57 | } 58 | -------------------------------------------------------------------------------- /0_comments.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Comments 6 | 7 | 8 | 9 | 23 | 24 | 25 |
 BackNext 

LearnJS

26 |
27 |
28 |

Comment your JavaScript Code


29 |

Comments are lines of code that JavaScript will intentionally ignore. Comments are a great way to leave notes to yourself and to other people who will later need to figure out what that code does.

30 |

There are two ways to write comments in JavaScript:

31 |

Using // will tell JavaScript to ignore the remainder of the text on the current line:

32 |
// This is an in-line comment.
33 |

You can make a multi-line comment beginning with /* and ending with */:

34 |
/* This is a
35 |    multi-line comment */

36 |
37 |

Best practice

38 |

As you write code, you should regularly add comments to clarify the function of parts of your code. Good commenting can help communicate the intent of your code - both for others and for your future self.

39 |
40 |
41 |

  Further reading

42 | 45 |
46 |
47 |
48 |

  Videos

49 | 50 |

51 |
52 |
53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /2_expressions.js: -------------------------------------------------------------------------------- 1 | document.addEventListener('DOMContentLoaded', function(event){ 2 | // Check if saved info, otherwise administer test 3 | if (localStorage.getItem('learn-js-user') === null){ 4 | window.location="./index.html"; 5 | } 6 | else { 7 | user = JSON.parse(localStorage.getItem('learn-js-user')); 8 | if (user['desiredJsKnowledge'] == 'full') 9 | document.getElementsByTagName('style')[0].innerHTML += 'div.js-further{ display: block;}'; 10 | if(user['expressionsEntryStereotype'] < 0){ 11 | user['expressionsEntryStereotype'] = user['stereotype']; 12 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 13 | } 14 | if (user['expressionsEntryStereotype'] >= 1100 || user['stereotype'] >= 1100 || user['chapter1Score'] > 60) 15 | document.getElementsByTagName('style')[0].innerHTML += 'div.advanced-1100{ display: block;}'; 16 | } 17 | window.setTimeout(function(){ 18 | if(user['expressions'] == 'available' || user['expressions'] == 'restricted') 19 | user['expressions'] = 'seen'; 20 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 21 | },5000); 22 | }); 23 | 24 | user = {}; 25 | 26 | function checkQuestion(questionId){ 27 | var userInput = document.querySelector('input[name="question-'+questionId+'"]:checked').value; 28 | if(user['expressions'] != 'completed'){ 29 | user['expressions-question-'+questionId] = userInput; 30 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 31 | } 32 | if(userInput == 'add'){ 33 | document.getElementById('question-'+questionId+'-message').innerHTML = 'The + symbol is the addition operator.'; 34 | document.getElementById('question-'+questionId+'-message').className = 'failure'; 35 | if(user['expressionsMistakes'][questionId] == 0) { 36 | user['expressionsMistakes'][questionId] = 1; 37 | user['stereotype'] -= 5; 38 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 39 | } 40 | } 41 | else if(userInput == 'sub'){ 42 | document.getElementById('question-'+questionId+'-message').innerHTML = 'The - symbol is the subtraction operator.'; 43 | document.getElementById('question-'+questionId+'-message').className = 'failure'; 44 | if(user['expressionsMistakes'][questionId] == 0) { 45 | user['expressionsMistakes'][questionId] = 1; 46 | user['stereotype'] -= 5; 47 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 48 | } 49 | } 50 | else if(userInput == 'mult'){ 51 | document.getElementById('question-'+questionId+'-message').innerHTML = 'The * symbol is the multiplication operator.'; 52 | document.getElementById('question-'+questionId+'-message').className = 'failure'; 53 | if(user['expressionsMistakes'][questionId] == 0) { 54 | user['expressionsMistakes'][questionId] = 1; 55 | user['stereotype'] -= 5; 56 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 57 | } 58 | } 59 | else if(userInput == 'div'){ 60 | document.getElementById('question-'+questionId+'-message').innerHTML = 'The / symbol is the division operator.'; 61 | document.getElementById('question-'+questionId+'-message').className = 'failure'; 62 | if(user['expressionsMistakes'][questionId] == 0) { 63 | user['expressionsMistakes'][questionId] = 1; 64 | user['stereotype'] -= 5; 65 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 66 | } 67 | } 68 | else if(userInput == 'quotient'){ 69 | document.getElementById('question-'+questionId+'-message').innerHTML = 'The % symbol returns the remainder of a division, not the quotient.'; 70 | document.getElementById('question-'+questionId+'-message').className = 'failure'; 71 | if(user['expressionsMistakes'][questionId] == 0) { 72 | user['expressionsMistakes'][questionId] = 1; 73 | user['stereotype'] -= 5; 74 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 75 | } 76 | } 77 | else{ 78 | document.getElementById('question-'+questionId+'-message').innerHTML = 'Correct!'; 79 | document.getElementById('question-'+questionId+'-message').className = 'success'; 80 | if(user['expressionsMistakes'][questionId] == 0) { 81 | user['expressionsMistakes'][questionId] = -1; 82 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 83 | } 84 | if(questionId == 4){ 85 | user['expressionsEntryStereotype'] = user['stereotype']; 86 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 87 | } 88 | } 89 | if(typeof user['expressions-question-0'] !== 'undefined' && typeof user['expressions-question-1'] !== 'undefined' 90 | && typeof user['expressions-question-2'] !== 'undefined' && typeof user['expressions-question-3'] !== 'undefined' && 91 | (typeof user['expressions-question-4'] !== 'undefined' || user['expressionsEntryStereotype'] < 1100) ) 92 | if(user['expressions-question-0'] == 'true' && user['expressions-question-1'] == 'true' 93 | && user['expressions-question-2'] == 'true' && user['expressions-question-3'] == 'true' && 94 | (user['expressions-question-4'] == 'true' || user['expressionsEntryStereotype'] < 1100) ) { 95 | if(user['expressions'] != 'completed') user['stereotype'] += 50; 96 | user['expressions'] = 'completed'; 97 | if(user['strings'] == 'restricted') 98 | user['strings'] = 'available'; 99 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /reviewTest.js: -------------------------------------------------------------------------------- 1 | document.addEventListener('DOMContentLoaded', function(event){ 2 | // Check if saved info, otherwise administer test 3 | if (localStorage.getItem('learn-js-user') === null){ 4 | window.location="./index.html"; 5 | } 6 | else { 7 | user = JSON.parse(localStorage.getItem('learn-js-user')); 8 | } 9 | generateQuestions(); 10 | }); 11 | 12 | var user = {}; 13 | var questions = []; 14 | function generateQuestions(){ 15 | var questionsElement = document.getElementById('questions'); 16 | questions.push({qDesc: 'What is the value of x after the following code is executed?
var num = 0;
num = num + 5;
var x = num + 3 * 2;
', answer1: '6', answer1value: 'false', answer2: '11', answer2value: 'true', answer3: '5', answer3value: 'false', type: ['variables','expressions']}); 17 | questions.push({qDesc: 'What is the value of c after the following code is executed?
var myString = "My head hurts";
myString = "I have a headache! " + myString;
var c = myString[20];
', answer1: '"a"', answer1value: 'false', answer2: '"M"', answer2value: 'false', answer3: '"y"', answer3value: 'true', type: ['variables','strings']}); 18 | questions.push({qDesc: 'What is the value of i after the following code is executed?
var i = 2;
var myName = "Donald";
i = i + 2;
i = myName[i];
', answer1: '"d"', answer1value: 'false', answer2: '"l"', answer2value: 'true', answer3: '"a"', answer3value: 'false', type: ['expressions','strings']}); 19 | questions.push({qDesc: 'What is the value of myArray after the following code is executed?
var myArray = [0];
for(var i = 0; i < 3; i++){
myArray.push(i);
}
', answer1: '[0,1,2,3]', answer1value: 'false', answer2: '[0,0,1,2]', answer2value: 'true', answer3: '[1,2,3]', answer3value: 'false', type: ['arrays','loops']}); 20 | questions.push({qDesc: 'What is the value of myValue after the following code is executed?
var myArray = [1,2,3];
var myValue;
switch(myArray[1]){
case 1:
myValue = 5;
break;
case 2:
myValue = myArray.pop();
break;
default:
myValue = 2;
}
', answer1: '3', answer1value: 'true', answer2: '2', answer2value: 'false', answer3: '5', answer3value: 'false', type: ['conditionals','arrays']}); 21 | questions.push({qDesc: 'What is the value of x after the following code is executed?
var x = 6;
var y = 4;
while(x > 0 && y < 8){
x = x - 1;
y = y + 2;
}
', answer1: '0', answer1value: 'false', answer2: '4', answer2value: 'true', answer3: '5', answer3value: 'false', type: ['loops','conditionals']}); 22 | questions = shuffle(questions); 23 | var htmlToRender = ''; 24 | for(var i = 0; i < questions.length; i++) { 25 | htmlToRender += generateQuestion(i+1, questions[i].qDesc, questions[i].answer1, questions[i].answer1value, questions[i].answer2, questions[i].answer2value, questions[i].answer3, questions[i].answer3value ); 26 | } 27 | questionsElement.innerHTML = htmlToRender; 28 | } 29 | 30 | function generateQuestion(id, qDesc, answer1, answer1value, answer2, answer2value, answer3, answer3value){ 31 | var htmlToRender = '
'; 32 | htmlToRender += ''; 33 | htmlToRender += '
'; 34 | htmlToRender += '
'; 35 | htmlToRender += '
'; 36 | htmlToRender += '

'; 37 | return htmlToRender; 38 | } 39 | 40 | function shuffle(array) { 41 | var currentIndex = array.length, temporaryValue, randomIndex; 42 | 43 | // While there remain elements to shuffle... 44 | while (0 !== currentIndex) { 45 | 46 | // Pick a remaining element... 47 | randomIndex = Math.floor(Math.random() * currentIndex); 48 | currentIndex -= 1; 49 | 50 | // And swap it with the current element. 51 | temporaryValue = array[currentIndex]; 52 | array[currentIndex] = array[randomIndex]; 53 | array[randomIndex] = temporaryValue; 54 | } 55 | 56 | return array; 57 | } 58 | 59 | function checkQuestions(){ 60 | for(var i = 1; i <= 6; i++) { 61 | if(document.querySelector('input[name="q'+i+'"]:checked') === null ){ 62 | showErrorMessage(i); 63 | return; 64 | } 65 | } 66 | var correct = 0, variablesCorrect = 0, expressionsCorrect = 0, stringsCorrect = 0, arraysCorrect = 0, conditionalsCorrect = 0, loopsCorrect = 0; 67 | 68 | for(var i = 1; i <= 6; i++) { 69 | if (document.querySelector('input[name="q'+i+'"]:checked').value == 'true') { 70 | correct += 1; 71 | if (questions[i-1].type[0] == 'variables' || questions[i-1].type[1] == 'variables') variablesCorrect += 1; 72 | if (questions[i-1].type[0] == 'expressions' || questions[i-1].type[1] == 'expressions') expressionsCorrect += 1; 73 | if (questions[i-1].type[0] == 'strings' || questions[i-1].type[1] == 'strings') stringsCorrect += 1; 74 | if (questions[i-1].type[0] == 'arrays' || questions[i-1].type[1] == 'arrays') arraysCorrect += 1; 75 | if (questions[i-1].type[0] == 'conditionals' || questions[i-1].type[1] == 'conditionals') conditionalsCorrect += 1; 76 | if (questions[i-1].type[0] == 'loops' || questions[i-1].type[1] == 'loops') loopsCorrect += 1; 77 | } 78 | } 79 | showResults(correct, variablesCorrect, expressionsCorrect, stringsCorrect, arraysCorrect, conditionalsCorrect, loopsCorrect); 80 | var currentScore = correct / 6 * 100; 81 | user['reviewScore'] = (user['reviewScore'] > currentScore) ? user['reviewScore'] : currentScore; 82 | user['reviewVariablesScore'] = (user['reviewVariablesScore'] > (variablesCorrect / 2 * 100)) ? user['reviewVariablesScore'] : variablesCorrect / 2 * 100; 83 | user['reviewExpressionsScore'] = (user['reviewExpressionsScore'] > (expressionsCorrect / 2 * 100)) ? user['reviewExpressionsScore'] : expressionsCorrect / 2 * 100; 84 | user['reviewStringsScore'] = (user['reviewStringsScore'] > (stringsCorrect / 2 * 100)) ? user['reviewStringsScore'] : stringsCorrect / 2 * 100; 85 | user['reviewArraysScore'] = (user['reviewArraysScore'] > (arraysCorrect / 2 * 100)) ? user['reviewArraysScore'] : arraysCorrect / 2 * 100; 86 | user['reviewConditionalsScore'] = (user['reviewConditionalsScore'] > (conditionalsCorrect / 2 * 100)) ? user['reviewConditionalsScore'] : conditionalsCorrect / 2 * 100; 87 | user['reviewLoopsScore'] = (user['reviewLoopsScore'] > (loopsCorrect / 2 * 100)) ? user['reviewLoopsScore'] : loopsCorrect / 2 * 100; 88 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 89 | document.getElementById('submitButton').setAttribute('disabled','true'); 90 | } 91 | 92 | function showResults(correct, variablesCorrect, expressionsCorrect, stringsCorrect, arraysCorrect, conditionalsCorrect, loopsCorrect){ 93 | document.getElementById('results').innerHTML = 'You have '+correct+' answers out of 6 questions.
Variables score: '+(variablesCorrect/2 * 100)+'%
Expressions score: '+(expressionsCorrect/2 * 100)+'%
Strings score: '+(stringsCorrect/2 * 100)+'%
Arrays score: '+(arraysCorrect/2 * 100)+'%
Conditionals score: '+(conditionalsCorrect/2 * 100)+'%
Loops score: '+(loopsCorrect/2 * 100)+'%
' + ((correct>=4)?'Congratulations, you have passed the test!':'You have failed the test. Refresh this page to retake it!'); 94 | } 95 | 96 | function showErrorMessage(id){ 97 | document.getElementById('results').innerHTML = 'You have not answered question '+id+'!'; 98 | } 99 | -------------------------------------------------------------------------------- /chapter1.js: -------------------------------------------------------------------------------- 1 | document.addEventListener('DOMContentLoaded', function(event){ 2 | // Check if saved info, otherwise administer test 3 | if (localStorage.getItem('learn-js-user') === null){ 4 | window.location="./index.html"; 5 | } 6 | else { 7 | user = JSON.parse(localStorage.getItem('learn-js-user')); 8 | } 9 | generateQuestions(); 10 | }); 11 | 12 | var user = {}; 13 | var questions = []; 14 | function generateQuestions(){ 15 | var questionsElement = document.getElementById('questions'); 16 | var q1Num = Math.floor(Math.random() * 20); 17 | questions.push({qDesc: 'Which of the following is the correct way to assign the value '+q1Num+' to a variable myNum?', answer1: 'myNum = "'+q1Num+'";', answer1value: 'false', answer2: 'myNum = '+q1Num+';', answer2value: 'true', answer3: 'var myNum = '+q1Num+';', answer3value: 'false', type: 'variables'}); 18 | questions.push({qDesc: 'Which of the following is the correct way to assign the contents of the variable a to variable b?', answer1: 'a = b;', answer1value: 'false', answer2: 'var a = b;', answer2value: 'false', answer3: 'b = a;', answer3value: 'true', type: 'variables'}); 19 | questions.push({qDesc: 'Which is the correct way to initialize a variable myCoffee with the value "Espresso"?', answer1: 'myCoffee = "Espresso";', answer1value: 'false', answer2: 'var myCoffee = "Espresso";', answer2value: 'true', answer3: 'var myCoffee = Espresso;', answer3value: 'false', type: 'variables'}); 20 | questions.push({qDesc: 'Which is the correct way to initialize a variable myNum with the value '+(50-q1Num)+'?', answer1: 'var myNum = "'+(50-q1Num)+'";', answer1value: 'false', answer2: 'var myNum = '+(50-q1Num)+';', answer2value: 'true', answer3: 'myNum = '+(50-q1Num)+';', answer3value: 'false', type: 'variables'}); 21 | if(user['variablesEntryStereotype'] >= 1250) 22 | questions.push({qDesc: 'Consider the following code:
var a;
a = a + 5;
What is the value of a?', answer1: '5', answer1value: 'false', answer2: 'undefined', answer2value: 'false', answer3: 'NaN', answer3value: 'true', type: 'variables'}); 23 | else 24 | questions.push({qDesc: 'Which is the correct way to increment the value of a variable a by 5?', answer1: 'a = a + 5;', answer1value: 'true', answer2: 'var a = a + 5;', answer2value: 'false', answer3: 'a = a + "5";', answer3value: 'false', type: 'variables'}); 25 | questions.push({qDesc: 'Which is the correct way to decrement the value of a variable b by 8?', answer1: 'b = b - 8;', answer1value: 'true', answer2: 'var b = b - 8;', answer2value: 'false', answer3: 'b = b - "8";', answer3value: 'false', type: 'expressions'}); 26 | questions.push({qDesc: 'Which of the following symbols is the multiplication operator?', answer1: '/', answer1value: 'false', answer2: '*', answer2value: 'true', answer3: '+', answer3value: 'false', type: 'expressions'}); 27 | questions.push({qDesc: 'What is the result of the expression 2 + 5 * 10?', answer1: '70', answer1value: 'false', answer2: '52', answer2value: 'true', answer3: '25', answer3value: 'false', type: 'expressions'}); 28 | questions.push({qDesc: 'What is the result of the expression 30/5 + 2?', answer1: '4', answer1value: 'false', answer2: '8', answer2value: 'true', answer3: '5', answer3value: 'false', type: 'expressions'}); 29 | if(user['expressionsEntryStereotype'] >= 1100) 30 | questions.push({qDesc: 'What is the result of the expression 20 % 3?', answer1: '2', answer1value: 'true', answer2: '18', answer2value: 'false', answer3: '6', answer3value: 'false', type: 'expressions'}); 31 | else 32 | questions.push({qDesc: 'What is the result of the expression 20 / 3?', answer1: '2', answer1value: 'false', answer2: '18', answer2value: 'false', answer3: '6', answer3value: 'true', type: 'expressions'}); 33 | questions = shuffle(questions); 34 | var htmlToRender = ''; 35 | for(var i = 0; i < questions.length; i++) { 36 | htmlToRender += generateQuestion(i+1, questions[i].qDesc, questions[i].answer1, questions[i].answer1value, questions[i].answer2, questions[i].answer2value, questions[i].answer3, questions[i].answer3value ); 37 | } 38 | questionsElement.innerHTML = htmlToRender; 39 | } 40 | 41 | function generateQuestion(id, qDesc, answer1, answer1value, answer2, answer2value, answer3, answer3value){ 42 | var htmlToRender = '
'; 43 | htmlToRender += ''; 44 | htmlToRender += '
'; 45 | htmlToRender += '
'; 46 | htmlToRender += '
'; 47 | htmlToRender += '

'; 48 | return htmlToRender; 49 | } 50 | 51 | function shuffle(array) { 52 | var currentIndex = array.length, temporaryValue, randomIndex; 53 | 54 | // While there remain elements to shuffle... 55 | while (0 !== currentIndex) { 56 | 57 | // Pick a remaining element... 58 | randomIndex = Math.floor(Math.random() * currentIndex); 59 | currentIndex -= 1; 60 | 61 | // And swap it with the current element. 62 | temporaryValue = array[currentIndex]; 63 | array[currentIndex] = array[randomIndex]; 64 | array[randomIndex] = temporaryValue; 65 | } 66 | 67 | return array; 68 | } 69 | 70 | function checkQuestions(){ 71 | for(var i = 1; i <= 10; i++) { 72 | if(document.querySelector('input[name="q'+i+'"]:checked') === null ){ 73 | showErrorMessage(i); 74 | return; 75 | } 76 | } 77 | var correct = 0, variablesCorrect = 0, expressionsCorrect = 0; 78 | 79 | for(var i = 1; i <= 10; i++) { 80 | if (document.querySelector('input[name="q'+i+'"]:checked').value == 'true') { 81 | correct += 1; 82 | if (questions[i-1].type == 'variables') variablesCorrect += 1; 83 | if (questions[i-1].type == 'expressions') expressionsCorrect += 1; 84 | } 85 | } 86 | showResults(correct, variablesCorrect, expressionsCorrect); 87 | var currentScore = correct / 10 * 100; 88 | if(user['chapter1Score'] > 80) { 89 | if(currentScore >= 100) user['stereotype'] += 25; 90 | } 91 | if(user['chapter1Score'] > 60) { 92 | if(currentScore >= 80) user['stereotype'] += 25; 93 | if(currentScore >= 100) user['stereotype'] += 25; 94 | } 95 | else { 96 | user['stereotype'] += 50; 97 | } 98 | user['chapter1Score'] = (user['chapter1Score'] > currentScore) ? user['chapter1Score'] : currentScore; 99 | user['chapter1VariablesScore'] = (user['chapter1VariablesScore'] > (variablesCorrect / 5 * 100)) ? user['chapter1VariablesScore'] : variablesCorrect / 5 * 100; 100 | user['chapter1ExpressionsScore'] = ( user['chapter1ExpressionsScore'] > (expressionsCorrect / 5 * 100)) ? user['chapter1ExpressionsScore'] : expressionsCorrect / 5 * 100 ; 101 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 102 | document.getElementById('submitButton').setAttribute('disabled','true'); 103 | } 104 | 105 | function showResults(correct, variablesCorrect, expressionsCorrect){ 106 | document.getElementById('results').innerHTML = 'You have '+correct+' answers out of 10 questions.
Variables score: '+(variablesCorrect/5 * 100)+'%
Expressions score: '+(expressionsCorrect/5 * 100)+'%
' + ((correct>=6)?'Congratulations, you have passed the test!':'You have failed the test. Refresh this page to retake it!'); 107 | } 108 | 109 | function showErrorMessage(id){ 110 | document.getElementById('results').innerHTML = 'You have not answered question '+id+'!'; 111 | } 112 | -------------------------------------------------------------------------------- /chapter2.js: -------------------------------------------------------------------------------- 1 | document.addEventListener('DOMContentLoaded', function(event){ 2 | // Check if saved info, otherwise administer test 3 | if (localStorage.getItem('learn-js-user') === null){ 4 | window.location="./index.html"; 5 | } 6 | else { 7 | user = JSON.parse(localStorage.getItem('learn-js-user')); 8 | } 9 | generateQuestions(); 10 | }); 11 | 12 | var user = {}; 13 | var questions = []; 14 | function generateQuestions(){ 15 | var questionsElement = document.getElementById('questions'); 16 | var q1Num = Math.floor(Math.random() * 20); 17 | questions.push({qDesc: 'Which of the below is a correct string literal?', answer1: 'Bob', answer1value: 'false', answer2: '"Bob"', answer2value: 'true', answer3: '\'Bob"', answer3value: 'false', type: 'strings'}); 18 | questions.push({qDesc: 'What is the .length of the following string literal?
"Ludwig van Beethoven"
', answer1: '18', answer1value: 'false', answer2: '19', answer2value: 'false', answer3: '20', answer3value: 'true', type: 'strings'}); 19 | questions.push({qDesc: 'Which of the following is the result of the following string concatenation?
"I love"+"JavaScript!"
', answer1: '"I love JavaScript!"', answer1value: 'false', answer2: '"I loveJavaScript!"', answer2value: 'true', answer3: '"I love+JavaScript!"', answer3value: 'false', type: 'strings'}); 20 | questions.push({qDesc: 'What is the result of the expressions mySong[2] if mySong is equal to "Cherry Bomb"?', answer1: 'h', answer1value: 'false', answer2: 'e', answer2value: 'true', answer3: 'r', answer3value: 'false', type: 'strings'}); 21 | if(user['stringsEntryStereotype'] >= 1300) 22 | questions.push({qDesc: 'What is the value of the variable id in the following code?
var id = "15" + 31;
', answer1: '1531', answer1value: 'false', answer2: '"46"', answer2value: 'false', answer3: '"1531"', answer3value: 'true', type: 'strings'}); 23 | else 24 | questions.push({qDesc: 'What is the result of the expressions myMovie[6] if mySong is equal to "Breakfast at Tiffany\'s"?', answer1: 'f', answer1value: 'false', answer2: 'a', answer2value: 'true', answer3: 's', answer3value: 'false', type: 'strings'}); 25 | questions.push({qDesc: 'Which is the correct way to initialize an array person so that it contains the string Tom and the number 45?', answer1: 'var person = ["Tom",45];', answer1value: 'true', answer2: 'var person = [Tom,45];', answer2value: 'false', answer3: 'var person = ["Tom","45"];', answer3value: 'false', type: 'arrays'}); 26 | questions.push({qDesc: 'What is the result of the expressions myArray[1] = "Beer" if myArray is equal to [2,4,2]?', answer1: '["Beer",4,2]', answer1value: 'false', answer2: '[2,"Beer",2]', answer2value: 'true', answer3: '[2,4,"Beer"]', answer3value: 'false', type: 'arrays'}); 27 | questions.push({qDesc: 'Consider the following code:
var myPun = ["I","scream","for","ice"];
myPun.push("cream");
What does the array myPun now equal to?', answer1: '["cream","I","scream","for","ice"]', answer1value: 'false', answer2: '["I","scream","for","ice","cream"]', answer2value: 'true', answer3: '["I","scream","for","cream"]', answer3value: 'false', type: 'arrays'}); 28 | questions.push({qDesc: 'Consider the following code:
var sequence = [1,11,21,1211,1221];
var oneDown = sequence.pop();
What is the value of the variable oneDown?', answer1: '1', answer1value: 'false', answer2: '1221', answer2value: 'true', answer3: '1211', answer3value: 'false', type: 'arrays'}); 29 | if(user['arraysEntryStereotype'] >= 1250) 30 | questions.push({qDesc: 'Consider the following code:
 var numbers = [2,4,8].unshift(1);
What is the value of the variable numbers?', answer1: '[1,2,4,8]', answer1value: 'true', answer2: '[2,4,8,1]', answer2value: 'false', answer3: '[4,8]', answer3value: 'false', type: 'arrays'}); 31 | else 32 | questions.push({qDesc: 'What is the result of the expressions myArray[3] = "Candy" if myArray is equal to [6,0,9]?', answer1: '[6,0,"Candy"]', answer1value: 'false', answer2: '[6,0,9,"Candy"]', answer2value: 'true', answer3: '[6,0,9]', answer3value: 'false', type: 'arrays'}); 33 | questions = shuffle(questions); 34 | var htmlToRender = ''; 35 | for(var i = 0; i < questions.length; i++) { 36 | htmlToRender += generateQuestion(i+1, questions[i].qDesc, questions[i].answer1, questions[i].answer1value, questions[i].answer2, questions[i].answer2value, questions[i].answer3, questions[i].answer3value ); 37 | } 38 | questionsElement.innerHTML = htmlToRender; 39 | } 40 | 41 | function generateQuestion(id, qDesc, answer1, answer1value, answer2, answer2value, answer3, answer3value){ 42 | var htmlToRender = '
'; 43 | htmlToRender += ''; 44 | htmlToRender += '
'; 45 | htmlToRender += '
'; 46 | htmlToRender += '
'; 47 | htmlToRender += '

'; 48 | return htmlToRender; 49 | } 50 | 51 | function shuffle(array) { 52 | var currentIndex = array.length, temporaryValue, randomIndex; 53 | 54 | // While there remain elements to shuffle... 55 | while (0 !== currentIndex) { 56 | 57 | // Pick a remaining element... 58 | randomIndex = Math.floor(Math.random() * currentIndex); 59 | currentIndex -= 1; 60 | 61 | // And swap it with the current element. 62 | temporaryValue = array[currentIndex]; 63 | array[currentIndex] = array[randomIndex]; 64 | array[randomIndex] = temporaryValue; 65 | } 66 | 67 | return array; 68 | } 69 | 70 | function checkQuestions(){ 71 | for(var i = 1; i <= 10; i++) { 72 | if(document.querySelector('input[name="q'+i+'"]:checked') === null ){ 73 | showErrorMessage(i); 74 | return; 75 | } 76 | } 77 | var correct = 0, stringsCorrect = 0, arraysCorrect = 0; 78 | 79 | for(var i = 1; i <= 10; i++) { 80 | if (document.querySelector('input[name="q'+i+'"]:checked').value == 'true') { 81 | correct += 1; 82 | if (questions[i-1].type == 'strings') stringsCorrect += 1; 83 | if (questions[i-1].type == 'arrays') arraysCorrect += 1; 84 | } 85 | } 86 | showResults(correct, stringsCorrect, arraysCorrect); 87 | var currentScore = correct / 10 * 100; 88 | if(user['chapter2Score'] > 80) { 89 | if(currentScore >= 100) user['stereotype'] += 25; 90 | } 91 | if(user['chapter2Score'] > 60) { 92 | if(currentScore >= 80) user['stereotype'] += 25; 93 | if(currentScore >= 100) user['stereotype'] += 25; 94 | } 95 | else { 96 | user['stereotype'] += 50; 97 | } 98 | user['chapter2Score'] = (user['chapter2Score'] > currentScore) ? user['chapter2Score'] : currentScore; 99 | user['chapter2StringsScore'] = (user['chapter2StringsScore'] > (stringsCorrect / 5 * 100)) ? user['chapter2StringsScore'] : stringsCorrect / 5 * 100; 100 | user['chapter2ArraysScore'] = ( user['chapter2ArraysScore'] > (arraysCorrect / 5 * 100)) ? user['chapter2ArraysScore'] : arraysCorrect / 5 * 100 ; 101 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 102 | document.getElementById('submitButton').setAttribute('disabled','true'); 103 | } 104 | 105 | function showResults(correct, stringsCorrect, arraysCorrect){ 106 | document.getElementById('results').innerHTML = 'You have '+correct+' answers out of 10 questions.
strings score: '+(stringsCorrect/5 * 100)+'%
arrays score: '+(arraysCorrect/5 * 100)+'%
' + ((correct>=6)?'Congratulations, you have passed the test!':'You have failed the test. Refresh this page to retake it!'); 107 | } 108 | 109 | function showErrorMessage(id){ 110 | document.getElementById('results').innerHTML = 'You have not answered question '+id+'!'; 111 | } 112 | -------------------------------------------------------------------------------- /4_arrays.js: -------------------------------------------------------------------------------- 1 | document.addEventListener('DOMContentLoaded', function(event){ 2 | // Check if saved info, otherwise administer test 3 | if (localStorage.getItem('learn-js-user') === null){ 4 | window.location="./index.html"; 5 | } 6 | else { 7 | user = JSON.parse(localStorage.getItem('learn-js-user')); 8 | if(user['arraysEntryStereotype'] < 0){ 9 | user['arraysEntryStereotype'] = user['stereotype']; 10 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 11 | } 12 | if (user['arraysEntryStereotype'] >= 1250 || user['stereotype'] >= 1250 || user['chapter2Score'] > 60) 13 | document.getElementsByTagName('style')[0].innerHTML += 'div.advanced-1250{ display: block;}'; 14 | if (user['videos'] == 'yes') 15 | document.getElementsByTagName('style')[0].innerHTML += 'div.videos-further{ display: block;}'; 16 | if (user['desiredJsKnowledge'] == 'full' || user['desiredProgrammingKnowledge'] == 'full') 17 | document.getElementsByTagName('style')[0].innerHTML += 'div.read-further{ display: block;}'; 18 | if (user['desiredJsKnowledge'] == 'full') 19 | document.getElementsByTagName('style')[0].innerHTML += 'li.js-further{ display: list-item;}'; 20 | if (user['desiredProgrammingKnowledge'] == 'full') 21 | document.getElementsByTagName('style')[0].innerHTML += 'li.programming-further{ display: list-item;}'; 22 | } 23 | window.setTimeout(function(){ 24 | if(user['arrays'] == 'available' || user['arrays'] == 'restricted') 25 | user['arrays'] = 'seen'; 26 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 27 | },5000); 28 | }); 29 | 30 | user = {}; 31 | 32 | function checkQuestion(questionId){ 33 | if(questionId == 0){ 34 | var userInput = document.querySelector('input[name="question-0"]:checked').value; 35 | if(user['arrays'] != 'completed'){ 36 | user['arrays-question-0'] = userInput; 37 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 38 | } 39 | if(userInput == 'no-square-bracket'){ 40 | document.getElementById('question-0-message').innerHTML = 'An array declaration starts with an opening square bracket [ and ends with a closing square bracket ].'; 41 | document.getElementById('question-0-message').className = 'failure'; 42 | if(user['arraysMistakes'][questionId] == 0) { 43 | user['arraysMistakes'][questionId] = 1; 44 | user['stereotype'] -= 5; 45 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 46 | } 47 | } 48 | else if(userInput == 'no-comma'){ 49 | document.getElementById('question-0-message').innerHTML = 'Between each array entry should be a comma.'; 50 | document.getElementById('question-0-message').className = 'failure'; 51 | if(user['arraysMistakes'][questionId] == 0) { 52 | user['arraysMistakes'][questionId] = 1; 53 | user['stereotype'] -= 5; 54 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 55 | } 56 | } 57 | else{ 58 | document.getElementById('question-0-message').innerHTML = 'Correct!'; 59 | document.getElementById('question-0-message').className = 'success'; 60 | if(user['arraysMistakes'][questionId] == 0) { 61 | user['arraysMistakes'][questionId] = -1; 62 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 63 | } 64 | } 65 | } 66 | else if(questionId == 1){ 67 | var userInput = document.querySelector('input[name="question-1"]:checked').value; 68 | if(user['arrays'] != 'completed'){ 69 | user['arrays-question-1'] = userInput; 70 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 71 | } 72 | if(userInput == 'non-zero-indexed'){ 73 | document.getElementById('question-1-message').innerHTML = 'Remember that Javascript uses zero-based indexing.'; 74 | document.getElementById('question-1-message').className = 'failure'; 75 | if(user['arraysMistakes'][questionId] == 0) { 76 | user['arraysMistakes'][questionId] = 1; 77 | user['stereotype'] -= 5; 78 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 79 | } 80 | } 81 | else{ 82 | document.getElementById('question-1-message').innerHTML = 'Correct!'; 83 | document.getElementById('question-1-message').className = 'success'; 84 | if(user['arraysMistakes'][questionId] == 0) { 85 | user['arraysMistakes'][questionId] = -1; 86 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 87 | } 88 | } 89 | } 90 | else if(questionId == 2){ 91 | var userInput = document.querySelector('input[name="question-2"]:checked').value; 92 | if(user['arrays'] != 'completed'){ 93 | user['arrays-question-2'] = userInput; 94 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 95 | } 96 | if(userInput == 'beginning'){ 97 | document.getElementById('question-2-message').innerHTML = 'The .push() function append data to the end of an array.'; 98 | document.getElementById('question-2-message').className = 'failure'; 99 | if(user['arraysMistakes'][questionId] == 0) { 100 | user['arraysMistakes'][questionId] = 1; 101 | user['stereotype'] -= 5; 102 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 103 | } 104 | } 105 | else{ 106 | document.getElementById('question-2-message').innerHTML = 'Correct!'; 107 | document.getElementById('question-2-message').className = 'success'; 108 | if(user['arraysMistakes'][questionId] == 0) { 109 | user['arraysMistakes'][questionId] = -1; 110 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 111 | } 112 | } 113 | } 114 | if(questionId == 3){ 115 | var userInput = document.querySelector('input[name="question-3"]:checked').value; 116 | if(user['arrays'] != 'completed'){ 117 | user['arrays-question-3'] = userInput; 118 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 119 | } 120 | if(userInput == 'beginning'){ 121 | document.getElementById('question-3-message').innerHTML = 'The .pop() function extracts the last element of an array.'; 122 | document.getElementById('question-3-message').className = 'failure'; 123 | if(user['arraysMistakes'][questionId] == 0) { 124 | user['arraysMistakes'][questionId] = 1; 125 | user['stereotype'] -= 5; 126 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 127 | } 128 | } 129 | else{ 130 | document.getElementById('question-3-message').innerHTML = 'Correct!'; 131 | document.getElementById('question-3-message').className = 'success'; 132 | if(user['arraysMistakes'][questionId] == 0) { 133 | user['arraysMistakes'][questionId] = -1; 134 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 135 | } 136 | } 137 | } 138 | if(questionId == 4){ 139 | var userInput = document.querySelector('input[name="question-4"]:checked').value; 140 | if(user['arrays'] != 'completed'){ 141 | user['arrays-question-4'] = userInput; 142 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 143 | } 144 | if(userInput == 'not-shift'){ 145 | document.getElementById('question-4-message').innerHTML = 'The .unshift() function inserts elements, instead of removing them.'; 146 | document.getElementById('question-4-message').className = 'failure'; 147 | if(user['arraysMistakes'][questionId] == 0) { 148 | user['arraysMistakes'][questionId] = 1; 149 | user['stereotype'] -= 5; 150 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 151 | } 152 | } 153 | if(userInput == 'not-at-end'){ 154 | document.getElementById('question-4-message').innerHTML = 'The .unshift() function does not insert elements at the end of the array.'; 155 | document.getElementById('question-4-message').className = 'failure'; 156 | if(user['arraysMistakes'][questionId] == 0) { 157 | user['arraysMistakes'][questionId] = 1; 158 | user['stereotype'] -= 5; 159 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 160 | } 161 | } 162 | else{ 163 | document.getElementById('question-4-message').innerHTML = 'Correct!'; 164 | document.getElementById('question-4-message').className = 'success'; 165 | user['arraysEntryStereotype'] = user['stereotype']; 166 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 167 | if(user['arraysMistakes'][questionId] == 0) { 168 | user['arraysMistakes'][questionId] = -1; 169 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 170 | } 171 | } 172 | } 173 | if(typeof user['arrays-question-0'] !== 'undefined' && typeof user['arrays-question-1'] !== 'undefined' 174 | && typeof user['arrays-question-2'] !== 'undefined' && typeof user['arrays-question-3'] !== 'undefined' && 175 | (typeof user['arrays-question-4'] !== 'undefined' || user['arraysEntryStereotype'] < 1250) ) 176 | if(user['arrays-question-0'] == 'true' && user['arrays-question-1'] == 'true' 177 | && user['arrays-question-2'] == 'true' && user['arrays-question-3'] == 'true'&& 178 | (user['arrays-question-4'] == 'true' || user['arraysEntryStereotype'] < 1250) ) { 179 | if(user['arrays'] != 'completed') user['stereotype'] += 50; 180 | user['arrays'] = 'completed'; 181 | if(user['conditionals'] == 'restricted') 182 | user['conditionals'] = 'available'; 183 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /chapter3.js: -------------------------------------------------------------------------------- 1 | document.addEventListener('DOMContentLoaded', function(event){ 2 | // Check if saved info, otherwise administer test 3 | if (localStorage.getItem('learn-js-user') === null){ 4 | window.location="./index.html"; 5 | } 6 | else { 7 | user = JSON.parse(localStorage.getItem('learn-js-user')); 8 | } 9 | generateQuestions(); 10 | }); 11 | 12 | var user = {}; 13 | var questions = []; 14 | function generateQuestions(){ 15 | var questionsElement = document.getElementById('questions'); 16 | var q1Num = Math.floor(Math.random() * 20); 17 | questions.push({qDesc: 'What is the value of num after the following code is executed?
num = 5;
if(num > 5) {
num = num + 1;
}
if(num < 6) {
num = num - 1;
}
', answer1: '6', answer1value: 'false', answer2: '4', answer2value: 'true', answer3: '5', answer3value: 'false', type: 'conditionals'}); 18 | questions.push({qDesc: 'Consider the following code:
if ("fun" == 0) {
console.log("I don\'t know!");
} else if ("fun" == "fun") {
console.log("Piña colada");
} else {
console.log("People are strange!");
}
What will be printed to the console?', answer1: 'I don\'t know!', answer1value: 'false', answer2: 'People are strange!', answer2value: 'false', answer3: 'Piña colada', answer3value: 'true', type: 'conditionals'}); 19 | questions.push({qDesc: 'Consider the following switch statment:
switch(val) {
case 1:
console.log("alpha");
break;
case 2:
console.log("beta");
break;
case 3:
console.log("gamma");
break;
}
If val = 2 , what will be printed to the console?', answer1: 'alpha', answer1value: 'false', answer2: 'beta', answer2value: 'true', answer3: 'gamma', answer3value: 'false', type: 'conditionals'}); 20 | questions.push({qDesc: 'Consider the following code. If today is neither Saturday nor Sunday, what will be printed to the console?
switch (day) {
case 6:
console.log("Today is Saturday");
break;
case 0:
console.log("Today is Sunday");
break;
default:
console.log("Looking forward to the Weekend");
}
', answer1: '"Today is Saturday"', answer1value: 'false', answer2: '"Looking forward to the Weekend"', answer2value: 'true', answer3: '"Today is Sunday"', answer3value: 'false', type: 'conditionals'}); 21 | if(user['conditionalsEntryStereotype'] >= 1350) 22 | questions.push({qDesc: 'Consider the following code:
var title = (married)?"Mrs":"Ms";
What is the value of title if the value of married is false?', answer1: '"Mrs"', answer1value: 'false', answer2: 'false', answer2value: 'false', answer3: '"Ms"', answer3value: 'true', type: 'conditionals'}); 23 | else 24 | questions.push({qDesc: 'What will be printed to the console?
bus = 5;
if (bus < 3 || bus > 6 && bus < 8) {
console.log("Take the bus!");
} else if (bus > 8) {
console.log("Too late!");
} else {
console.log("Stay put!");
}
', answer1: '"Take the bus!"', answer1value: 'false', answer2: '"Stay put!"', answer2value: 'true', answer3: '"Too late!"', answer3value: 'false', type: 'conditionals'}); 25 | questions.push({qDesc: 'Which of the following is the end condition for this for loop?
for (var condition = 0; condition < 5; condition ++) {
return condition;
}
', answer1: 'condition < 5', answer1value: 'true', answer2: 'condition = 0', answer2value: 'false', answer3: 'condition ++', answer3value: 'false', type: 'loops'}); 26 | questions.push({qDesc: 'Consider the following code. After the loop stops executing what does the array workDays equal to ?
var workDays = [];
for (var i = 1; i < 6; i++) {
myArray.push(i);
}
', answer1: '[1,2,3,4,5,6]', answer1value: 'false', answer2: '[1,2,3,4,5]', answer2value: 'true', answer3: '[0,1,2,3,4,5]', answer3value: 'false', type: 'loops'}); 27 | questions.push({qDesc: 'Given the following code, what value does the variable total hold?
var myArr = [0, 1, 2, 3, 4, 5];
var total = 0;
for (var i = 0; i < 5; i++) {
total += myArr[i];
}
', answer1: '"012345"', answer1value: 'false', answer2: '10', answer2value: 'true', answer3: '15', answer3value: 'false', type: 'loops'}); 28 | questions.push({qDesc: 'Consider the following code. After the loop stops executing what does the array myArray equal to ?
var myArray = ["Simple","as"];
var i = 1;
while(i < 4) {
myArray.push(i);
i++;
}
', answer1: '["Simple","as",1,2,3,4]', answer1value: 'false', answer2: '["Simple","as",1,2,3]', answer2value: 'true', answer3: '[1,2,3]', answer3value: 'false', type: 'loops'}); 29 | if(user['loopsEntryStereotype'] >= 1400) 30 | questions.push({qDesc: 'Consider the following code:
 var x = 0;
do {
if (x == 0) x = 5;
else x = 0;
console.log("Looping");
} while(x > 0);
How many times will Looping will be printed?', answer1: '2', answer1value: 'true', answer2: '0', answer2value: 'false', answer3: '1', answer3value: 'false', type: 'loops'}); 31 | else 32 | questions.push({qDesc: 'Consider the following code:
 var x = 0;
while(x > 0){
if (x == 0) x = 5;
else x = 0;
console.log("Looping");
}
How many times will Looping will be printed?', answer1: '2', answer1value: 'false', answer2: '0', answer2value: 'true', answer3: '1', answer3value: 'false', type: 'loops'}); 33 | questions = shuffle(questions); 34 | var htmlToRender = ''; 35 | for(var i = 0; i < questions.length; i++) { 36 | htmlToRender += generateQuestion(i+1, questions[i].qDesc, questions[i].answer1, questions[i].answer1value, questions[i].answer2, questions[i].answer2value, questions[i].answer3, questions[i].answer3value ); 37 | } 38 | questionsElement.innerHTML = htmlToRender; 39 | } 40 | 41 | function generateQuestion(id, qDesc, answer1, answer1value, answer2, answer2value, answer3, answer3value){ 42 | var htmlToRender = '
'; 43 | htmlToRender += ''; 44 | htmlToRender += '
'; 45 | htmlToRender += '
'; 46 | htmlToRender += '
'; 47 | htmlToRender += '

'; 48 | return htmlToRender; 49 | } 50 | 51 | function shuffle(array) { 52 | var currentIndex = array.length, temporaryValue, randomIndex; 53 | 54 | // While there remain elements to shuffle... 55 | while (0 !== currentIndex) { 56 | 57 | // Pick a remaining element... 58 | randomIndex = Math.floor(Math.random() * currentIndex); 59 | currentIndex -= 1; 60 | 61 | // And swap it with the current element. 62 | temporaryValue = array[currentIndex]; 63 | array[currentIndex] = array[randomIndex]; 64 | array[randomIndex] = temporaryValue; 65 | } 66 | 67 | return array; 68 | } 69 | 70 | function checkQuestions(){ 71 | for(var i = 1; i <= 10; i++) { 72 | if(document.querySelector('input[name="q'+i+'"]:checked') === null ){ 73 | showErrorMessage(i); 74 | return; 75 | } 76 | } 77 | var correct = 0, conditionalsCorrect = 0, loopsCorrect = 0; 78 | 79 | for(var i = 1; i <= 10; i++) { 80 | if (document.querySelector('input[name="q'+i+'"]:checked').value == 'true') { 81 | correct += 1; 82 | if (questions[i-1].type == 'conditionals') conditionalsCorrect += 1; 83 | if (questions[i-1].type == 'loops') loopsCorrect += 1; 84 | } 85 | } 86 | showResults(correct, conditionalsCorrect, loopsCorrect); 87 | var currentScore = correct / 10 * 100; 88 | if(user['chapter3Score'] > 80) { 89 | if(currentScore >= 100) user['stereotype'] += 25; 90 | } 91 | if(user['chapter3Score'] > 60) { 92 | if(currentScore >= 80) user['stereotype'] += 25; 93 | if(currentScore >= 100) user['stereotype'] += 25; 94 | } 95 | else { 96 | user['stereotype'] += 50; 97 | } 98 | user['chapter3Score'] = (user['chapter3Score'] > currentScore) ? user['chapter3Score'] : currentScore; 99 | user['chapter3ConditionalsScore'] = (user['chapter3ConditionalsScore'] > (conditionalsCorrect / 5 * 100)) ? user['chapter3ConditionalsScore'] : conditionalsCorrect / 5 * 100; 100 | user['chapter3LoopsScore'] = ( user['chapter3LoopsScore'] > (loopsCorrect / 5 * 100)) ? user['chapter3LoopsScore'] : loopsCorrect / 5 * 100 ; 101 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 102 | document.getElementById('submitButton').setAttribute('disabled','true'); 103 | } 104 | 105 | function showResults(correct, conditionalsCorrect, loopsCorrect){ 106 | document.getElementById('results').innerHTML = 'You have '+correct+' answers out of 10 questions.
conditionals score: '+(conditionalsCorrect/5 * 100)+'%
loops score: '+(loopsCorrect/5 * 100)+'%
' + ((correct>=6)?'Congratulations, you have passed the test!':'You have failed the test. Refresh this page to retake it!'); 107 | } 108 | 109 | function showErrorMessage(id){ 110 | document.getElementById('results').innerHTML = 'You have not answered question '+id+'!'; 111 | } 112 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | document.addEventListener('DOMContentLoaded', function(event){ 2 | // Check if saved info, otherwise administer test 3 | if (localStorage.getItem('learn-js-user') === null){ 4 | document.getElementById('new-user-questions').className = ''; 5 | advanceQuestion(); 6 | } 7 | else { 8 | user = JSON.parse(localStorage.getItem('learn-js-user')); 9 | updateUserMenu(); 10 | document.getElementById('existing-user-menu').className = ''; 11 | } 12 | }); 13 | 14 | function deleteUserSystemInfo(){ 15 | localStorage.removeItem('learn-js-user'); 16 | return 'Done!'; 17 | } 18 | 19 | var user = {}; 20 | var userScore = [false, false, false, false, false, false, false, false, false, '']; 21 | questionId = 0; 22 | 23 | function score(qId, value){ 24 | userScore[qId] = value; 25 | advanceQuestion(); 26 | } 27 | 28 | function advanceQuestion(){ 29 | if (questionId > 0 && questionId <6) 30 | document.getElementById('skill-q'+questionId).className = 'question hidden'; 31 | else if (questionId > 5) 32 | document.getElementById('preference-q'+(questionId-5)).className = 'question hidden'; 33 | questionId += 1; 34 | if(questionId < 6){ 35 | document.getElementById('skill-q'+questionId).className = 'question'; 36 | window.setTimeout( function() { 37 | document.getElementById('skill-q'+questionId).style.marginLeft = '0'; 38 | }, 10); 39 | } 40 | else if(questionId < 11){ 41 | document.getElementById('preference-q'+(questionId-5)).className = 'question'; 42 | window.setTimeout( function() { 43 | document.getElementById('preference-q'+(questionId-5)).style.marginLeft = '0'; 44 | }, 10); 45 | } 46 | else { 47 | var userStereotype = 1000; 48 | if (userScore[0]) userStereotype += 50; 49 | if (userScore[0] && userScore[1]) userStereotype += 50; 50 | if (userScore[0] && userScore[2]) userStereotype += 100; 51 | if (userScore[3]) userStereotype += 50; 52 | if (userScore[3] && userScore[4]) userStereotype += 50; 53 | if (userScore[7]) userStereotype += 50; 54 | var user = { 55 | 'comments' : 'available', 56 | 'variables' : userScore[0]?'available':'restricted', 57 | 'expressions' : (userScore[0] && userScore[1])?'available':'restricted', 58 | 'strings' : (userScore[0] && userScore[1])?'available':'restricted', 59 | 'arrays' : (userScore[0] && userScore[2])?'available':'restricted', 60 | 'conditionals' : userScore[3]?'available':'restricted', 61 | 'loops' : (userScore[3] && userScore[4])?'available':'restricted', 62 | 'desiredJsKnowledge' : userScore[5]?'full':'basic', 63 | 'desiredProgrammingKnowledge' : userScore[6]?'full':'basic', 64 | 'experience' : userScore[7]?'yes':'no', 65 | 'videos' : userScore[8]?'yes':'no', 66 | 'animal' : userScore[9], 67 | 'stereotype' : userStereotype, 68 | // Properties needed for lessons etc. 69 | 'variablesEntryStereotype' : -1, 70 | 'expressionsEntryStereotype' : -1, 71 | 'expressionsMistakes' : [0,0,0,0,0], 72 | 'stringsEntryStereotype' : -1, 73 | 'stringsMistakes' : [0,0,0,0,0], 74 | 'arraysEntryStereotype' : -1, 75 | 'arraysMistakes' : [0,0,0,0,0], 76 | 'conditionalsEntryStereotype' : -1, 77 | 'conditionalsMistakes' : [0,0,0,0,0,0], 78 | 'loopsEntryStereotype' : -1, 79 | 'loopsMistakes' : [0,0,0,0,0], 80 | 'chapter1Score' : -1, 81 | 'chapter1VariablesScore' : -1, 82 | 'chapter1ExpressionsScore' : -1, 83 | 'chapter2Score' : -1, 84 | 'chapter2StringsScore' : -1, 85 | 'chapter2ArraysScore' : -1, 86 | 'chapter3Score' : -1, 87 | 'chapter3ConditionalsScore' : -1, 88 | 'chapter3LoopsScore' : -1, 89 | 'reviewScore' : -1, 90 | 'reviewVariablesScore' : -1, 91 | 'reviewExpressionsScore' : -1, 92 | 'reviewStringsScore' : -1, 93 | 'reviewArraysScore' : -1, 94 | 'reviewConditionalsScore' : -1, 95 | 'reviewLoopsScore' : -1 96 | } 97 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 98 | location.reload(); 99 | } 100 | } 101 | 102 | function updateUserMenu(){ 103 | updateMenuItem('comments'); 104 | updateMenuItem('variables'); 105 | updateMenuItem('expressions'); 106 | updateMenuItem('strings'); 107 | updateMenuItem('arrays'); 108 | updateMenuItem('conditionals'); 109 | updateMenuItem('loops'); 110 | updateTestItems(); 111 | } 112 | 113 | function updateMenuItem(name){ 114 | switch (user[name]){ 115 | case 'available': 116 | document.getElementById('menu-'+name).innerHTML = availableIcon; 117 | document.getElementById('menu-'+name).setAttribute('aria-label', 'Available'); 118 | break; 119 | case 'seen': 120 | document.getElementById('menu-'+name).innerHTML = seenIcon; 121 | document.getElementById('menu-'+name).setAttribute('aria-label', 'Seen'); 122 | break; 123 | case 'completed': 124 | document.getElementById('menu-'+name).innerHTML = completedIcon; 125 | document.getElementById('menu-'+name).setAttribute('aria-label', 'Completed'); 126 | break; 127 | case 'restricted': 128 | default: 129 | document.getElementById('menu-'+name).innerHTML = restrictedIcon; 130 | document.getElementById('menu-'+name).setAttribute('aria-label', 'Restricted'); 131 | break; 132 | } 133 | } 134 | 135 | function updateTestItems(){ 136 | if(user['chapter1Score'] >= 60){ 137 | document.getElementById('test-chapter-1').innerHTML = completedIcon; 138 | document.getElementById('test-chapter-1').setAttribute('aria-label', 'Completed'); 139 | } 140 | else if(user['variables'] == 'completed' && user['expressions'] == 'completed'){ 141 | document.getElementById('test-chapter-1').innerHTML = availableIcon; 142 | document.getElementById('test-chapter-1').setAttribute('aria-label', 'Available'); 143 | } 144 | else { 145 | document.getElementById('test-chapter-1').innerHTML = restrictedIcon; 146 | document.getElementById('test-chapter-1').setAttribute('aria-label', 'Restricted'); 147 | } 148 | if(user['chapter2Score'] >= 60){ 149 | document.getElementById('test-chapter-2').innerHTML = completedIcon; 150 | document.getElementById('test-chapter-2').setAttribute('aria-label', 'Completed'); 151 | } 152 | else if(user['strings'] == 'completed' && user['arrays'] == 'completed'){ 153 | document.getElementById('test-chapter-2').innerHTML = availableIcon; 154 | document.getElementById('test-chapter-2').setAttribute('aria-label', 'Available'); 155 | } 156 | else { 157 | document.getElementById('test-chapter-2').innerHTML = restrictedIcon; 158 | document.getElementById('test-chapter-2').setAttribute('aria-label', 'Restricted'); 159 | } 160 | if(user['chapter3Score'] >= 60){ 161 | document.getElementById('test-chapter-3').innerHTML = completedIcon; 162 | document.getElementById('test-chapter-3').setAttribute('aria-label', 'Completed'); 163 | } 164 | else if(user['conditionals'] == 'completed' && user['loops'] == 'completed'){ 165 | document.getElementById('test-chapter-3').innerHTML = availableIcon; 166 | document.getElementById('test-chapter-3').setAttribute('aria-label', 'Available'); 167 | } 168 | else { 169 | document.getElementById('test-chapter-3').innerHTML = restrictedIcon; 170 | document.getElementById('test-chapter-3').setAttribute('aria-label', 'Restricted'); 171 | } 172 | if(user['reviewScore'] >= 60){ 173 | document.getElementById('test-review').innerHTML = completedIcon; 174 | document.getElementById('test-review').setAttribute('aria-label', 'Completed'); 175 | } 176 | else if(user['variables'] == 'completed' && user['expressions'] == 'completed' && 177 | user['strings'] == 'completed' && user['arrays'] == 'completed' && 178 | user['conditionals'] == 'completed' && user['loops'] == 'completed'){ 179 | document.getElementById('test-review').innerHTML = availableIcon; 180 | document.getElementById('test-review').setAttribute('aria-label', 'Available'); 181 | } 182 | else { 183 | document.getElementById('test-review').innerHTML = restrictedIcon; 184 | document.getElementById('test-review').setAttribute('aria-label', 'Restricted'); 185 | } 186 | if(user['chapter1Score'] != -1 || user['chapter2Score'] != -1 || user['chapter3Score'] != -1 || user['reviewScore'] != -1){ 187 | document.getElementById('test-statistics').innerHTML = availableIcon; 188 | document.getElementById('test-statistics').setAttribute('aria-label', 'Available'); 189 | } 190 | else { 191 | document.getElementById('test-statistics').innerHTML = restrictedIcon; 192 | document.getElementById('test-statistics').setAttribute('aria-label', 'Restricted'); 193 | } 194 | } 195 | 196 | var availableIcon = ''; 197 | 198 | var seenIcon = ''; 199 | 200 | var completedIcon = ''; 201 | 202 | var restrictedIcon = ''; 203 | -------------------------------------------------------------------------------- /1_variables.js: -------------------------------------------------------------------------------- 1 | document.addEventListener('DOMContentLoaded', function(event){ 2 | // Check if saved info, otherwise administer test 3 | if (localStorage.getItem('learn-js-user') === null){ 4 | window.location="./index.html"; 5 | } 6 | else { 7 | user = JSON.parse(localStorage.getItem('learn-js-user')); 8 | if(user['variablesEntryStereotype'] < 0){ 9 | user['variablesEntryStereotype'] = user['stereotype']; 10 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 11 | } 12 | if(user['animal'] == 'dog'){ 13 | document.getElementById('dog-question').className = 'card fluid rounded'; 14 | document.getElementById('cat-question').className = 'card fluid rounded hidden'; 15 | } 16 | if (user['variablesEntryStereotype'] >= 1250 || user['stereotype'] >= 1250 || user['chapter1Score'] > 60) 17 | document.getElementsByTagName('style')[0].innerHTML += 'div.advanced-1250{ display: block;}'; 18 | if(user['experience'] == 'yes') 19 | document.getElementById('variables-explanation').innerHTML = '

Variables allow computers to label and store data, while their labels can be used similarly to the x and y variables you use in mathematics. The only difference between computer and mathematical variables is that computer variables can store any of the seven data types, not just numbers.

'; 20 | if (user['videos'] == 'yes') 21 | document.getElementsByTagName('style')[0].innerHTML += 'div.videos-further{ display: block;}'; 22 | if (user['desiredJsKnowledge'] == 'full') 23 | document.getElementsByTagName('style')[0].innerHTML += 'li.js-further{ display: block;}'; 24 | if (user['desiredProgrammingKnowledge'] == 'full') 25 | document.getElementsByTagName('style')[0].innerHTML += 'li.programming-further{ display: block;}' 26 | } 27 | window.setTimeout(function(){ 28 | if(user['variables'] == 'available' || user['variables'] == 'restricted') 29 | user['variables'] = 'seen'; 30 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 31 | },5000); 32 | }); 33 | 34 | user = {}; 35 | 36 | function checkQuestion(questionId){ 37 | if(questionId == 0){ 38 | var userInput = document.getElementById('question-0').value.trim(); 39 | if(user['variables'] != 'completed'){ 40 | user['variables-question-0'] = userInput; 41 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 42 | } 43 | var inputValues = { 44 | 'definedVar' : userInput.indexOf('var') != -1, 45 | 'definedName' : userInput.indexOf('myVar') != -1, 46 | 'usedSemicolon' : userInput.indexOf(';') != -1, 47 | 'nameAfterVar' : userInput.indexOf(' myVar') > userInput.indexOf('var '), 48 | 'semicolonAfterName' : userInput.indexOf(';') > userInput.indexOf(' myVar') 49 | } 50 | if(!inputValues.definedVar){ 51 | document.getElementById('question-0-message').innerHTML = 'You need to define a variable using the var keyword.'; 52 | document.getElementById('question-0-message').className = 'failure'; 53 | } 54 | else if(!inputValues.definedName){ 55 | document.getElementById('question-0-message').innerHTML = 'You need to define a variable named myVar.'; 56 | document.getElementById('question-0-message').className = 'failure'; 57 | } 58 | else if(!inputValues.usedSemicolon){ 59 | document.getElementById('question-0-message').innerHTML = 'Your variable definition should be followed by a ;.'; 60 | document.getElementById('question-0-message').className = 'failure'; 61 | } 62 | else if(!inputValues.nameAfterVar){ 63 | document.getElementById('question-0-message').innerHTML = 'Your variable\'s name should be after the var keyword.'; 64 | document.getElementById('question-0-message').className = 'failure'; 65 | } 66 | else if(!inputValues.semicolonAfterName){ 67 | document.getElementById('question-0-message').innerHTML = 'You should put the ; at the end of the variable definition.'; 68 | document.getElementById('question-0-message').className = 'failure'; 69 | } 70 | else{ 71 | document.getElementById('question-0-message').innerHTML = 'Correct!'; 72 | document.getElementById('question-0-message').className = 'success'; 73 | user['variables-question-0'] = 'true'; 74 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 75 | } 76 | } 77 | else if(questionId == 1){ 78 | var userInput = document.querySelector('input[name="question-1"]:checked').value; 79 | if(user['variables'] != 'completed'){ 80 | user['variables-question-1'] = userInput; 81 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 82 | } 83 | if(userInput == 'assign-not-initialize'){ 84 | document.getElementById('question-1-message').innerHTML = 'You should assign a value to myNum, not initialize the variable.'; 85 | document.getElementById('question-1-message').className = 'failure'; 86 | } 87 | else if(userInput == 'not-a-number'){ 88 | document.getElementById('question-1-message').innerHTML = 'The value of myNum should be a number.'; 89 | document.getElementById('question-1-message').className = 'failure'; 90 | } 91 | else{ 92 | document.getElementById('question-1-message').innerHTML = 'Correct!'; 93 | document.getElementById('question-1-message').className = 'success'; 94 | } 95 | } 96 | else if(questionId == 2){ 97 | var userInput = document.querySelector('input[name="question-2"]:checked').value; 98 | if(user['variables'] != 'completed'){ 99 | user['variables-question-2'] = userInput; 100 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 101 | } 102 | if(userInput == 'reverse'){ 103 | document.getElementById('question-2-message').innerHTML = 'You should assign the value of a to b, not the other way around.'; 104 | document.getElementById('question-2-message').className = 'failure'; 105 | } 106 | else{ 107 | document.getElementById('question-2-message').innerHTML = 'Correct!'; 108 | document.getElementById('question-2-message').className = 'success'; 109 | } 110 | } 111 | else if(questionId == 3){ 112 | var userInput = document.querySelector('input[name="question-3"]:checked').value; 113 | if(user['variables'] != 'completed'){ 114 | user['variables-question-3'] = userInput; 115 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 116 | } 117 | if(userInput == 'initialize-not-assign'){ 118 | document.getElementById('question-3-message').innerHTML = 'You should initialize the variable myCat, not just assign a value to it.'; 119 | document.getElementById('question-3-message').className = 'failure'; 120 | } 121 | else if(userInput == 'not-a-string'){ 122 | document.getElementById('question-3-message').innerHTML = 'The value of myCat should be a string.'; 123 | document.getElementById('question-3-message').className = 'failure'; 124 | } 125 | else{ 126 | document.getElementById('question-3-message').innerHTML = 'Correct!'; 127 | document.getElementById('question-3-message').className = 'success'; 128 | } 129 | } 130 | else if(questionId == 4){ 131 | var userInput = document.querySelector('input[name="question-4"]:checked').value; 132 | if(user['variables'] != 'completed'){ 133 | user['variables-question-3'] = userInput; 134 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 135 | } 136 | if(userInput == 'initialize-not-assign'){ 137 | document.getElementById('question-4-message').innerHTML = 'You should initialize the variable myDog, not just assign a value to it.'; 138 | document.getElementById('question-4-message').className = 'failure'; 139 | } 140 | else if(userInput == 'not-a-string'){ 141 | document.getElementById('question-4-message').innerHTML = 'The value of myDog should be a string.'; 142 | document.getElementById('question-4-message').className = 'failure'; 143 | } 144 | else{ 145 | document.getElementById('question-4-message').innerHTML = 'Correct!'; 146 | document.getElementById('question-4-message').className = 'success'; 147 | } 148 | } 149 | else if(questionId == 5){ 150 | var userInput = document.querySelector('input[name="question-5"]:checked').value; 151 | if(user['variables'] != 'completed'){ 152 | user['variables-question-5'] = userInput; 153 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 154 | } 155 | if(userInput == 'not-nan'){ 156 | document.getElementById('question-5-message').innerHTML = 'No mathematical operation is applied on variable a.'; 157 | document.getElementById('question-5-message').className = 'failure'; 158 | } 159 | else if(userInput == 'not-zero'){ 160 | document.getElementById('question-5-message').innerHTML = 'Variable a is not explicitly assigned a value of zero.'; 161 | document.getElementById('question-5-message').className = 'failure'; 162 | } 163 | else{ 164 | document.getElementById('question-5-message').innerHTML = 'Correct!'; 165 | document.getElementById('question-5-message').className = 'success'; 166 | user['variablesEntryStereotype'] = user['stereotype']; 167 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 168 | } 169 | } 170 | if(typeof user['variables-question-0'] !== 'undefined' && typeof user['variables-question-1'] !== 'undefined' 171 | && typeof user['variables-question-2'] !== 'undefined' && typeof user['variables-question-3'] !== 'undefined' && 172 | (typeof user['variables-question-5'] !== 'undefined' || user['variablesEntryStereotype'] < 1250) ) 173 | if(user['variables-question-0'] == 'true' && user['variables-question-1'] == 'true' 174 | && user['variables-question-2'] == 'true' && user['variables-question-3'] == 'true' && 175 | (user['variables-question-5'] == 'true' || user['variablesEntryStereotype'] < 1250) ) { 176 | if(user['variables'] != 'completed') user['stereotype'] += 100; 177 | user['variables'] = 'completed'; 178 | if(user['expressions'] == 'restricted') 179 | user['expressions'] = 'available'; 180 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Learn Javascript 6 | 7 | 8 | 9 | 18 | 19 | 20 |

LearnJS

21 |
22 | 103 | 137 |
138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /2_expressions.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Expressions & Statements 6 | 7 | 8 | 9 | 29 | 30 | 31 |
 BackNext 

LearnJS

32 |
33 |

Arithmetic Operations in JavaScript


34 |

Number is a data type in JavaScript which represents numeric data. Now let's try to add two numbers using JavaScript.

35 |

JavaScript uses the + symbol as addition operation when placed between two numbers.

36 |
myVar = 5 + 10; // assigned 15
37 |

We can also subtract one number from another. JavaScript uses the - symbol for subtraction.

38 |
myVar = 12 - 6; // assigned 6
39 |

We can also multiply one number by another. JavaScript uses the * symbol for multiplication of two numbers.

40 |
myVar = 13 * 13; // assigned 169
41 |

We can also divide one number by another. JavaScript uses the / symbol for division.

42 |
myVar = 16 / 2; // assigned 8
43 |
44 |
45 |

The remainder operator


46 |

In JavaScript, you can also get the remainder (or modulo) of an integer division using the % symbol.

47 |
myVar = 11 % 2; // assigned 1
48 |
49 |
50 |
51 |

  Further reading

52 | 56 |
57 |
58 |
59 |
60 |

Questions

61 |
62 |

Please, take the time to read the lesson, before you answer these questions.

63 |
64 |
65 | 66 |
67 | 68 | 69 |
70 |
71 | 72 | 73 |
74 |
75 | 76 | 77 |
78 |
79 | 80 | 81 |
82 | 83 |

84 |
85 |
86 |
87 |
88 | 89 |
90 | 91 | 92 |
93 |
94 | 95 | 96 |
97 |
98 | 99 | 100 |
101 |
102 | 103 | 104 |
105 | 106 |

107 |
108 |
109 |
110 |
111 | 112 |
113 | 114 | 115 |
116 |
117 | 118 | 119 |
120 |
121 | 122 | 123 |
124 |
125 | 126 | 127 |
128 | 129 |

130 |
131 |
132 |
133 |
134 | 135 |
136 | 137 | 138 |
139 |
140 | 141 | 142 |
143 |
144 | 145 | 146 |
147 |
148 | 149 | 150 |
151 | 152 |

153 |
154 |
155 |
156 |
157 | 158 |
159 | 160 | 161 |
162 |
163 | 164 | 165 |
166 | 167 |

168 |
169 |
170 |
171 |
172 | 173 | 174 | 175 | 176 | -------------------------------------------------------------------------------- /5_conditionals.js: -------------------------------------------------------------------------------- 1 | document.addEventListener('DOMContentLoaded', function(event){ 2 | // Check if saved info, otherwise administer test 3 | if (localStorage.getItem('learn-js-user') === null){ 4 | window.location="./index.html"; 5 | } 6 | else { 7 | user = JSON.parse(localStorage.getItem('learn-js-user')); 8 | if(user['conditionalsEntryStereotype'] < 0){ 9 | user['conditionalsEntryStereotype'] = user['stereotype']; 10 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 11 | } 12 | if (user['conditionalsEntryStereotype'] >= 13250 || user['stereotype'] >= 1350 || user['chapter3Score'] > 60) 13 | document.getElementsByTagName('style')[0].innerHTML += 'div.advanced-1350{ display: block;}'; 14 | if (user['videos'] == 'yes') 15 | document.getElementsByTagName('style')[0].innerHTML += 'div.videos-further{ display: block;}'; 16 | if (user['desiredJsKnowledge'] == 'full' || user['desiredProgrammingKnowledge'] == 'full') 17 | document.getElementsByTagName('style')[0].innerHTML += 'div.read-further{ display: block;}'; 18 | if (user['desiredJsKnowledge'] == 'full') 19 | document.getElementsByTagName('style')[0].innerHTML += 'li.js-further{ display: list-item;}'; 20 | if (user['desiredProgrammingKnowledge'] == 'full') 21 | document.getElementsByTagName('style')[0].innerHTML += 'li.programming-further{ display: list-item;}'; 22 | if (user['desiredJsKnowledge'] == 'full' && user['desiredProgrammingKnowledge'] == 'full') 23 | document.getElementsByTagName('style')[0].innerHTML += 'li.js-p-further{ display: list-item;}'; 24 | } 25 | window.setTimeout(function(){ 26 | if(user['conditionals'] == 'available' || user['conditionals'] == 'restricted') 27 | user['conditionals'] = 'seen'; 28 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 29 | },5000); 30 | }); 31 | 32 | user = {}; 33 | 34 | function checkQuestion(questionId){ 35 | if(questionId == 0){ 36 | var userInput = document.querySelector('input[name="question-0"]:checked').value; 37 | if(user['conditionals'] != 'completed'){ 38 | user['conditionals-question-0'] = userInput; 39 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 40 | } 41 | if(userInput == 'false'){ 42 | document.getElementById('question-0-message').innerHTML = 'Remember that when the condition evaluates to true, the program executes the statement inside the curly braces'; 43 | document.getElementById('question-0-message').className = 'failure'; 44 | if(user['conditionalsMistakes'][questionId] == 0) { 45 | user['conditionalsMistakes'][questionId] = 1; 46 | user['stereotype'] -= 5; 47 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 48 | } 49 | } 50 | else{ 51 | document.getElementById('question-0-message').innerHTML = 'Correct!'; 52 | document.getElementById('question-0-message').className = 'success'; 53 | if(user['conditionalsMistakes'][questionId] == 0) { 54 | user['conditionalsMistakes'][questionId] = -1; 55 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 56 | } 57 | } 58 | } 59 | else if(questionId == 1){ 60 | var userInput = document.querySelector('input[name="question-1"]:checked').value; 61 | if(user['conditionals'] != 'completed'){ 62 | user['conditionals-question-1'] = userInput; 63 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 64 | } 65 | if(userInput == 'notEqual'){ 66 | document.getElementById('question-1-message').innerHTML = 'Remember that when the boolean condition is false, the code in the code block is not executed'; 67 | document.getElementById('question-1-message').className = 'failure'; 68 | if(user['conditionalsMistakes'][questionId] == 0) { 69 | user['conditionalsMistakes'][questionId] = 1; 70 | user['stereotype'] -= 5; 71 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 72 | } 73 | } 74 | else if(userInput == 'allFalse'){ 75 | document.getElementById('question-1-message').innerHTML = 'Remember that only when all boolean condition are false the code block associated with the else keyword is executed'; 76 | document.getElementById('question-1-message').className = 'failure'; 77 | if(user['conditionalsMistakes'][questionId] == 0) { 78 | user['conditionalsMistakes'][questionId] = 1; 79 | user['stereotype'] -= 5; 80 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 81 | } 82 | } 83 | else{ 84 | document.getElementById('question-1-message').innerHTML = 'Correct!'; 85 | document.getElementById('question-1-message').className = 'success'; 86 | if(user['conditionalsMistakes'][questionId] == 0) { 87 | user['conditionalsMistakes'][questionId] = -1; 88 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 89 | } 90 | } 91 | } 92 | else if(questionId == 2){ 93 | var userInput = document.querySelector('input[name="question-2"]:checked').value; 94 | if(user['conditionals'] != 'completed'){ 95 | user['conditionals-question-2'] = userInput; 96 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 97 | } 98 | if(userInput == 'alpha' || userInput == 'gamma'){ 99 | document.getElementById('question-2-message').innerHTML = 'A switch statement looks for the first case clause whose expression evaluates to the same value as the result of the input expression'; 100 | document.getElementById('question-2-message').className = 'failure'; 101 | if(user['conditionalsMistakes'][questionId] == 0) { 102 | user['conditionalsMistakes'][questionId] = 1; 103 | user['stereotype'] -= 5; 104 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 105 | } 106 | } 107 | else{ 108 | document.getElementById('question-2-message').innerHTML = 'Correct!'; 109 | document.getElementById('question-2-message').className = 'success'; 110 | if(user['conditionalsMistakes'][questionId] == 0) { 111 | user['conditionalsMistakes'][questionId] = -1; 112 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 113 | } 114 | } 115 | } 116 | else if(questionId == 3){ 117 | var userInput = document.querySelector('input[name="question-3"]:checked').value; 118 | if(user['conditionals'] != 'completed'){ 119 | user['conditionals-question-3'] = userInput; 120 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 121 | } 122 | if(userInput == 'false'){ 123 | document.getElementById('question-3-message').innerHTML = 'The default statement will be executed if no matching case statements are found.'; 124 | document.getElementById('question-3-message').className = 'failure'; 125 | if(user['conditionalsMistakes'][questionId] == 0) { 126 | user['conditionalsMistakes'][questionId] = 1; 127 | user['stereotype'] -= 5; 128 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 129 | } 130 | } 131 | else{ 132 | document.getElementById('question-3-message').innerHTML = 'Correct!'; 133 | document.getElementById('question-3-message').className = 'success'; 134 | if(user['conditionalsMistakes'][questionId] == 0) { 135 | user['conditionalsMistakes'][questionId] = -1; 136 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 137 | } 138 | } 139 | } 140 | else if(questionId == 4){ 141 | var userInput = document.querySelector('input[name="question-4"]:checked').value; 142 | if(user['conditionals'] != 'completed'){ 143 | user['conditionals-question-4'] = userInput; 144 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 145 | } 146 | if(userInput == 'false'){ 147 | document.getElementById('question-4-message').innerHTML = 'The condition evaluates to true when the number is between 6 and 8 or below 3.'; 148 | document.getElementById('question-4-message').className = 'failure'; 149 | if(user['conditionalsMistakes'][questionId] == 0) { 150 | user['conditionalsMistakes'][questionId] = 1; 151 | user['stereotype'] -= 5; 152 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 153 | } 154 | } 155 | else{ 156 | document.getElementById('question-4-message').innerHTML = 'Correct!'; 157 | document.getElementById('question-4-message').className = 'success'; 158 | if(user['conditionalsMistakes'][questionId] == 0) { 159 | user['conditionalsMistakes'][questionId] = -1; 160 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 161 | } 162 | } 163 | } 164 | else if(questionId == 5){ 165 | var userInput = document.querySelector('input[name="question-5"]:checked').value; 166 | if(user['conditionals'] != 'completed'){ 167 | user['conditionals-question-5'] = userInput; 168 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 169 | } 170 | if(userInput == 'false'){ 171 | document.getElementById('question-5-message').innerHTML = 'The first part of the statement is applied only if the condition evaluates to true.'; 172 | document.getElementById('question-5-message').className = 'failure'; 173 | if(user['conditionalsMistakes'][questionId] == 0) { 174 | user['conditionalsMistakes'][questionId] = 1; 175 | user['stereotype'] -= 5; 176 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 177 | } 178 | } 179 | else{ 180 | document.getElementById('question-5-message').innerHTML = 'Correct!'; 181 | document.getElementById('question-5-message').className = 'success'; 182 | user['conditionalsEntryStereotype'] = user['stereotype']; 183 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 184 | if(user['conditionalsMistakes'][questionId] == 0) { 185 | user['conditionalsMistakes'][questionId] = -1; 186 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 187 | } 188 | } 189 | } 190 | if(typeof user['conditionals-question-0'] !== 'undefined' && typeof user['conditionals-question-1'] !== 'undefined' 191 | && typeof user['conditionals-question-2'] !== 'undefined' && typeof user['conditionals-question-3'] !== 'undefined' && typeof user['conditionals-question-4'] !== 'undefined' && 192 | (typeof user['conditionals-question-5'] !== 'undefined' || user['conditionalsEntryStereotype'] < 1350) ) 193 | if(user['conditionals-question-0'] == 'true' && user['conditionals-question-1'] == 'true' 194 | && user['conditionals-question-2'] == 'true' && user['conditionals-question-3'] == 'true' && user['conditionals-question-4'] == 'true'&& 195 | (user['conditionals-question-5'] == 'true' || user['conditionalsEntryStereotype'] < 1350) ) { 196 | user['conditionals'] = 'completed'; 197 | if(user['conditionals'] != 'completed') user['stereotype'] += 50; 198 | if(user['loops'] == 'restricted') 199 | user['loops'] = 'available'; 200 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 201 | } 202 | } 203 | -------------------------------------------------------------------------------- /6_loops.js: -------------------------------------------------------------------------------- 1 | document.addEventListener('DOMContentLoaded', function(event){ 2 | // Check if saved info, otherwise administer test 3 | if (localStorage.getItem('learn-js-user') === null){ 4 | window.location="./index.html"; 5 | } 6 | else { 7 | user = JSON.parse(localStorage.getItem('learn-js-user')); 8 | if(user['loopsEntryStereotype'] < 0){ 9 | user['loopsEntryStereotype'] = user['stereotype']; 10 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 11 | } 12 | if (user['loopsEntryStereotype'] >= 1400 || user['stereotype'] >= 1400 || user['chapter3Score'] > 60) 13 | document.getElementsByTagName('style')[0].innerHTML += 'div.advanced-1400{ display: block;}'; 14 | if (user['videos'] == 'yes') 15 | document.getElementsByTagName('style')[0].innerHTML += 'div.videos-further{ display: block;}'; 16 | if (user['desiredJsKnowledge'] == 'full' || user['desiredProgrammingKnowledge'] == 'full') 17 | document.getElementsByTagName('style')[0].innerHTML += 'div.read-further{ display: block;}'; 18 | if (user['desiredJsKnowledge'] == 'full') 19 | document.getElementsByTagName('style')[0].innerHTML += 'li.js-further{ display: list-item;}'; 20 | if (user['desiredProgrammingKnowledge'] == 'full') 21 | document.getElementsByTagName('style')[0].innerHTML += 'li.programming-further{ display: list-item;}'; 22 | if (user['desiredJsKnowledge'] == 'full' && user['desiredProgrammingKnowledge'] == 'full') 23 | document.getElementsByTagName('style')[0].innerHTML += 'li.js-p-further{ display: list-item;}'; 24 | } 25 | window.setTimeout(function(){ 26 | if(user['loops'] == 'available' || user['loops'] == 'restricted') 27 | user['loops'] = 'seen'; 28 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 29 | },5000); 30 | }); 31 | 32 | user = {}; 33 | 34 | function checkQuestion(questionId){ 35 | if(questionId == 0){ 36 | var userInput = document.querySelector('input[name="question-0"]:checked').value; 37 | if(user['loops'] != 'completed'){ 38 | user['loops-question-0'] = userInput; 39 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 40 | } 41 | if(userInput == 'initialization'){ 42 | document.getElementById('question-0-message').innerHTML = 'This is the initialization statement. It is typically used to define and setup your loop variable.'; 43 | document.getElementById('question-0-message').className = 'failure'; 44 | if(user['loopsMistakes'][questionId] == 0) { 45 | user['loopsMistakes'][questionId] = 1; 46 | user['stereotype'] -= 5; 47 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 48 | } 49 | } 50 | else if(userInput == 'codeBlock'){ 51 | document.getElementById('question-0-message').innerHTML = 'This is the code block that is to be executed'; 52 | document.getElementById('question-0-message').className = 'failure'; 53 | if(user['loopsMistakes'][questionId] == 0) { 54 | user['loopsMistakes'][questionId] = 1; 55 | user['stereotype'] -= 5; 56 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 57 | } 58 | } 59 | else if(userInput == 'finalExpression'){ 60 | document.getElementById('question-0-message').innerHTML = 'This is final-expression. It is usually used to increment or decrement your loop counter.'; 61 | document.getElementById('question-0-message').className = 'failure'; 62 | if(user['loopsMistakes'][questionId] == 0) { 63 | user['loopsMistakes'][questionId] = 1; 64 | user['stereotype'] -= 5; 65 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 66 | } 67 | } 68 | else{ 69 | document.getElementById('question-0-message').innerHTML = 'Correct!'; 70 | document.getElementById('question-0-message').className = 'success'; 71 | if(user['loopsMistakes'][questionId] == 0) { 72 | user['loopsMistakes'][questionId] = -1; 73 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 74 | } 75 | } 76 | } 77 | else if(questionId == 1){ 78 | var userInput = document.querySelector('input[name="question-1"]:checked').value; 79 | if(user['loops'] != 'completed'){ 80 | user['loops-question-1'] = userInput; 81 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 82 | } 83 | if(userInput == 'iteration'){ 84 | document.getElementById('question-1-message').innerHTML = 'The loop runs for i from 1 up to (but not including) 6'; 85 | document.getElementById('question-1-message').className = 'failure'; 86 | if(user['loopsMistakes'][questionId] == 0) { 87 | user['loopsMistakes'][questionId] = 1; 88 | user['stereotype'] -= 5; 89 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 90 | } 91 | } 92 | else if(userInput == 'iValue'){ 93 | document.getElementById('question-1-message').innerHTML = 'The loop variable i is initialized to start at 1.'; 94 | document.getElementById('question-1-message').className = 'failure'; 95 | if(user['loopsMistakes'][questionId] == 0) { 96 | user['loopsMistakes'][questionId] = 1; 97 | user['stereotype'] -= 5; 98 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 99 | } 100 | } 101 | else{ 102 | document.getElementById('question-1-message').innerHTML = 'Correct!'; 103 | document.getElementById('question-1-message').className = 'success'; 104 | if(user['loopsMistakes'][questionId] == 0) { 105 | user['loopsMistakes'][questionId] = -1; 106 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 107 | } 108 | } 109 | } 110 | else if(questionId == 2){ 111 | var userInput = document.getElementById('question-2').value; 112 | if(user['loops'] != 'completed'){ 113 | user['loops-question-2'] = userInput; 114 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 115 | } 116 | if(userInput < 10 || userInput > 10 && userInput != 15){ 117 | document.getElementById('question-2-message').innerHTML = 'The loop stop executing when i = 5'; 118 | document.getElementById('question-2-message').className = 'failure'; 119 | if(user['loopsMistakes'][questionId] == 0) { 120 | user['loopsMistakes'][questionId] = 1; 121 | user['stereotype'] -= 5; 122 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 123 | } 124 | } 125 | else if(userInput == 15){ 126 | document.getElementById('question-2-message').innerHTML = 'The loop runs for i from 0 up to (but not including) 5'; 127 | document.getElementById('question-2-message').className = 'failure'; 128 | if(user['loopsMistakes'][questionId] == 0) { 129 | user['loopsMistakes'][questionId] = 1; 130 | user['stereotype'] -= 5; 131 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 132 | } 133 | } 134 | else{ 135 | document.getElementById('question-2-message').innerHTML = 'Correct!'; 136 | document.getElementById('question-2-message').className = 'success'; 137 | if(user['loopsMistakes'][questionId] == 0) { 138 | user['loopsMistakes'][questionId] = -1; 139 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 140 | } 141 | if(user['loops'] != 'completed'){ 142 | user['loops-question-2'] = 'true'; 143 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 144 | } 145 | } 146 | } 147 | else if(questionId == 3){ 148 | var userInput = document.querySelector('input[name="question-3"]:checked').value; 149 | if(user['loops'] != 'completed'){ 150 | user['loops-question-3'] = userInput; 151 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 152 | } 153 | if(userInput == 'iteration'){ 154 | document.getElementById('question-3-message').innerHTML = 'The loop runs for i from 1 up to (but not including) 4'; 155 | document.getElementById('question-3-message').className = 'failure'; 156 | if(user['loopsMistakes'][questionId] == 0) { 157 | user['loopsMistakes'][questionId] = 1; 158 | user['stereotype'] -= 5; 159 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 160 | } 161 | } 162 | else if(userInput == 'append'){ 163 | document.getElementById('question-3-message').innerHTML = 'Remember that the .push() function appends data to the end of an array'; 164 | document.getElementById('question-3-message').className = 'failure'; 165 | if(user['loopsMistakes'][questionId] == 0) { 166 | user['loopsMistakes'][questionId] = 1; 167 | user['stereotype'] -= 5; 168 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 169 | } 170 | } 171 | else{ 172 | document.getElementById('question-3-message').innerHTML = 'Correct!'; 173 | document.getElementById('question-3-message').className = 'success'; 174 | user['loopsEntryStereotype'] = user['stereotype']; 175 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 176 | if(user['loopsMistakes'][questionId] == 0) { 177 | user['loopsMistakes'][questionId] = -1; 178 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 179 | } 180 | } 181 | } 182 | 183 | else if(questionId == 4){ 184 | var userInput = document.querySelector('input[name="question-4"]:checked').value; 185 | if(user['loops'] != 'completed'){ 186 | user['loops-question-4'] = userInput; 187 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 188 | } 189 | if(userInput == 'condition-at-end'){ 190 | document.getElementById('question-4-message').innerHTML = 'Remember that the condition is evaluated at the end of the loop, after the code inside the loop is executed.'; 191 | document.getElementById('question-4-message').className = 'failure'; 192 | if(user['loopsMistakes'][questionId] == 0) { 193 | user['loopsMistakes'][questionId] = 1; 194 | user['stereotype'] -= 5; 195 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 196 | } 197 | } 198 | else{ 199 | document.getElementById('question-4-message').innerHTML = 'Correct!'; 200 | document.getElementById('question-4-message').className = 'success'; 201 | if(user['loopsMistakes'][questionId] == 0) { 202 | user['loopsMistakes'][questionId] = -1; 203 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 204 | } 205 | } 206 | } 207 | if(typeof user['loops-question-0'] !== 'undefined' && typeof user['loops-question-1'] !== 'undefined' 208 | && typeof user['loops-question-2'] !== 'undefined' && typeof user['loops-question-3'] !== 'undefined' && 209 | (typeof user['loops-question-4'] !== 'undefined' || user['loopsEntryStereotype'] < 1400) ) 210 | if(user['loops-question-0'] == 'true' && user['loops-question-1'] == 'true' 211 | && user['loops-question-2'] == 'true' && user['loops-question-3'] == 'true'&& 212 | (user['loops-question-4'] == 'true' || user['loopsEntryStereotype'] < 1400) ) { 213 | user['loops'] = 'completed'; 214 | if(user['loops'] != 'completed') user['stereotype'] += 50; 215 | // if(user['expressions'] == 'restricted') 216 | // user['expressions'] = 'available'; 217 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /3_strings.js: -------------------------------------------------------------------------------- 1 | document.addEventListener('DOMContentLoaded', function(event){ 2 | // Check if saved info, otherwise administer test 3 | if (localStorage.getItem('learn-js-user') === null){ 4 | window.location="./index.html"; 5 | } 6 | else { 7 | user = JSON.parse(localStorage.getItem('learn-js-user')); 8 | if(user['stringsEntryStereotype'] < 0){ 9 | user['stringsEntryStereotype'] = user['stereotype']; 10 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 11 | } 12 | if (user['stringsEntryStereotype'] >= 1300 || user['stereotype'] >= 1300 || user['chapter2Score'] > 60) 13 | document.getElementsByTagName('style')[0].innerHTML += 'div.advanced-1300{ display: block;}'; 14 | if(user['animal'] == 'cat'){ 15 | document.getElementById('cat-question').className = 'card fluid rounded'; 16 | document.getElementById('dog-question').className = 'card fluid rounded hidden'; 17 | } 18 | if (user['videos'] == 'yes') 19 | document.getElementsByTagName('style')[0].innerHTML += 'div.videos-further{ display: block;}'; 20 | if (user['desiredJsKnowledge'] == 'full' || user['desiredProgrammingKnowledge'] == 'full') 21 | document.getElementsByTagName('style')[0].innerHTML += 'div.read-further{ display: block;}'; 22 | if (user['desiredJsKnowledge'] == 'full') 23 | document.getElementsByTagName('style')[0].innerHTML += 'li.js-further{ display: list-item;}'; 24 | if (user['desiredProgrammingKnowledge'] == 'full') 25 | document.getElementsByTagName('style')[0].innerHTML += 'li.programming-further{ display: list-item;}'; 26 | if (user['desiredJsKnowledge'] == 'full' && user['desiredProgrammingKnowledge'] == 'full') 27 | document.getElementsByTagName('style')[0].innerHTML += 'li.js-p-further{ display: list-item;}'; 28 | } 29 | window.setTimeout(function(){ 30 | if(user['strings'] == 'available' || user['strings'] == 'restricted') 31 | user['strings'] = 'seen'; 32 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 33 | },5000); 34 | }); 35 | 36 | user = {}; 37 | 38 | function checkQuestion(questionId){ 39 | if(questionId == 0){ 40 | var userInput = document.querySelector('input[name="question-0"]:checked').value; 41 | if(user['strings'] != 'completed'){ 42 | user['strings-question-0'] = userInput; 43 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 44 | } 45 | if(userInput == 'false'){ 46 | document.getElementById('question-0-message').innerHTML = 'Remember that a string literal is a series of zero or more characters enclosed in single or double quotes.'; 47 | document.getElementById('question-0-message').className = 'failure'; 48 | if(user['stringsMistakes'][questionId] == 0) { 49 | user['stringsMistakes'][questionId] = 1; 50 | user['stereotype'] -= 5; 51 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 52 | } 53 | } 54 | else{ 55 | document.getElementById('question-0-message').innerHTML = 'Correct!'; 56 | document.getElementById('question-0-message').className = 'success'; 57 | if(user['stringsMistakes'][questionId] == 0) { 58 | user['stringsMistakes'][questionId] = -1; 59 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 60 | } 61 | } 62 | } 63 | else if(questionId == 1){ 64 | var userInput = document.getElementById('question-1').value; 65 | if(user['strings'] != 'completed'){ 66 | user['strings-question-1'] = userInput; 67 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 68 | } 69 | if(userInput < 14 || userInput > 17 || userInput == 16){ 70 | document.getElementById('question-1-message').innerHTML = 'The .length of a string is the number of characters in the string.'; 71 | document.getElementById('question-1-message').className = 'failure'; 72 | if(user['stringsMistakes'][questionId] == 0) { 73 | user['stringsMistakes'][questionId] = 1; 74 | user['stereotype'] -= 5; 75 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 76 | } 77 | } 78 | else if(userInput == 14){ 79 | document.getElementById('question-1-message').innerHTML = 'The .length of a string is not zero-indexed.'; 80 | document.getElementById('question-1-message').className = 'failure'; 81 | if(user['stringsMistakes'][questionId] == 0) { 82 | user['stringsMistakes'][questionId] = 1; 83 | user['stereotype'] -= 5; 84 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 85 | } 86 | } 87 | else if(userInput == 17){ 88 | document.getElementById('question-1-message').innerHTML = 'The .length of a string doesn\'t take quotations into account.'; 89 | document.getElementById('question-1-message').className = 'failure'; 90 | if(user['stringsMistakes'][questionId] == 0) { 91 | user['stringsMistakes'][questionId] = 1; 92 | user['stereotype'] -= 5; 93 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 94 | } 95 | } 96 | else{ 97 | document.getElementById('question-1-message').innerHTML = 'Correct!'; 98 | document.getElementById('question-1-message').className = 'success'; 99 | if(user['strings'] != 'completed'){ 100 | user['strings-question-1'] = 'true'; 101 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 102 | if(user['stringsMistakes'][questionId] == 0) { 103 | user['stringsMistakes'][questionId] = -1; 104 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 105 | } 106 | } 107 | } 108 | } 109 | else if(questionId == 2){ 110 | var userInput = document.querySelector('input[name="question-2"]:checked').value; 111 | if(user['strings'] != 'completed'){ 112 | user['strings-question-2'] = userInput; 113 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 114 | } 115 | if(userInput == 'spaced'){ 116 | document.getElementById('question-2-message').innerHTML = 'Remember that string concatenation doesn\'t add spaces between concatenated strings.'; 117 | document.getElementById('question-2-message').className = 'failure'; 118 | if(user['stringsMistakes'][questionId] == 0) { 119 | user['stringsMistakes'][questionId] = 1; 120 | user['stereotype'] -= 5; 121 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 122 | } 123 | } 124 | else{ 125 | document.getElementById('question-2-message').innerHTML = 'Correct!'; 126 | document.getElementById('question-2-message').className = 'success'; 127 | if(user['stringsMistakes'][questionId] == 0) { 128 | user['stringsMistakes'][questionId] = -1; 129 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 130 | } 131 | } 132 | } 133 | else if(questionId == 3){ 134 | var userInput = document.querySelector('input[name="question-3"]:checked').value; 135 | if(user['strings'] != 'completed'){ 136 | user['strings-question-3'] = userInput; 137 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 138 | } 139 | if(userInput == 'non-zero-indexed'){ 140 | document.getElementById('question-3-message').innerHTML = 'Remember that Javascript used zero-based indexing.'; 141 | document.getElementById('question-3-message').className = 'failure'; 142 | if(user['stringsMistakes'][3] == 0) { 143 | user['stringsMistakes'][3] = 1; 144 | user['stereotype'] -= 5; 145 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 146 | } 147 | } 148 | else{ 149 | document.getElementById('question-3-message').innerHTML = 'Correct!'; 150 | document.getElementById('question-3-message').className = 'success'; 151 | if(user['stringsMistakes'][3] == 0) { 152 | user['stringsMistakes'][3] = -1; 153 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 154 | } 155 | } 156 | } 157 | else if(questionId == 4){ 158 | var userInput = document.querySelector('input[name="question-4"]:checked').value; 159 | if(user['strings'] != 'completed'){ 160 | user['strings-question-3'] = userInput; 161 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 162 | } 163 | if(userInput == 'non-zero-indexed'){ 164 | document.getElementById('question-4-message').innerHTML = 'Remember that Javascript used zero-based indexing.'; 165 | document.getElementById('question-4-message').className = 'failure'; 166 | if(user['stringsMistakes'][3] == 0) { 167 | user['stringsMistakes'][3] = 1; 168 | user['stereotype'] -= 5; 169 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 170 | } 171 | } 172 | else{ 173 | document.getElementById('question-4-message').innerHTML = 'Correct!'; 174 | document.getElementById('question-4-message').className = 'success'; 175 | if(user['stringsMistakes'][3] == 0) { 176 | user['stringsMistakes'][3] = -1; 177 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 178 | } 179 | } 180 | } 181 | else if(questionId == 5){ 182 | var userInput = document.querySelector('input[name="question-5"]:checked').value; 183 | if(user['strings'] != 'completed'){ 184 | user['strings-question-4'] = userInput; 185 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 186 | } 187 | if(userInput == 'not-addition'){ 188 | document.getElementById('question-5-message').innerHTML = 'Using the + operator on a string and a numeric variable performs concatenation, not addition.'; 189 | document.getElementById('question-5-message').className = 'failure'; 190 | if(user['stringsMistakes'][4] == 0) { 191 | user['stringsMistakes'][4] = 1; 192 | user['stereotype'] -= 5; 193 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 194 | } 195 | } 196 | if(userInput == 'not-a-number'){ 197 | document.getElementById('question-5-message').innerHTML = 'The result of a concatenation between a string and a numeric variable is not a number.'; 198 | document.getElementById('question-5-message').className = 'failure'; 199 | if(user['stringsMistakes'][4] == 0) { 200 | user['stringsMistakes'][4] = 1; 201 | user['stereotype'] -= 5; 202 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 203 | } 204 | } 205 | else{ 206 | document.getElementById('question-5-message').innerHTML = 'Correct!'; 207 | document.getElementById('question-5-message').className = 'success'; 208 | user['stringsEntryStereotype'] = user['stereotype']; 209 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 210 | if(user['stringsMistakes'][4] == 0) { 211 | user['stringsMistakes'][4] = -1; 212 | localStorage.setItem('learn-js-user', JSON.stringify(user)); 213 | } 214 | } 215 | } 216 | if(typeof user['strings-question-0'] !== 'undefined' && typeof user['strings-question-1'] !== 'undefined' 217 | && typeof user['strings-question-2'] !== 'undefined' && typeof user['strings-question-3'] !== 'undefined' && 218 | (typeof user['strings-question-4'] !== 'undefined' || user['stringsEntryStereotype'] < 1300) ) 219 | if(user['strings-question-0'] == 'true' && user['strings-question-1'] == 'true' 220 | && user['strings-question-2'] == 'true' && user['strings-question-3'] == 'true'&& 221 | (user['strings-question-4'] == 'true' || user['stringsEntryStereotype'] < 1300) ) { 222 | if(user['strings'] != 'completed') user['stereotype'] += 50; 223 | user['strings'] = 'completed'; 224 | if(user['arrays'] == 'restricted') 225 | user['arrays'] = 'available'; 226 | localStorage.setItem('learn-js-user',JSON.stringify(user)); 227 | } 228 | } 229 | -------------------------------------------------------------------------------- /3_strings.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Strings 6 | 7 | 8 | 9 | 29 | 30 | 31 |
 BackNext 

LearnJS

32 |
33 |

Declare String Variables


34 |

Previously we have used the following code:

35 |
var myName = "your name";
36 |

"your name" is called a string literal. It is a string because it is a series of zero or more characters enclosed in single or double quotes.

37 |
38 | 39 |

Concatenating Strings with Plus Operator


40 |

In JavaScript, when the + operator is used with a String value, it is called the concatenation operator. You can build a new string out of other strings by concatenating them together. For example:

41 |
'My name is Alan,' + ' I concatenate.'
42 |

Remember to watch out for spaces. Concatenation does not add spaces between concatenated strings, so you'll need to add them yourself.

43 |
44 | 45 |

Find the Length of a String


46 |

You can find the length of a String value by writing .length after the string variable or string literal.

47 |
"Alan Peter".length; // 10
48 |

For example, if we created a variable var firstName = "Charles", we could find out how long the string "Charles" is by using the firstName.length property.

49 |
50 | 51 |

Use Bracket Notation to Find a Character in a String


52 |

Bracket notation is a way to get a character at a specific index within a string.

53 |

Most modern programming languages, like JavaScript, don't start counting at 1 like humans do. They start at 0. This is referred to as zero-based indexing.

54 |

For example, the character at index 0 in the word "Charles" is "C". So if var firstName = "Charles", you can get the value of the first letter of the string by using firstName[0].

55 |

You can also use bracket notation to get the character at other positions within a string. For example:

56 |
"Charles"[2] // "a"
57 |
58 |
59 |

Concatenating Strings with Variables


60 |

Apart from concatenating two strings, you can also concatenate a string with any other type of variable For example:

61 |
var age = 25;
 62 | var ageString = "I am " + age + " years old.";
63 |
64 |
65 | 75 |
76 |

  Videos

77 | 78 |

79 |
80 |
81 |
82 |

Questions

83 |
84 |

Please, take the time to read the lesson, before you answer these questions.

85 |
86 |
87 | 89 |
90 | 91 | 92 |
93 |
94 | 95 | 96 |
97 | 98 |

99 |
100 |
101 |
102 |
103 | 105 | 106 | 107 |

108 |
109 |
110 |
111 |
112 | 114 |
115 | 116 | 117 |
118 |
119 | 120 | 121 |
122 | 123 |

124 |
125 |
126 |
127 |
128 | 129 |
130 | 131 | 132 |
133 |
134 | 135 | 136 |
137 |
138 | 139 | 140 |
141 | 142 |

143 |
144 |
145 | 164 |
165 |
166 | 167 |
168 | 169 | 170 |
171 |
172 | 173 | 174 |
175 |
176 | 177 | 178 |
179 | 180 |

181 |
182 |
183 |
184 |
185 | 186 | 187 | 188 | 189 | -------------------------------------------------------------------------------- /1_variables.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Variables 6 | 7 | 8 | 9 | 29 | 30 | 31 |
 BackNext 

LearnJS

32 |
33 |

Declare JavaScript Variables


34 |

In computer science, data is anything that is meaningful to the computer. JavaScript provides seven different data types which are undefined, null, boolean, string, symbol, number, and object.

35 |

For example, computers distinguish between numbers, such as the number 12, and strings, such as "12", "dog", or "123 cats", which are collections of characters. Computers can perform mathematical operations on a number, but not on a string.

36 | 37 |

Variables allow computers to store and manipulate data in a dynamic fashion. They do this by using a label to point to the data rather than using the data itself. Any of the seven data types may be stored in a variable.

38 |

Variables are similar to the x and y variables you use in mathematics, which means they're a simple name to represent the data we want to refer to. Computer variables differ from mathematical variables in that they can store different values at different times.

39 |
40 |

We tell JavaScript to create or declare a variable by putting the keyword var in front of it, like so:

41 |
var ourName;
42 |

The above code creates a variable called ourName. In JavaScript we end statements with semicolons.

43 |

Variable names can be made up of numbers, letters, and $ or _, but may not contain spaces or start with a number.

44 |
45 | 46 |

Storing Values with the Assignment Operator


47 |

In JavaScript, you can store a value in a variable with the assignment operator.

48 |
myVariable = 5;
49 |

The above code assigns the Number value 5 to myVariable.

50 |

Assignment always goes from right to left. Everything to the right of the = operator is resolved before the value is assigned to the variable to the left of the operator.

51 |
myVar = 5;
 52 | myNum = myVar;
53 |

The above code assigns 5 to myVar and then resolves myVar to 5 again and assigns it to myNum.

54 |
55 | 56 |

Initializing Variables with the Assignment Operator


57 |

It is common to initialize a variable to an initial value in the same line as it is declared.

58 |
var myVar = 0;
59 |

The above code creates a new variable called myVar and assigns it an initial value of 0.

60 |
61 | 62 |
63 |

Understanding Uninitialized Variables


64 |

When JavaScript variables are declared, they have an initial value of undefined. If you do a mathematical operation on an undefined variable your result will be NaN which means Not a Number.

65 |
66 |
67 | 68 |
69 |

  Further reading

70 | 75 |
76 |
77 |
78 |

  Videos

79 | 80 |

81 |
82 |
83 |
84 |

Questions

85 |
86 |

Please, take the time to read the lesson, before you answer these questions.

87 |
88 |
89 | 90 | 91 | 92 |

93 |
94 |
95 |
96 |
97 | 98 |
99 | 100 | 101 |
102 |
103 | 104 | 105 |
106 |
107 | 108 | 109 |
110 | 111 |

112 |
113 |
114 |
115 |
116 | 117 |
118 | 119 | 120 |
121 |
122 | 123 | 124 |
125 | 126 |

127 |
128 |
129 |
130 |
131 | 132 |
133 | 134 | 135 |
136 |
137 | 138 | 139 |
140 |
141 | 142 | 143 |
144 | 145 |

146 |
147 |
148 | 167 |
168 |
169 | 170 |
171 | 172 | 173 |
174 |
175 | 176 | 177 |
178 |
179 | 180 | 181 |
182 | 183 |

184 |
185 |
186 |
187 |
188 | 189 | 190 | 191 | 192 | -------------------------------------------------------------------------------- /4_arrays.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Arrays 6 | 7 | 8 | 9 | 29 | 30 | 31 |
 BackNext 

LearnJS

32 |
33 |

Store Multiple Values in one Variable using JavaScript Arrays


34 |

With JavaScript array variables, we can store several pieces of data in one place. You start an array declaration with an opening square bracket, end it with a closing square bracket, and put a comma between each entry, like this:

35 |
var sandwich = ["peanut butter", "jelly", "bread"]
36 |
37 | 38 |

Access Array Data with Indexes


39 |

We can access the data inside arrays using indexes. Array indexes are written in the same bracket notation that strings use, except that instead of specifying a character, they are specifying an entry in the array. Like strings, arrays use zero-based indexing, so the first element in an array is element 0. For example:

40 |
var array = [1,2,3];
 41 | array[0]; // equals 1
 42 | var data = array[1];  // equals 2
43 |
44 | 45 |

Modify Array Data With Indexes


46 |

Unlike strings, the entries of arrays are mutable and can be changed freely. For example:

47 |
var ourArray = [3,2,1];
 48 | ourArray[0] = 1; // equals [1,2,1]
49 |
50 | 51 |

Manipulate Arrays With push


52 |

An easy way to append data to the end of an array is via the .push() function.

53 |

.push() takes one or more parameters and pushes them onto the end of the array. For example:

54 |
var arr = [1,2,3];
 55 | arr.push(4);
 56 | // arr is now [1,2,3,4]
57 |
58 | 59 |

Manipulate Arrays With pop


60 |

Another way to change the data in an array is with the .pop() function.

61 |

.pop() is used to pop a value off of the end of an array. We can store this popped off value by assigning it to a variable. Any type of entry can be popped off of an array - numbers, strings, even nested arrays. For example:

62 |
var oneDown = [1, 4, 6].pop();
63 |

In the above code, the variable oneDown now holds the value 6 and the array becomes [1, 4].

64 |
65 |
66 |

Manipulate Arrays With shift


67 |

pop() always removes the last element of an array. What if you want to remove the first? That's where .shift() comes in. It works just like .pop()), except it removes the first element instead of the last. For example:

68 |
var oneDown = [1, 4, 6].shift();
69 |

In the above code, the variable oneDown now holds the value 1 and the array becomes [4, 6].

70 |
71 |

Manipulate Arrays With pop


72 |

Not only can you shift elements off of the beginning of an array, you can also unshift elements to the beginning of an array i.e. add elements in front of the array. .unshift() works exactly like .push(), but instead of adding the element at the end of the array, unshift() adds the element at the beginning of the array. For example:

73 |
var arr = [1,2,3];
 74 | arr.push(0);
 75 | // arr is now [0,1,2,3]
76 |
77 |
78 | 87 |
88 |

  Videos

89 | 90 |

91 |
92 |
93 |
94 |

Questions

95 |
96 |

Please, take the time to read the lesson, before you answer these questions.

97 |
98 |
99 | 100 |
101 | 102 | 103 |
104 |
105 | 106 | 107 |
108 |
109 | 110 | 111 |
112 | 113 |

114 |
115 |
116 |
117 |
118 | 119 |
120 | 121 | 122 |
123 |
124 | 125 | 126 |
127 |
128 | 129 | 130 |
131 | 132 |

133 |
134 |
135 |
136 |
137 | 139 |
140 | 141 | 142 |
143 |
144 | 145 | 146 |
147 | 148 |

149 |
150 |
151 |
152 |
153 | 155 |
156 | 157 | 158 |
159 |
160 | 161 | 162 |
163 | 164 |

165 |
166 |
167 |
168 |
169 | 171 |
172 | 173 | 174 |
175 |
176 | 177 | 178 |
179 |
180 | 181 | 182 |
183 | 184 |

185 |
186 |
187 |
188 |
189 | 190 | 191 | 192 | 193 | -------------------------------------------------------------------------------- /6_loops.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Loops 6 | 7 | 8 | 9 | 29 | 30 | 31 |
 BackNext 

LearnJS

32 |
33 |

Iterate with JavaScript For Loops


34 |

You can run the same code multiple times by using a loop. The most common type of JavaScript loop is called a for loop because it runs for a specific number of times. For loops are declared with three optional expressions separated by semicolons:

35 |
for ([initialization]; [condition]; [final-expression])
36 |

The initialization statement is executed one time only before the loop starts. It is typically used to define and setup your loop variable.

37 |

The condition statement is evaluated at the beginning of every loop iteration and will continue as long as it evaluates to true. When condition is false at the start of the iteration, the loop will stop executing. This means if condition starts as false, your loop will never execute.

38 |

The final-expression is executed at the end of each loop iteration, prior to the next condition check and is usually used to increment or decrement your loop counter.

39 |

In the following example we initialize with i = 0 and iterate while our condition i < 5 is true. We'll increment i by 1 in each loop iteration with i++ as our final-expression.

40 |
var ourArray = [];
 41 | for (var i = 0; i < 5; i++) {
 42 |   ourArray.push(i);
 43 | }
 44 | // The array now contains [0,1,2,3,4]
45 |
46 | 47 |

Iterate Through an Array with a For Loop


48 |

A common task in JavaScript is to iterate through the contents of an array. One way to do that is with a for loop. This code will output each element of the array arr to the console:

49 |
var arr = [10,9,8,7,6];
 50 | for (var i=0; i < arr.length; i++) {
 51 |   console.log(arr[i]);
 52 | }
53 |

Remember that arrays have zero-based numbering, which means the last index of the array is length - 1. Our condition for this loop is i < arr.length, which stops when i is at length - 1.

54 |
55 | 56 |

Iterate with JavaScript While Loops


57 |

Another type of JavaScript loop is called a while loop, because it runs while a specified condition is true and stops once that condition is no longer true.

58 |
var ourArray = [];
 59 | var i = 0;
 60 | while(i < 5) {
 61 |   ourArray.push(i);
 62 |   i++;
 63 | }
 64 | // The array now contains [0,1,2,3,4]

65 |
66 |

Iterating with do...while loops


67 |

Apart from the while loop, you can also use the do...while loop. The only difference between the two is that in the second one the condition is evaluated after the loop's body is executed for the first time, instead of before.

68 |
var ourArray = [];
 69 | var i = 0;
 70 | do {
 71 |   ourArray.push(i);
 72 |   i++;
 73 | } while(i < 5);
 74 | // The array now contains [0,1,2,3,4]

75 |
76 | 86 |
87 |

  Videos

88 | 89 |
90 | 91 |

92 |
93 |
94 |
95 |

Questions

96 |
97 |

Please, take the time to read the lesson, before you answer these questions.

98 |
99 |
100 | 101 |
102 | 103 | 104 |
105 |
106 | 107 | 108 |
109 |
110 | 111 | 112 |
113 |
114 | 115 | 116 |
117 | 118 |

119 |
120 |
121 |
122 |
123 | 124 |
125 | 126 | 127 |
128 |
129 | 130 | 131 |
132 |
133 | 134 | 135 |
136 | 137 |

138 |
139 |
140 |
141 |
142 | 144 | 145 | 146 |

147 |
148 |
149 |
150 |
151 | 152 |
153 | 154 | 155 |
156 |
157 | 158 | 159 |
160 |
161 | 162 | 163 |
164 | 165 |

166 |
167 |
168 |
169 |
170 | 172 |
173 | 174 | 175 |
176 |
177 | 178 | 179 |
180 |
181 | 182 | 183 |
184 | 185 |

186 |
187 |
188 |
189 |
190 | 191 | 192 | 193 | 194 | -------------------------------------------------------------------------------- /5_conditionals.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Conditionals 6 | 7 | 8 | 9 | 28 | 29 | 30 |
 BackNext 

LearnJS

31 |
32 |

Use Conditional Logic with If Statements


33 |

if statements are used to make decisions in code. The keyword if tells JavaScript to execute the code in the curly braces under certain conditions, defined in the parentheses. These conditions are known as Boolean conditions because they may only be true or false. For example:

34 |
if(num > 0) {
 35 |   console.log("Greater than zero.");
 36 | }
37 |

When the condition evaluates to true, the program executes the statement inside the curly braces. When the Boolean condition evaluates to false, the statement inside the curly braces will not execute. For example:

38 |
39 | 40 |

Introducing Else Statements


41 |

When a condition for an if statement is true, the block of code following it is executed. What about when that condition is false? Normally nothing would happen. With an else statement, an alternate block of code can be executed.

42 |
if (num > 10) {
 43 |   console.log("Bigger than 10");
 44 | } else {
 45 |   console.log("10 or Less");
 46 | }
47 |
48 | 49 |

Selecting from many options with Switch Statements


50 |

If you have many options to choose from, use a switch statement. A switch statement tests a value and can have many case statements which defines various possible values. Statements are executed from the first matched case value until a break is encountered. For example:

51 |
switch(num) {
 52 |   case 1:
 53 |     console.log("1");
 54 |     break;
 55 |   case 2:
 56 |     console.log("2");
 57 |     break;
 58 | }
59 |

case values are tested with strict equality (===). The break tells JavaScript to stop executing statements. If the break is omitted, the next statement will be executed.

60 |
61 | 62 |

Adding a default option in Switch statements


63 |

In a switch statement you may not be able to specify all possible values as case statements. Instead, you can add the default statement which will be executed if no matching case statements are found. Think of it like the final else statement in an if/else chain. A default statement should be the last case. For example:

64 |
switch(num) {
 65 |   case 1:
 66 |     console.log("One");
 67 |     break;
 68 |   case 2:
 69 |     console.log("Two");
 70 |     break;
 71 |   default:
 72 |     console.log("Not one or two");
 73 | }
74 |
75 | 76 |

Comparisons with Logical Operators


77 |

Sometimes you will need to test more than one thing at a time. The logical AND operator (&&) returns true if and only if the operands to the left and right of it are true. The same effect could be achieved by nesting an if statement inside another if:

78 |
if (num > 5) {
 79 |   if (num < 10) {
 80 |     console.log("Yes");
 81 | }
82 |

The above code will only print Yes if num is between 6 and 9 (6 and 9 included). The same logic can be written as:

83 |
if (num > 5 && num < 10) {
 84 |   console.log("Yes");
 85 | }
86 |

The logical OR operator (||) returns true if either of the operands is true. Otherwise, it returns false. The same effect could be achieved using multiple if statements:

87 |
if (num > 10) {
 88 |   console.log("No");
 89 | }
 90 | if (num < 5) {
 91 |   console.log("No");
 92 | }
93 |

The above code will print No only if num is below 5 or above 10. The same logic can be written as:

94 |
if (num > 10 || num < 5) {
 95 |   console.log("No");
 96 | }

97 |
98 |

Using the ternary operator


99 |

It is not uncommon to set a variable's value based on a condition. Instead of using an if...else statement, you can use the ternary ? operator. For example, you can transform the following code:

100 |
if (num < 10) {
101 |   var x = "Under 10";
102 | }
103 | else {
104 |   var x = "Over 10";
105 | }
106 |

To this, more compact form using the ? operator:

107 |
var x = (num < 10) ? "Under 10" : "Over 10";
108 |
109 |
110 | 120 |
121 |

  Videos

122 | 123 |
124 | 125 |

126 |
127 |
128 |
129 |

Questions

130 |
131 |

Please, take the time to read the lesson, before you answer these questions.

132 |
133 |
134 | 136 |
137 | 138 | 139 |
140 |
141 | 142 | 143 |
144 | 145 |

146 |
147 |
148 |
149 |
150 | 151 |
152 | 153 | 154 |
155 |
156 | 157 | 158 |
159 |
160 | 161 | 162 |
163 | 164 |

165 |
166 |
167 |
168 |
169 | 170 |
171 | 172 | 173 |
174 |
175 | 176 | 177 |
178 |
179 | 180 | 181 |
182 | 183 |

184 |
185 |
186 |
187 |
188 | 189 |
190 | 191 | 192 |
193 |
194 | 195 | 196 |
197 | 198 |

199 |
200 |
201 |
202 |
203 | 204 |
205 | 206 | 207 |
208 |
209 | 210 | 211 |
212 | 213 |

214 |
215 |
216 |
217 |
218 | 219 |
220 | 221 | 222 |
223 |
224 | 225 | 226 |
227 | 228 |

229 |
230 |
231 |
232 |
233 | 234 | 235 | 236 | 237 | --------------------------------------------------------------------------------