diff --git a/Week1/practice-exercises/1-hello-world.js b/Week1/practice-exercises/1-hello-world.js index e345354..4a211e8 100644 --- a/Week1/practice-exercises/1-hello-world.js +++ b/Week1/practice-exercises/1-hello-world.js @@ -10,3 +10,4 @@ * Hola, mundo! // Spanish */ +console.log('Привет МИР!') //Russian diff --git a/Week1/practice-exercises/2-syntax-error.js b/Week1/practice-exercises/2-syntax-error.js index a0005dd..00d67ba 100644 --- a/Week1/practice-exercises/2-syntax-error.js +++ b/Week1/practice-exercises/2-syntax-error.js @@ -3,4 +3,4 @@ * Fix it so that when running this file it shows the message 'I'm awesome!' */ -console.log('I'm awesome'!; \ No newline at end of file +console.log("I'm awesome!"); diff --git a/Week1/practice-exercises/3-log-number.js b/Week1/practice-exercises/3-log-number.js index c29038c..dfd8506 100644 --- a/Week1/practice-exercises/3-log-number.js +++ b/Week1/practice-exercises/3-log-number.js @@ -4,20 +4,22 @@ */ // 1. Declare your variable numberX. Do not initialize it (which means, don't give it a starting value) yet - +let numberX; // 2. Add a console.log statement that explains in words what you think the value of x is - +console.log('X number is not defined'); // 3. Add a console.log statement that logs the value of numberX. - +console.log(numberX); // 4. Now initialize your variable numberX with a number (also called an integer in computer science terms) - +numberX = 5; // 5. Next, add a console.log statement that explains what you think the value of numberX is - +console.log('X number is now 5'); // 6. Add a console.log statement that logs the value of numberX +console.log(numberX); + diff --git a/Week1/practice-exercises/4-log-string.js b/Week1/practice-exercises/4-log-string.js index 8e38cb3..196bab0 100644 --- a/Week1/practice-exercises/4-log-string.js +++ b/Week1/practice-exercises/4-log-string.js @@ -4,20 +4,20 @@ */ // 1. Declare a variable myString and assign a string to it. Use your full name, including spaces, as the content for the string. - +let myString = 'Andrei Popov'; // 2. Write a console.log statement in which you explain in words what you think the value of the string is. - +console.log('String value is my full name'); // 3. Now console.log the variable myString. - +console.log(myString); // 4. Now reassign to the variable myString a new string. - +myString = 'Andrei'; // 5. Just like what you did before write a console.log statement that explains in words what you think will be logged to the console. - +console.log('String value is my name'); // 6. Now console.log myString again. - +console.log(myString); diff --git a/Week1/practice-exercises/5-round-number.js b/Week1/practice-exercises/5-round-number.js index 602c5f1..d0672f6 100644 --- a/Week1/practice-exercises/5-round-number.js +++ b/Week1/practice-exercises/5-round-number.js @@ -3,15 +3,20 @@ */ // 1. Declare a variable z and assign the number 7.25 to it. - +let z = 7.25; // 2. Write a console.log statement in which you log the value of z. - +console.log('z value is ' + z); // 3. Declare another variable a that has the value of z but rounded to the nearest integer. - +let a = Math.round(z); // 4. Write a console.log statement in which you log the value of a. - +console.log('a value is ' + a); // 5. So now we have z and a find a way to compare the two values and log true if a is greater than z or false if a is smaller than z. +if (a > z) { + console.log(true); +} else { + console.log(false); +} diff --git a/Week1/practice-exercises/6-log-animals.js b/Week1/practice-exercises/6-log-animals.js index 65c2e41..3f6bcba 100644 --- a/Week1/practice-exercises/6-log-animals.js +++ b/Week1/practice-exercises/6-log-animals.js @@ -3,23 +3,23 @@ */ // 1. Declare variable and assign to it an empty array. Make sure that the name of the variable indicates it contains more than 1 item. For example items instead of item. - +const items = []; // 2. Write a console.log statement that explains in words what you think the value of the array is. - +console.log('array is empty as it declared'); // 3. Write a console.log statement that logs the array. - +console.log(items); // 4. Create a new variable with an array that has 3 of your favorite animals, each in a different string. Make sure the name of the variables says something about what the variable contains. - +const myFavoriteAnimals = ['cat', 'horse', 'monkey'] // 5. Write a console.log statement that logs the second array. - +console.log(myFavoriteAnimals); // 6. Add a statement that adds another string ("Piglet)" to the array of animals. - +myFavoriteAnimals.push('piglet'); // 7. Write a console.log statement that logs the second array! - +console.log(myFavoriteAnimals); diff --git a/Week1/practice-exercises/7-log-string-length.js b/Week1/practice-exercises/7-log-string-length.js index b1ee540..cbc2698 100644 --- a/Week1/practice-exercises/7-log-string-length.js +++ b/Week1/practice-exercises/7-log-string-length.js @@ -3,7 +3,7 @@ */ // 1. Declare a variable called mySentence and initialize it with the following string: "Programming is so interesting!". - +const mySentence = "Programming is so interesting!" // 2. Figure out (using Google) how to get the length of mySentence. Then write a console.log statement to log the length of mySentence. - +console.log(`${mySentence} Length of this sentence is ${mySentence.length}.`); diff --git a/Week2/practice-exercises/1-remove-the-comma.js b/Week2/practice-exercises/1-remove-the-comma.js index b71cffd..79d7d31 100644 --- a/Week2/practice-exercises/1-remove-the-comma.js +++ b/Week2/practice-exercises/1-remove-the-comma.js @@ -7,8 +7,9 @@ let myString = 'hello,this,is,a,difficult,to,read,sentence'; - +myString = myString.replace(/,/g, ' '); +//"g" means to all symbols in the string not only the first one /* --- Code that will test your solution, do NOT change. Write above this line --- */ -console.assert(myString === 'hello this is a difficult to read sentence', 'There is something wrong with your solution'); \ No newline at end of file +console.assert(myString === 'hello this is a difficult to read sentence', 'There is something wrong with your solution'); diff --git a/Week2/practice-exercises/2-even-odd-reporter.js b/Week2/practice-exercises/2-even-odd-reporter.js index 6edf23e..ae4d8d8 100644 --- a/Week2/practice-exercises/2-even-odd-reporter.js +++ b/Week2/practice-exercises/2-even-odd-reporter.js @@ -7,3 +7,12 @@ * If it's even, log to the console The number [PUT_NUMBER_HERE] is even!. */ +let num = 0; +do { + if (num % 2 == 0) { + console.log(num + " is even"); + } else { + console.log(num + " is odd"); + } + num++ + } while (num <= 20); diff --git a/Week2/practice-exercises/3-recipe-card.js b/Week2/practice-exercises/3-recipe-card.js index 24bcb54..2f8c2fb 100644 --- a/Week2/practice-exercises/3-recipe-card.js +++ b/Week2/practice-exercises/3-recipe-card.js @@ -12,3 +12,12 @@ * Ingredients: 4 eggs, 2 strips of bacon, 1 tsp salt/pepper */ +const mealRecipe = { + mealName: 'Omelette', + serves: 2, + ingredients: ['eggs', 'bacon', 'salt', 'pepper', 'milk'], +}; + +for (const property in mealRecipe) { + console.log(`${property}: ${mealRecipe[property]}`.replace(/,/g, ', ')); //changing ',' to ', + space' for beauty in array elements +} diff --git a/Week2/practice-exercises/4-reading-list.js b/Week2/practice-exercises/4-reading-list.js index f535657..c9a395d 100644 --- a/Week2/practice-exercises/4-reading-list.js +++ b/Week2/practice-exercises/4-reading-list.js @@ -9,3 +9,27 @@ * If you haven't read it log a string like You still need to read "The Lord of the Rings" */ +const myBooks = [ + { + title: "Harry Potter and the Philosopher's Stone", + author: 'J. K. Rowling', + alreadyRead: true, + }, + { + title: 'Harry Potter and the Goblet of Fire', + author: 'J. K. Rowling', + alreadyRead: true, + }, + { + title: 'Harry Potter and the half-blood', + author: 'J. K. Rowling', + alreadyRead: false, + }, +]; +for (const book of myBooks) { + if (book.alreadyRead == true) { + console.log(`You already read "${book.title}" by ${book.author}`); + } else { + console.log(`You still need to read "${book.title}" by ${book.author}`); + } +} diff --git a/Week2/practice-exercises/5-who-wants-a-drink.js b/Week2/practice-exercises/5-who-wants-a-drink.js index f37f02b..c112bf2 100644 --- a/Week2/practice-exercises/5-who-wants-a-drink.js +++ b/Week2/practice-exercises/5-who-wants-a-drink.js @@ -8,4 +8,25 @@ */ // There are 3 different types of drinks: -const drinkTypes = ['cola', 'lemonade', 'water']; \ No newline at end of file + +const drinkTypes = ['cola', 'lemonade', 'water']; +const drinkTray = []; +let n = 0; +for (let i = 0; i < 5; i++) { + if (n > 2) { + n = 0; + } + drinkTray.push(drinkTypes[n]); + n++; +} +console.log(`Hey guys, I brought a ${drinkTray.join(', ')}!`); + +// Another version with rendom function + +const drinkTypes = ['cola', 'lemonade', 'water']; +const drinkTray = []; +for (let i = 0; i <5; i++) { + let n = Math.floor(Math.random() * 3); + drinkTray.push(drinkTypes[n]); +}; +console.log(`Hey guys, I brought a ${drinkTray.join(', ')}!`); diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light-1.js b/Week2/prep-exercises/1-traffic-light/traffic-light-1.js index f1d9169..c9f3126 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light-1.js @@ -1,31 +1,38 @@ -"use strict"; +'use strict'; /** * The `state` property says what the traffic light's state (i.e. colour) is at * that moment. */ const trafficLight = { - state: "green", + state: 'green', }; let rotations = 0; while (rotations < 2) { const currentState = trafficLight.state; - console.log("The traffic light is on", currentState); + console.log('The traffic light is on', currentState); // TODO // if the color is green, turn it orange // if the color is orange, turn it red // if the color is red, add 1 to rotations and turn it green + + if (currentState == 'green') { + trafficLight.state = 'orange'; + } else if (currentState == 'orange') { + trafficLight.state = 'red'; + } else { + rotations++; + trafficLight.state = 'green'; + } } /** * The output should be: - The traffic light is on green The traffic light is on orange The traffic light is on red The traffic light is on green The traffic light is on orange The traffic light is on red - */ diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light-2.js b/Week2/prep-exercises/1-traffic-light/traffic-light-2.js index 8c6ba95..90b7afb 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light-2.js @@ -18,16 +18,40 @@ while (cycle < 2) { // if the color is green, turn it orange // if the color is orange, turn it red // if the color is red, add 1 to cycles and turn it green + + if (currentState == 'green') { + trafficLight.stateIndex = 1; + } else if (currentState == 'orange') { + trafficLight.stateIndex = 2; + } else { + cycle++; + trafficLight.stateIndex = 0; + } } - /** * The output should be: - The traffic light is on green The traffic light is on orange The traffic light is on red The traffic light is on green The traffic light is on orange The traffic light is on red - */ +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +//Shortest solution for traffic light exercise +'use strict'; +const trafficLight = { + possibleStates: ['green', 'orange', 'red'], +}; + +for (let cycle = 0; cycle < 2; cycle++) { + for ( + trafficLight.stateIndex = 0; + trafficLight.stateIndex <= 2; + trafficLight.stateIndex++ + ) { + const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; + console.log('The traffic light is on', currentState); + } +} diff --git a/Week3/prep-exercises/1-traffic-light/traffic-light.js b/Week3/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..defe3a8 100644 --- a/Week3/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week3/prep-exercises/1-traffic-light/traffic-light.js @@ -1,52 +1,49 @@ -"use strict"; -/** - * The `trafficLight` object is now no longer a global variable. Instead, - * it is defined in function `main()` and passed as a parameter to other - * functions, as and when needed. - */ +'use strict'; function getCurrentState(trafficLight) { - // TODO - // Should return the current state (i.e. colour) of the `trafficLight` - // object passed as a parameter. + return trafficLight.possibleStates[trafficLight.stateIndex]; } function getNextStateIndex(trafficLight) { - // TODO - // Return the index of the next state of the `trafficLight` such that: - // - if the color is green, it will turn to orange - // - if the color is orange, it will turn to red - // - if the color is red, it will turn to green + +// if (trafficLight.stateIndex === 0) { +// return (trafficLight.stateIndex = 1); +// } else if (trafficLight.stateIndex === 1) { +// return (trafficLight.stateIndex = 2); +// } else { +// return (trafficLight.stateIndex = 0); +// } + +// The same but shorter with ternary operators + return trafficLight.stateIndex === 0 + ? (trafficLight.stateIndex = 1) + : trafficLight.stateIndex === 1 + ? (trafficLight.stateIndex = 2) + : (trafficLight.stateIndex = 0); } -// This function loops for the number of seconds specified by the `secs` -// parameter and then returns. -// IMPORTANT: This is not the recommended way to implement 'waiting' in -// JavaScript. You will learn better ways of doing this when you learn about -// asynchronous code. function waitSync(secs) { const start = Date.now(); - while (Date.now() - start < secs * 1000) { - // nothing do to here - } + while (Date.now() - start < secs * 1000) {} } function main() { const trafficLight = { - possibleStates: ["green", "orange", "red"], + possibleStates: ['green', 'orange', 'red'], stateIndex: 0, }; for (let cycle = 0; cycle < 6; cycle++) { const currentState = getCurrentState(trafficLight); - console.log(cycle, "The traffic light is now", currentState); - - waitSync(1); // Wait a second before going to the next state + console.log(cycle, 'The traffic light is now', currentState); + waitSync(1); trafficLight.stateIndex = getNextStateIndex(trafficLight); + console.log(getNextStateIndex(trafficLight)); } } main(); + /** * The output should be: diff --git a/Week4/prep-exercises/1-hyf-program/1-find-mentors.js b/Week4/prep-exercises/1-hyf-program/1-find-mentors.js index a096de0..d474bba 100644 --- a/Week4/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week4/prep-exercises/1-hyf-program/1-find-mentors.js @@ -1,4 +1,4 @@ -import { modules, students, mentors, classes } from "./hyf"; +import { modules, students, mentors, classes } from './hyf.js'; /** * Tjebbe would like help to get a list of possible mentors for a module. @@ -8,10 +8,14 @@ import { modules, students, mentors, classes } from "./hyf"; * ['John', 'Mary'] */ const possibleMentorsForModule = (moduleName) => { - // TODO complete this function + const possibleMentors = mentors.filter((mentor) => + mentor.canTeach.includes(moduleName), + ); + const possibleMentorsNames = []; + possibleMentors.forEach((mentor) => possibleMentorsNames.push(mentor.name)); + return possibleMentorsNames; }; -// You can uncomment out this line to try your function -// console.log(possibleMentorsForModule('using-apis')); +console.log(possibleMentorsForModule('using-apis')); /** * Tjebbe wants to make it even easier for himself. @@ -19,8 +23,13 @@ const possibleMentorsForModule = (moduleName) => { * * It should return a single name. */ + const findMentorForModule = (moduleName) => { - // TODO complete this function + const possibleMentorsNames = possibleMentorsForModule(moduleName); + const randomName = + possibleMentorsNames[ + Math.floor(Math.random() * possibleMentorsNames.length) + ]; + return randomName; }; -// You can uncomment out this line to try your function -// console.log(findMentorForModule('javascript')); +console.log(findMentorForModule('javascript')); diff --git a/Week4/prep-exercises/1-hyf-program/2-class-list.js b/Week4/prep-exercises/1-hyf-program/2-class-list.js index 995f9f1..a33b715 100644 --- a/Week4/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week4/prep-exercises/1-hyf-program/2-class-list.js @@ -1,4 +1,4 @@ -import { modules, students, mentors, classes } from "./hyf"; +import { modules, students, mentors, classes } from './hyf.js'; /** * We would like to have a list of everyone that is currently participating in a class. @@ -12,10 +12,25 @@ import { modules, students, mentors, classes } from "./hyf"; * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ const getPeopleOfClass = (className) => { - // TODO complete this function + const peopleOfClass = []; + const studentsOfClass = students.filter( + (student) => student.class == className, + ); + studentsOfClass.forEach((student) => + peopleOfClass.push({ name: student.name, role: 'student' }), + ); + const moduleName = classes.find( + (classN) => classN.name == className, + ).currentModule; + const mentorsOfClass = mentors.filter( + (mentor) => mentor.nowTeaching == moduleName, + ); + mentorsOfClass.forEach((mentor) => + peopleOfClass.push({ name: mentor.name, role: 'mentor' }), + ); + return peopleOfClass; }; -// You can uncomment out this line to try your function -// console.log(getPeopleOfClass('class34')); +console.log(getPeopleOfClass('class34')); /** * We would like to have a complete overview of the current active classes. @@ -29,8 +44,23 @@ const getPeopleOfClass = (className) => { * class35: [{ name: 'Jane', role: 'student' }, { name: 'Steve', role: 'mentor' }] * } */ + const getActiveClasses = () => { - // TODO complete this function + const getActiveClasses = () => { + const getActiveClassesTemp = {}; + const getActive = classes.filter((classN) => classN.active == true); + getActive.forEach( + (classN) => + (getActiveClassesTemp[classN.name] = getPeopleOfClass(classN.name)), + ); + return getActiveClassesTemp; + }; + // Another method + // const activeClasses = {}; + // for (const item of classes) { + // if (item.active) activeClasses[item.name] = getPeopleOfClass(item.name); + // } + return getActiveClasses; }; -// You can uncomment out this line to try your function -// console.log(getActiveClasses()); + +console.log(getActiveClasses()); diff --git a/Week4/prep-exercises/1-hyf-program/index.html b/Week4/prep-exercises/1-hyf-program/index.html new file mode 100644 index 0000000..2dc3c6d --- /dev/null +++ b/Week4/prep-exercises/1-hyf-program/index.html @@ -0,0 +1,15 @@ + + + +
+ + + +