diff --git a/1-exercises/A-array-find/README.md b/1-exercises/A-array-find/README.md deleted file mode 100644 index 01405174..00000000 --- a/1-exercises/A-array-find/README.md +++ /dev/null @@ -1,27 +0,0 @@ -Imagine you have an array of names: - -```js -let names = ["Daniel", "James", "Irina", "Mozafar", "Ashleigh"]; -``` - -How would you find the first name that's longer than 6 characters? - -You can write a predicate function that checks if a string is longer than 6 characters: - -```js -function isLongName(name) { - return name.length > 6; -} -``` - -To find the first item that satisfies the predicate you would have to go through each array item, and pass it into `isLongName`. Once it returns true, we can stop going through the array and grab the item that passed the predicate's test. Sounds complicated! Thankfully there is an array method that does just this! - -## `.find()` - -_Searches through the array and returns the value of the first item that satisfies a predicate function._ - -```js -let longName = names.find(isLongName); - -console.log(longName); // logs Mozafar -``` diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js deleted file mode 100644 index 35902fed..00000000 --- a/1-exercises/A-array-find/exercise.js +++ /dev/null @@ -1,25 +0,0 @@ -/* - You are given an array of names. - Using .find(), we'd like to find the first name which starts with A and is longer than 7 letters. -*/ - -// write your code here - -let names = [ - "Rakesh", - "Antonio", - "Alexandra", - "Andronicus", - "Annam", - "Mikey", - "Anastasia", - "Karim", - "Ahmed", -]; - -let longNameThatStartsWithA = findLongNameThatStartsWithA(names); - -console.log(longNameThatStartsWithA); - -/* EXPECTED OUTPUT */ -// "Alexandra" diff --git a/1-exercises/B-array-some/README.md b/1-exercises/B-array-some/README.md deleted file mode 100644 index 114016b5..00000000 --- a/1-exercises/B-array-some/README.md +++ /dev/null @@ -1,27 +0,0 @@ -Imagine you have an array of numbers: - -```js -let numbers = [1, 3, -1, 5, 9]; -``` - -You know that the array is supposed to contain positive numbers, but you want to check if it also contains any negative numbers. - -We can write a function that checks this: - -```js -function isNegative(number) { - return number < 0; -} -``` - -To check your array of numbers, you'd have to run this function against every number in the array. Thankfully there is an array method that does just this! - -## `.some()` - -_Searches through an array and returns true if at least one array item satisifies the predicate function you provided._ - -```js -let containsNegative = ages.some(isNegative); - -console.log(containsNegative); // logs true -``` diff --git a/1-exercises/B-array-some/exercise.js b/1-exercises/B-array-some/exercise.js deleted file mode 100644 index fddc69ee..00000000 --- a/1-exercises/B-array-some/exercise.js +++ /dev/null @@ -1,24 +0,0 @@ -/* - You are given a program that logs pairings between mentors and students - It fails because the array `pairsById` can contain null values - It is decided that if there is a null value the program should exit - - Add a check for null values, and if one exists, exit the program - - Do not edit any of the existing code -*/ - -let pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]]; - -// If there is a null value in the array exit the program with the error code -// https://nodejs.org/api/process.html#process_process_exit_code -// process.exit(1); - -let students = ["Islam", "Lesley", "Harun", "Rukmini"]; -let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; - -let pairs = pairsByIndex.map(function (indexes) { - let student = students[indexes[0]]; - let mentor = mentors[indexes[1]]; - return [student, mentor]; -}); - -console.log(pairs); diff --git a/1-exercises/C-array-every/README.md b/1-exercises/C-array-every/README.md deleted file mode 100644 index 74628ab6..00000000 --- a/1-exercises/C-array-every/README.md +++ /dev/null @@ -1,27 +0,0 @@ -Imagine you have an array of people's names: - -```js -let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley"]; -``` - -You want to check that every student in the array has a name longer than 3 characters. How do you do that for every value in the array? - -We can write a function that returns true or false: - -```js -function isAboveThreshold(name) { - return name.length > 3; -} -``` - -To check that each name is longer than 3 characters, you'd have to run this function against every name in the array and return false if someone's name is 3 or fewer characters. Thankfully there is an array method that does just this! - -## `.every()` - -_Searches through an array and returns true if every item satisifies the predicate function you provided. Otherwise, it returns false_. - -```js -let studentNameLength = students.every(isAboveThreshold); - -console.log(studentNameLength); // logs true -``` diff --git a/1-exercises/C-array-every/exercise.js b/1-exercises/C-array-every/exercise.js deleted file mode 100644 index 347b9632..00000000 --- a/1-exercises/C-array-every/exercise.js +++ /dev/null @@ -1,18 +0,0 @@ -/* - This program should check if the array `group` contains only students -*/ - -let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; -let group = ["Austine", "Dany", "Swathi", "Daniel"]; - -let groupIsOnlyStudents; // complete this statement - -if (groupIsOnlyStudents) { - console.log("The group contains only students"); -} else { - console.log("The group does not contain only students"); -} - -/* EXPECTED RESULT */ - -// The group does not contain only students diff --git a/1-exercises/D-array-filter/README.md b/1-exercises/D-array-filter/README.md deleted file mode 100644 index aaf01919..00000000 --- a/1-exercises/D-array-filter/README.md +++ /dev/null @@ -1,27 +0,0 @@ -Imagine you have an array of students' test scores: - -```js -let testScores = [90, 50, 100, 66, 25, 80, 81]; -``` - -You want to show only the test scores that are higher than 80. How do you do that for every value in the array? - -We can write a function that checks if one score is greater than 80: - -```js -function isHighScore(score) { - return score > 80; -} -``` - -To find out which scores were greater than 80, you'd have to run this function against every score in the array, and push the 80+ scores into a new array. Thankfully there is an array method that does just this! - -## `.filter()` - -_Runs every item in the array through a condition that we set, and returns a new array with the values that match the condition_. - -```js -let highTestScores = testScores.filter(isHighScore); - -console.log(highTestScores); // logs [90, 100, 81] -``` diff --git a/1-exercises/D-array-filter/exercise.js b/1-exercises/D-array-filter/exercise.js deleted file mode 100644 index 51837028..00000000 --- a/1-exercises/D-array-filter/exercise.js +++ /dev/null @@ -1,27 +0,0 @@ -/* - You are given a program that logs pairings between mentors and students - It fails because the array `pairsById` can contain different values that break the program - It is decided that array items which are not pairs should be filtered out - - Finish the statement on line 11 to produce an array with valid content - - Do not edit any of the existing code -*/ - -let pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; - -let pairsByIndex; // Complete this statement - -let students = ["Islam", "Lesley", "Harun", "Rukmini"]; -let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; - -let pairs = pairsByIndex.map(function (indexes) { - let student = students[indexes[0]]; - let mentor = mentors[indexes[1]]; - return [student, mentor]; -}); - -console.log(pairs); - -/* EXPECTED RESULT - - [ [ 'Islam', 'Luke' ], [ 'Lesley', 'Mozafar' ], [ 'Harun', 'Irina' ] ] -*/ \ No newline at end of file diff --git a/1-exercises/E-array-map/README.md b/1-exercises/E-array-map/README.md deleted file mode 100644 index c243ece8..00000000 --- a/1-exercises/E-array-map/README.md +++ /dev/null @@ -1,58 +0,0 @@ -We learnt about the `.map()` method in the previous week. This week we'll study how it works in more depth. - -You might remember this example: - -```js -function double(number) { - return number * 2; -} - -let numbers = [1, 2, 3]; -let numbersDoubled = numbers.map(double); -``` - -The `map()` method runs the function we provided (`double`) on each item in the array and uses the return values to create a new array. In the example `numbersDoubled` is a new array containing `[2, 4, 6]`. - -### Callback functions - -A function that we provide to a method is commonly called a _callback_ function. The term highlights that although we _provide_ the `double` function, the `.map()` method _calls_ it. (Notice how we never write `double()` to call the function). - -We'll see callback functions used a lot more in the coming weeks. - -Often, when a function is only needed for a map operation, developers will declare the callback function inside of the method call. Let's try copying and pasting the function declaration inside of the `.map()` method call. - -```js -let numbers = [1, 2, 3]; -let numbersDoubled = numbers.map(function double(number) { - return number * 2; -}); -``` - -We can make this shorter by removing the function name. We can do this because we are not using the function anywhere else in the code, so we do not need the function name to reference it. - -```js -let numbers = [1, 2, 3]; -let numbersDoubled = numbers.map(function (number) { - return number * 2; -}); -``` - -We can make this code even shorter still. In the latest versions of JavaScript a way of declaring functions was introduced called _arrow functions_. - -```js -let numbers = [1, 2, 3]; -let numbersDoubled = numbers.map(number => { - return number * 2 -}); -``` - -The arrow function syntax lets you declare a function without the `function` keyword. (There are some other subtle differences between arrow functions and regular functions that you will learn about at a much later stage). - -There is one last thing you can do to make your code shorter. If you remove the braces (`{}`) from an arrow function, the body of the function will be returned without needing to write the `return` keyword. - -```js -let numbers = [1, 2, 3]; -let numbersDoubled = numbers.map(number => number * 2); -``` - -In the example above, the expression `number * 2` is automatically returned because it comes directly after the `=>` arrow (instead of coming after curly braces). This is called an `implicit return`. \ No newline at end of file diff --git a/1-exercises/E-array-map/exercise.js b/1-exercises/E-array-map/exercise.js deleted file mode 100644 index 5a157279..00000000 --- a/1-exercises/E-array-map/exercise.js +++ /dev/null @@ -1,13 +0,0 @@ -// Using the .map() method, create a new array with `numbers` multiplied by 100 -// Write multiple solutions using different syntax (as shown in the README) - -let numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; - -let numbersMultipliedByOneHundred; // complete this statement - -console.log(numbersMultipliedByOneHundred); - -/* EXPECTED RESULT - - [10, 20, 30, 40, 50] -*/ \ No newline at end of file diff --git a/1-exercises/F-array-forEach/README.md b/1-exercises/F-array-forEach/README.md deleted file mode 100644 index dfc43ba7..00000000 --- a/1-exercises/F-array-forEach/README.md +++ /dev/null @@ -1,72 +0,0 @@ -The `.forEach()` method is similar to `.map()` except it does not return a new array. Therefore `.forEach()` is only useful if you want to perform _side effects_. - -### Side effects - -Generally, functions should take an input and return an output (based on that input), and not do anything else. - -When functions meet this criteria they can be called _pure functions_. - -A pure function does not: - -* access any data unless it was passed in as a parameter -* change data declared outside the function -* interacts with anything outside of the function (e.g. logs a message to the console, shows a message on a website, saves data to disk) - -These are all example of _side effects_. Of course, from time to time, we will need to perform side effects, but we should try to avoid side effects inside of functions and only have them when absolutely necessary. - -### Example - -Say we want to log to the console a list of names. - -```js -let names = ["Daniel", "mozafar", "irina"]; -``` - -We can use `.forEach()` to go through the array, item by item, and call a function we provide. - -```js -names.forEach(function(name, index) { - console.log(index + ": " + name); -}); -``` - -This logs each name to the console as hoped, but we notice that the names are not formatted correctly. You might be tempted to format the name inside of the `forEach` function. - -However, it is good practise to write small functions with a single responsibility. So instead, we can write a `formatName` function (which we can re-use in other places) and pass it to `.map()` before calling `.forEach()`. - -```js -function formatName(name) { - return name.split("")[0].toUpperCase() + name.slice(1); -} - -names.map(formatName).forEach(function(name, index) { - console.log(index + ": " + name); -}); -``` - -### Chaining - -Notice how we were able to write one method after another e.g. `names.map(formatName).forEach(log)`? This is called _method chaining_. - -You can call `.forEach()` after `.map()` because `.map()` returns a new array. - -Consider this code: - -```js -let namesFormatted = names.map(format); -namesFormatted.forEach(log); -``` - -It can be written more simply (without assigning the array returned from `.map()` to a variable): - -```js -names.map(format).forEach(log); -``` - -Be careful though! You can not call `.map()` after `.forEach`. - -```js -names.forEach(log).map(formatName); // ERROR -``` - -This code does not work because `forEach()` does not return a new array (it returns `undefined`). The code is therefore attempting to call `.map()` on `undefined`, and `undefined` does not have a `.map()` method. diff --git a/1-exercises/F-array-forEach/exercise.js b/1-exercises/F-array-forEach/exercise.js deleted file mode 100644 index 985068cc..00000000 --- a/1-exercises/F-array-forEach/exercise.js +++ /dev/null @@ -1,30 +0,0 @@ -/* - Using .forEach() print the numbers 1 to 15, with some exceptions: - - For multiples of 3 print “Fizz” instead of the number - - For the multiples of 5 print “Buzz”. - - For numbers which are multiples of both 3 and 5 print “FizzBuzz” - - An array with numbers 1-15 has been provided. -*/ - -let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; - -/* EXPECTED OUTPUT */ - -/* -1 -2 -'Fizz' -4 -'Buzz' -'Fizz' -7 -8 -'Fizz' -'Buzz' -11 -'Fizz' -13 -14 -'FizzBuzz' -*/ diff --git a/1-exercises/G-array-methods/README.md b/1-exercises/G-array-methods/README.md deleted file mode 100644 index 893117ea..00000000 --- a/1-exercises/G-array-methods/README.md +++ /dev/null @@ -1,55 +0,0 @@ -Do you remember how strings have special functions called methods? Don't worry if not! Here's an example to jog your memory: - -```sh -$ node -> let name = "Daniel" -undefined -> name.toLowerCase() -daniel -``` - -Arrays also have several methods that you can use. - -### `.sort()` - -_An array method that sorts the values in an array into ascending alphabetical or numerical order._ - -```js -let unorderedLetters = ["z", "v", "b", "f", "g"]; -let orderedLetters = unorderedLetters.sort(); - -let unorderedNumbers = [8, 5, 1, 4, 2]; -let orderedNumbers = unorderedNumbers.sort(); - -console.log(orderedLetters); // logs [ 'b', 'f', 'g', 'v', 'z' ] -console.log(unorderedLetters); // logs [ 'b', 'f', 'g', 'v', 'z' ] -console.log(orderedNumbers); // logs [ 1, 2, 4, 5, 8 ] -console.log(unorderedNumbers); // logs [ 1, 2, 4, 5, 8 ] -``` - -> When you call this array method it uses the array on the left side of the dot as an input, and it sorts that array also returning it. Note how both ordered and unordered arrays are sorted now! - -### `.concat()` - -_Adds (or concatenates) another value or array to the array._ - -```sh -$ node -> let arr = [1, 2, 3] -undefined -> arr.concat(4) -[1, 2, 3, 4] -> arr -[1, 2, 3] -``` - -Did you notice how calling the concat method did not change `arr`? This is because `concat`, like most array methods, returns a _new_ array, it does not alter the one you called the method on. - -If you wan to use the array returned by calling `.concat()` you should store it in a new variable. - -```js -let arr = [1, 2, 3]; -let newArr = arr.concat(4); - -console.log(newArr); // logs [1, 2, 3, 4] -``` diff --git a/1-exercises/G-array-methods/exercise.js b/1-exercises/G-array-methods/exercise.js deleted file mode 100644 index 4367ef6e..00000000 --- a/1-exercises/G-array-methods/exercise.js +++ /dev/null @@ -1,19 +0,0 @@ -/* - Array methods - sort - -------------------- -*/ - -let numbers = [3, 2, 1]; -let sortedNumbers; // complete this statement - -/* - DO NOT EDIT BELOW THIS LINE - --------------------------- */ - -console.log(sortedNumbers); - -/* - EXPECTED RESULT - --------------- - [1, 2, 3] -*/ diff --git a/1-exercises/G-array-methods/exercise2.js b/1-exercises/G-array-methods/exercise2.js deleted file mode 100644 index 4c68c3a6..00000000 --- a/1-exercises/G-array-methods/exercise2.js +++ /dev/null @@ -1,22 +0,0 @@ -/* - Array methods - concat - ---------------------- - The variable everyone should be an array containing both mentors and students. -*/ - -let mentors = ["Daniel", "Irina", "Rares"]; -let students = ["Rukmini", "Abdul", "Austine", "Swathi"]; - -let everyone; // complete this statement - -/* - DO NOT EDIT BELOW THIS LINE - --------------------------- */ - -console.log(everyone); - -/* - EXPECTED RESULT - --------------- - ["Daniel", "Irina", "Rares", "Rukmini", "Abdul", "Austine", "Swathi"] -*/ diff --git a/1-exercises/H-array-methods-2/README.md b/1-exercises/H-array-methods-2/README.md deleted file mode 100644 index 2c49b98a..00000000 --- a/1-exercises/H-array-methods-2/README.md +++ /dev/null @@ -1,46 +0,0 @@ -Let's explore some more array methods. - -### `.slice()` - -_Returns a slice of the array._ - -You can tell `.slice()` where you want the slice to begin and end by passing it two parameters. - -```sh -$ node -> let arr = [0, 1, 2, 3, 4] -undefined -> arr.slice(0, 2) -[0, 1] -> ["a", "b", "c", "d"].slice(1, 2) -['b'] -``` - -### `.includes()` - -_Returns true if a value is in the array._ - -```js -let mentors = ["Daniel", "Irini", "Ashleigh", "Rob", "Etzali"]; - -function isAMentor(name) { - return mentors.includes(name); -} - -consooe.log("Is Rukmuni a mentor?"); -console.log(isAMentor("Rukmini")); // logs false -``` - -### `.join()` - -_Returns all the array values joined together in a string. By default, this method takes no parameters and then the elements are divided with a comma `,`. If you provide it with a string parameter though, then it becomes the divider of the elements, like the example below:_ - -```sh -$ node -> ["H", "e", "l", "l", "o"].join(); -'H,e,l,l,o' -> ["H", "e", "l", "l", "o"].join("--"); -'H--e--l--l--o' -``` - -There is a string method `.split()`. In an interactive console try using the string `.split()` method and the array `.join()`. How could they work together? diff --git a/1-exercises/H-array-methods-2/exercise.js b/1-exercises/H-array-methods-2/exercise.js deleted file mode 100644 index 59c5daa7..00000000 --- a/1-exercises/H-array-methods-2/exercise.js +++ /dev/null @@ -1,33 +0,0 @@ -/* - Array methods - .slice() - ------------------------ - The variable `firstFive` should contain the first five items of `everyone` - The variable `lastFive` should contain the last five items of `everyone` -*/ - -let everyone = [ - "Daniel", - "Irina", - "Rares", - "Rukmini", - "Abdul", - "Austine", - "Swathi", -]; - -let firstFive; // complete this statement -let lastFive; // complete this statement - -/* - DO NOT EDIT BELOW THIS LINE - --------------------------- */ - -console.log(firstFive); -console.log(lastFive); - -/* - EXPECTED RESULT - --------------- - ["Daniel", "Irina", "Rares", "Rukmini", "Abdul"] - ["Rares", "Rukmini", "Abdul", "Austine", "Swathi"] -*/ diff --git a/1-exercises/H-array-methods-2/exercise2.js b/1-exercises/H-array-methods-2/exercise2.js deleted file mode 100644 index 14bb4318..00000000 --- a/1-exercises/H-array-methods-2/exercise2.js +++ /dev/null @@ -1,25 +0,0 @@ -/* - Array methods - .join() - ------------------------- - Complete the capitalise function - It should return a string with the first letter in uppercase - For example, capitailise("hello") should return "Hello" - Tip: use the string method .split() and the array method .join() -*/ - -function capitalise(str) {} - -/* - DO NOT EDIT BELOW THIS LINE - --------------------------- */ -let name = "daniel"; - -console.log(capitalise(name)); -console.log(capitalise("hello")); - -/* - EXPECTED RESULT - --------------- - Daniel - Hello -*/ diff --git a/1-exercises/H-array-methods-2/exercise3.js b/1-exercises/H-array-methods-2/exercise3.js deleted file mode 100644 index c8e079e4..00000000 --- a/1-exercises/H-array-methods-2/exercise3.js +++ /dev/null @@ -1,26 +0,0 @@ -/* - Array methods - .includes() - --------------------------- - Complete the function below to check if a country is in the UK -*/ - -let ukNations = ["Scotland", "Wales", "England", "Northern Ireland"]; - -function isInUK(country) { - return; // complete this statement -} - -/* - DO NOT EDIT BELOW THIS LINE - --------------------------- */ -console.log(isInUK("France")); -console.log(isInUK("Republic of Ireland")); -console.log(isInUK("England")); - -/* - EXPECTED RESULT - --------------- - false - false - true -*/ diff --git a/1-exercises/I-string-replace/README.md b/1-exercises/I-string-replace/README.md deleted file mode 100644 index 5d7315e3..00000000 --- a/1-exercises/I-string-replace/README.md +++ /dev/null @@ -1,17 +0,0 @@ -# Using Replace In A String - -.replace() allows us to add something where we removed something - -```js -let greeting = "Good Morning"; -let result = greeting.replace("Morning", "Evening"); // returns Good Evening -console.log(result); -``` - -This works for whole words like above or for single letters - -```js -let greeting = "Dog"; -let result = greeting.replace("D", "F"); // Returns "Fog" -console.log(result); -``` diff --git a/1-exercises/I-string-replace/exercise.js b/1-exercises/I-string-replace/exercise.js deleted file mode 100644 index 3f7104d7..00000000 --- a/1-exercises/I-string-replace/exercise.js +++ /dev/null @@ -1,46 +0,0 @@ -/* - You are given a sentence contains a story. - - Current it says - - "I like dogs. One day I went to the park and I saw 10 dogs. It was a great day." - - Change the story using .replace() so that it says - - "I like cats. One night I went to the park and I saw 100000 cats. It was a brilliant night." -*/ - -let story = - "I like dogs. One day I went to the park and I saw 10 dogs. It was a great day."; - -let result = story.replace("", ""); - -/* EXPECTED OUTPUT */ - -const util = require("util"); - -function test(test_name, actual, expected) { - console.log(""); - let status; - if (actual === expected) { - status = "PASSED"; - } else { - status = `FAILED: \nexpected: ${util.inspect( - expected - )} \nbut your function returned: ${util.inspect(actual)}`; - } - - console.log(`${test_name}: ${status}`); -} - -test( - "1. Original story has not been changed", - story, - "I like dogs. One day I went to the park and I saw 10 dogs. It was a great day." -); - -test( - "2. The result of the replace is correct", - result, - "I like cats. One night I went to the park and I saw 100000 cats. It was a brilliant night." -); diff --git a/1-exercises/J-string-substring/README.md b/1-exercises/J-string-substring/README.md deleted file mode 100644 index e3fa1d56..00000000 --- a/1-exercises/J-string-substring/README.md +++ /dev/null @@ -1,19 +0,0 @@ -# Using Substring On A String - -.substring() allows us to remove things from strings. - -To remove the start of a string you can start with 0. For example - -```js -let dessert = "ice cream and pancakes"; -let newDessert = dessert.substring(0, 9); -console.log(newDessert); // outputs "ice cream" -``` - -To remove the end of the string you can use .length - -```js -let dessert = "ice cream and pancakes"; -let newDessert = dessert.substring(10, dessert.length); -console.log(newDessert); // outputs "and pancakes" -``` diff --git a/1-exercises/J-string-substring/exercise.js b/1-exercises/J-string-substring/exercise.js deleted file mode 100644 index 4624db68..00000000 --- a/1-exercises/J-string-substring/exercise.js +++ /dev/null @@ -1,17 +0,0 @@ -/* - You are given an statement - - You should remove the word "and dogs" by using substring -*/ - -let statement = "I like programming and dogs"; - -statement = statement.substring(); - -console.log(statement); - -/* EXPECTED OUTPUT - - "I like programming" - -*/ diff --git a/1-exercises/J-string-substring/exercise2.js b/1-exercises/J-string-substring/exercise2.js deleted file mode 100644 index a1d9bf62..00000000 --- a/1-exercises/J-string-substring/exercise2.js +++ /dev/null @@ -1,35 +0,0 @@ -/* - You are given an array with a list of names. - - You should log only the peoples first names. - - For example, if the name is "John Smith" you should return "John" -*/ - -let names = [ - "Tamzin Lindsay", - "Jessica Tate", - "Tony Holcomb", - "Shae Patton", - "Arron Graham", -]; - -names[0] = names[0].substring(); -names[1] = names[1].substring(); -names[2] = names[2].substring(); -names[3] = names[3].substring(); -names[4] = names[4].substring(); - -names.forEach((name) => { - console.log(name); -}); - -/* EXPECTED OUTPUT - - "Tamzin" - "Jessica" - "Tony" - "Shae" - "Arron" - -*/ diff --git a/1-exercises/J-string-substring/exercise3.js b/1-exercises/J-string-substring/exercise3.js deleted file mode 100644 index 14f77417..00000000 --- a/1-exercises/J-string-substring/exercise3.js +++ /dev/null @@ -1,19 +0,0 @@ -/* - You are given an statement - - You should remove the word "not" by using .substring() and log the result. - - HINT: You will need to use .substring() twice -*/ - -let statement = "I do not like programming"; - -let result = ""; - -console.log(result); - -/* EXPECTED OUTPUT - - "I do like programming" - -*/ diff --git a/2-mandatory/4-space-colonies.js b/2-mandatory/4-space-colonies.js deleted file mode 100644 index 30095213..00000000 --- a/2-mandatory/4-space-colonies.js +++ /dev/null @@ -1,46 +0,0 @@ -/* - The voyagers decide that they quite like this planet, and some of them want to settle there and colonise it. - - They call the planet "Alpha" and they decide that the FAMILIES whose last names start with 'A' should stay, - while the others go on in search of other planets to call home. - - Create a function that returns an array of colonisers that will stay, according to the above rules. - - NOTE: don't include any element that is not a "family". - - HINT: Whenever you read the above the instructions, try to come up with the main input and output and logic - Input: Is an array - Output: Is an array - Logic: Only strings that start with A, and finish with family - -*/ - -function getSettlers() {} - -/* ======= TESTS - DO NOT MODIFY ===== */ - -test("getSettlers function works", () => { - const voyagers = [ - "Adam family", - "Potter family", - "Eric", - "Aldous", - "Button family", - "Jude", - "Carmichael", - "Bunny", - "Asimov", - "Oscar family", - "Avery family", - "Archer family", - "Just A. family", - "A Great family", - ]; - - expect(getSettlers(voyagers)).toEqual([ - "Adam family", - "Avery family", - "Archer family", - "A Great family", - ]); -}); diff --git a/README.md b/README.md index fe8d9029..0439b72e 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,9 @@ Like learning a musical instrument, programming requires daily practise. -The exercises are split into three folders: `exercises`, `mandatory` and `extra`. All homework in the `exercise` and `mandatory` section **must** be completed for homework by the following lesson. +The exercises are split into two folders: `mandatory` and `extra`. All homework in the `mandatory` section **must** be completed for homework by the following lesson. The `extra` folder contains exercises that you can complete to challenge yourself, but are not required for the following lesson. - ## Solutions The solutions for this coursework can be found here: @@ -15,9 +14,8 @@ This is a **private** repository. Please request access from your Teachers, Budd ## Testing your work -- Each of the *.js files in the `1-exercises` folder can be run from the terminal using the `node` command with the path to the file. For example, `node 1-exercises/A-array-find/exercise.js` can be run from the root of the project. -- To run the tests in the `2-mandatory` folder, run `npm run test` from the root of the project (after having run `npm install` once before). -- To run the tests in the `3-extra` folder, run `npm run extra-tests` from the root of the project (after having run `npm install` once before). +- To run the tests in the `mandatory` folder, run `npm run test` from the root of the project (after having run `npm install` once before). +- To run the tests in the `extra` folder, run `npm run extra-tests` from the root of the project (after having run `npm install` once before). ## Instructions for submission diff --git a/3-extra/1-card-vailidator.md b/extra/1-card-validator.js similarity index 69% rename from 3-extra/1-card-vailidator.md rename to extra/1-card-validator.js index 65ace5bc..9bdf4de6 100644 --- a/3-extra/1-card-vailidator.md +++ b/extra/1-card-validator.js @@ -1,4 +1,5 @@ -## **PROJECT: Credit Card Validator** +/* +PROJECT: Credit Card Validator In this project you'll write a script that validates whether or not a credit card number is valid. @@ -11,26 +12,22 @@ Here are the rules for a valid number: For example, the following credit card numbers are valid: -```markdown -9999777788880000 -6666666666661666 -``` +- 9999777788880000 +- 6666666666661666 And the following credit card numbers are invalid: -```markdown -a92332119c011112 (invalid characters) -4444444444444444 (only one type of number) -1111111111111110 (sum less than 16) -6666666666666661 (odd final number) -``` +- a92332119c011112 (invalid characters) +- 4444444444444444 (only one type of number) +- 1111111111111110 (sum less than 16) +- 6666666666666661 (odd final number) These are the requirements your project needs to fulfill: -- Make a JavaScript file with a name that describes its contents. - Create a function with a descriptive name which makes it clear what the function does. The function should take one argument, the credit card number to validate. - Write at least 2 comments that explain to others what a line of code is meant to do. - Return a boolean from the function to indicate whether the credit card number is valid. -- Use `node` from the command line to test if your code works as expected. +- Use `node extra/1-card-validator.js` from the command line to test if your code works as expected. Good luck! +*/ diff --git a/3-extra/2-sorting-algorithm.js b/extra/2-sorting-algorithm.js similarity index 100% rename from 3-extra/2-sorting-algorithm.js rename to extra/2-sorting-algorithm.js diff --git a/2-mandatory/1-create-functions.js b/mandatory/1-create-functions.js similarity index 76% rename from 2-mandatory/1-create-functions.js rename to mandatory/1-create-functions.js index 6df12961..b0ba03b7 100644 --- a/2-mandatory/1-create-functions.js +++ b/mandatory/1-create-functions.js @@ -3,16 +3,14 @@ Write a function that: - Accepts an array as a parameter. - Returns a new array containing the first five elements of the passed array. */ -function first5() { -} +function first5() {} /* Write a function that: - Accepts an array as a parameter. - Returns a new array containing the same elements, except sorted. */ -function sortArray() { -} +function sortArray() {} /* NOTE: This exercise is the same as one you did last week - try to do it again using things you learnt this week. @@ -24,8 +22,7 @@ Write a function that: - Removes any forward slashes (/) in the strings. - Makes the strings all lowercase. */ -function tidyUpString() { -} +function tidyUpString() {} /* Write a function that: @@ -33,8 +30,7 @@ Write a function that: - Returns a new array containing the same elements, but without the element at the passed index. */ -function remove() { -} +function remove() {} /* Write a function that: @@ -44,10 +40,24 @@ Write a function that: - Numbers greater 100 must be replaced with 100. */ -function formatPercentage() { -} +function formatPercentage() {} -/* ======= TESTS - DO NOT MODIFY ===== */ +/* +Write a function that: +- Takes an array as a parameter +- Returns the *first* name which starts with A and is longer than 7 letters. +*/ + +function findLongNameThatStartsWithA() {} + +/* +=================================================== +======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== +There are some Tests in this file that will help you work out if your code is working. +To run the tests for just this one file, type `npm test -- --testPathPattern 1-create-functions` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +=================================================== +*/ test("first5 function works for more than five elements", () => { const numbers = [1, 2, 3, 4, 5, 6, 7, 8]; @@ -143,3 +153,19 @@ test("formatPercentage function works", () => { "0.37%", ]); }); + +test("findLongNameThatStartsWithA function works", () => { + expect( + findLongNameThatStartsWithA([ + "Rakesh", + "Antonio", + "Alexandra", + "Andronicus", + "Annam", + "Mikey", + "Anastasia", + "Karim", + "Ahmed", + ]) + ).toEqual("Alexandra"); +}); diff --git a/mandatory/10-is-in-uk.js b/mandatory/10-is-in-uk.js new file mode 100644 index 00000000..7eb13981 --- /dev/null +++ b/mandatory/10-is-in-uk.js @@ -0,0 +1,28 @@ +/* + Write a function that accepts a name of a country as a string and returns true or false whether the country is in the UK. +*/ + +const ukNations = ["Scotland", "Wales", "England", "Northern Ireland"]; + +function isInUK(country) {} + +/* +=================================================== +======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== +There are some Tests in this file that will help you work out if your code is working. +To run the tests for just this one file, type `npm test -- --testPathPattern 11-is-in-uk` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +=================================================== +*/ + +test("isInUK works - case 1", () => { + expect(isInUK("France")).toEqual(false); +}); + +test("isInUK works - case 2", () => { + expect(isInUK("Republic of Ireland")).toEqual(false); +}); + +test("isInUK works - case 3", () => { + expect(isInUK("England")).toEqual(true); +}); diff --git a/mandatory/11-fizz-buzz.js b/mandatory/11-fizz-buzz.js new file mode 100644 index 00000000..22955488 --- /dev/null +++ b/mandatory/11-fizz-buzz.js @@ -0,0 +1,44 @@ +/* + Write a function that accepts an array of numbers as a parameter and loops through the numbers. + For each number the function should print (using console.log): + + - If the number is a multiple of 3, print “Fizz” instead of the number + - If the number is a multiple of 5, print “Buzz” instead of the number + - If the number is a multiple of *both* 3 and 5, print “FizzBuzz” instead of the number + - If the number is none of the above, just print the number +*/ + +function fizzBuzz(numbers) {} + +/* +=================================================== +======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== +There are some Tests in this file that will help you work out if your code is working. +To run the tests for just this one file, type `npm test -- --testPathPattern 12-fizz-buzz` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +=================================================== +*/ + +test("fizzBuzz works", () => { + const log = jest.spyOn(console, "log").mockImplementation(() => {}); + + fizzBuzz([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); + + expect(log).toHaveBeenNthCalledWith(1, 1); + expect(log).toHaveBeenNthCalledWith(2, 2); + expect(log).toHaveBeenNthCalledWith(3, "Fizz"); + expect(log).toHaveBeenNthCalledWith(4, 4); + expect(log).toHaveBeenNthCalledWith(5, "Buzz"); + expect(log).toHaveBeenNthCalledWith(6, "Fizz"); + expect(log).toHaveBeenNthCalledWith(7, 7); + expect(log).toHaveBeenNthCalledWith(8, 8); + expect(log).toHaveBeenNthCalledWith(9, "Fizz"); + expect(log).toHaveBeenNthCalledWith(10, "Buzz"); + expect(log).toHaveBeenNthCalledWith(11, 11); + expect(log).toHaveBeenNthCalledWith(12, "Fizz"); + expect(log).toHaveBeenNthCalledWith(13, 13); + expect(log).toHaveBeenNthCalledWith(14, 14); + expect(log).toHaveBeenNthCalledWith(15, "FizzBuzz"); + + log.mockRestore(); +}); diff --git a/2-mandatory/2-oxygen-levels.js b/mandatory/2-oxygen-levels.js similarity index 74% rename from 2-mandatory/2-oxygen-levels.js rename to mandatory/2-oxygen-levels.js index 5711c5e5..f06e41dc 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/mandatory/2-oxygen-levels.js @@ -13,7 +13,14 @@ function findSafeOxygenLevel() {} -/* ======= TESTS - DO NOT MODIFY ===== */ +/* +=================================================== +======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== +There are some Tests in this file that will help you work out if your code is working. +To run the tests for just this one file, type `npm test -- --testPathPattern 2-oxygen-levels` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +=================================================== +*/ test("findSafeOxygenLevel function works - case 1", () => { expect( diff --git a/2-mandatory/3-bush-berries.js b/mandatory/3-bush-berries.js similarity index 65% rename from 2-mandatory/3-bush-berries.js rename to mandatory/3-bush-berries.js index b434a507..e5d23be8 100644 --- a/2-mandatory/3-bush-berries.js +++ b/mandatory/3-bush-berries.js @@ -25,9 +25,16 @@ function isBushSafe(berryArray) { //Write your code here } -/* ======= TESTS - DO NOT MODIFY ===== */ +/* +=================================================== +======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== +There are some Tests in this file that will help you work out if your code is working. +To run the tests for just this one file, type `npm test -- --testPathPattern 3-bush-berries` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +=================================================== +*/ -test("isBushSafe finds toxic busy", () => { +test("isBushSafe finds toxic bush", () => { expect( isBushSafe(["pink", "pink", "pink", "neon", "pink", "transparent"]) ).toEqual("Toxic! Leave bush alone!"); @@ -38,3 +45,9 @@ test("isBushSafe function finds safe bush", () => { "Bush is safe to eat from" ); }); + +test("isBushSafe finds toxic bush when all toxic", () => { + expect(isBushSafe(["neon", "transparent", "red", "green", "grey"])).toEqual( + "Toxic! Leave bush alone!" + ); +}); diff --git a/2-mandatory/5-eligible-students.js b/mandatory/4-eligible-students.js similarity index 65% rename from 2-mandatory/5-eligible-students.js rename to mandatory/4-eligible-students.js index f92478e0..298eb5ae 100644 --- a/2-mandatory/5-eligible-students.js +++ b/mandatory/4-eligible-students.js @@ -9,7 +9,14 @@ function getEligibleStudents() {} -/* ======= TESTS - DO NOT MODIFY ===== */ +/* +=================================================== +======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== +There are some Tests in this file that will help you work out if your code is working. +To run the tests for just this one file, type `npm test -- --testPathPattern 5-eligible-students` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +=================================================== +*/ test("getEligibleStudents function works", () => { const attendance = [ diff --git a/2-mandatory/6-journey-planner.js b/mandatory/5-journey-planner.js similarity index 83% rename from 2-mandatory/6-journey-planner.js rename to mandatory/5-journey-planner.js index 25a14083..f5a2027e 100644 --- a/2-mandatory/6-journey-planner.js +++ b/mandatory/5-journey-planner.js @@ -1,32 +1,3 @@ -/* - Before we go to the big story, we will introduce some more string methods. - Some of the methods you're using on arrays are similar to ones you can use on strings. - Methods like: IndexOf, Include, Search, Slice , Spilt and more. - - You can always Google how a method of a string works! - Here are links to some of those: - - https://www.w3schools.com/js/js_string_methods.asp - - https://javascript.info/string#quotes - Now let's do this small exercise - - Using string methods update the checkCodeIsThere() function - - The function will have a string as a paramter - - The function should check if the word "code" exists in the string - - If it does exist, return the index of it, if not return "Not found" - - Hint: search for string methods like Includes and IndexOf. -*/ - -function checkCodeIsThere(stringText) { - let magicWord = "code"; - //edit code below - if (stringText) { - return stringText; - } else { - return "Not found"; - } -} - /* I am new to London and would like to know what transport I can take to different famous locations. The input provided contains a list of locations in London. Each of locations is followed by a list @@ -119,13 +90,20 @@ function getLocationName() {} - Use array method to remove locations that are not accessible by the given transportMode. - Use array method to manipulate its elements. - Advanced challange: try to use arrow function when invoking an array method. + Advanced challenge: try to use arrow function when invoking an array method. */ function journeyPlanner(locations, transportMode) { // Implement the function body } -/* ======= TESTS - DO NOT MODIFY ===== */ +/* +=================================================== +======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== +There are some Tests in this file that will help you work out if your code is working. +To run the tests for just this one file, type `npm test -- --testPathPattern 6-journey-planner` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +=================================================== +*/ const string1 = "I Love coding and perfect code makes me happy"; const string2 = "I don't like to do coding"; diff --git a/2-mandatory/7-lane-names.js b/mandatory/6-lane-names.js similarity index 53% rename from 2-mandatory/7-lane-names.js rename to mandatory/6-lane-names.js index 40c31b68..fb75226a 100644 --- a/2-mandatory/7-lane-names.js +++ b/mandatory/6-lane-names.js @@ -8,7 +8,14 @@ function getLanes() {} -/* ======= TESTS - DO NOT MODIFY ===== */ +/* +=================================================== +======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== +There are some Tests in this file that will help you work out if your code is working. +To run the tests for just this one file, type `npm test -- --testPathPattern 7-lane-names` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +=================================================== +*/ test("getLanes function works", () => { const streetNames = [ diff --git a/2-mandatory/8-password-validator.js b/mandatory/7-password-validator.js similarity index 80% rename from 2-mandatory/8-password-validator.js rename to mandatory/7-password-validator.js index dc3e84d6..44516423 100644 --- a/2-mandatory/8-password-validator.js +++ b/mandatory/7-password-validator.js @@ -45,7 +45,14 @@ function containsSymbol(string) { return /[!#$%.*&]/.test(string); } -/* ======= TESTS - DO NOT MODIFY ===== */ +/* +=================================================== +======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== +There are some Tests in this file that will help you work out if your code is working. +To run the tests for just this one file, type `npm test -- --testPathPattern 8-password-validator` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +=================================================== +*/ test("Example 1", () => { expect( diff --git a/mandatory/8-group-people.js b/mandatory/8-group-people.js new file mode 100644 index 00000000..7cd966ad --- /dev/null +++ b/mandatory/8-group-people.js @@ -0,0 +1,33 @@ +/* + Write a function that accepts two arrays as parameters: + 1. An array of volunteer names + 2. An array of trainee names + + The function should return a single array containing *both* the volunteers and trainee names. +*/ + +function groupPeople() {} + +/* +=================================================== +======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== +There are some Tests in this file that will help you work out if your code is working. +To run the tests for just this one file, type `npm test -- --testPathPattern 2-oxygen-levels` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +=================================================== +*/ + +test("volunteersAndTrainees works", () => { + let volunteers = ["Daniel", "Irina", "Rares"]; + let trainees = ["Rukmini", "Abdul", "Austine", "Swathi"]; + + expect(groupPeople(volunteers, trainees)).toEqual([ + "Daniel", + "Irina", + "Rares", + "Rukmini", + "Abdul", + "Austine", + "Swathi", + ]); +}); diff --git a/mandatory/9-capitalize.js b/mandatory/9-capitalize.js new file mode 100644 index 00000000..3ecccfad --- /dev/null +++ b/mandatory/9-capitalize.js @@ -0,0 +1,28 @@ +/* + Write a function that accepts a string as a parameter and returns a string where the first letter of the input string is uppercase. + For example, capitalize("hello") should return "Hello" + Tip: use the string method .split() and the array method .join() +*/ + +function capitalize(str) {} + +/* +=================================================== +======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== +There are some Tests in this file that will help you work out if your code is working. +To run the tests for just this one file, type `npm test -- --testPathPattern 10-capitalize` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +=================================================== +*/ + +test("capitalize works - case 1", () => { + expect(capitalize("hello")).toEqual("Hello"); +}); + +test("capitalize works - case 2", () => { + expect(capitalize("daniel")).toEqual("Daniel"); +}); + +test("capitalize works - case 3", () => { + expect(capitalize("ivina")).toEqual("Ivina"); +});