Skip to content
This repository was archived by the owner on Oct 26, 2020. It is now read-only.

Add Old Homework To Scotland Class 4 Branch #852

Merged
merged 3 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions week-2/Homework/L-extra/level-1/1-boolean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// The code is valid but the program does not produce the expected result
// Fix it.

var isHappy = false;

if (isHappy) {
console.log("I am happy");
} else {
console.log("I am not happy");
}

/*
EXPECTED RESULT
---------------
I am happy
*/
17 changes: 17 additions & 0 deletions week-2/Homework/L-extra/level-1/2-validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Update the variable `isBigEnough` so that the program produces the expected result
// TIP: You should write an expression that returns a boolean value

var num = 10;
var isBigEnough; // ONLY EDIT THIS LINE

if (isBigEnough) {
console.log("num is bigger than or equal to 10");
} else {
console.log("num is not big enough");
}

/*
EXPECTED RESULT
---------------
num is bigger than or equal to 10
*/
13 changes: 13 additions & 0 deletions week-2/Homework/L-extra/level-1/3-sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Update the variable `sortedLetters` so that it contains the values of `letters` in ascending order
// TIP: use an array method

var letters = ["a", "n", "c", "e", "z", "f"];
var sortedLetters; // ONLY EDIT THIS LINE

console.log(sortedLetters);

/*
EXPECTED RESULT
---------------
[ 'a', 'c', 'e', 'f', 'n', 'z' ]
*/
16 changes: 16 additions & 0 deletions week-2/Homework/L-extra/level-1/4-slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Write a function `first5` that:
// - returns the first 5 items from a provided array

/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
var numbers = [1, 2, 3, 4, 5, 6, 7, 8];
var first5Numbers = first5(numbers);

console.log(first5Numbers);

/*
EXPECTED RESULT
---------------
[1, 2, 3, 4, 5]
*/
14 changes: 14 additions & 0 deletions week-2/Homework/L-extra/level-1/5-indexOf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Find the index of an item in an array
// TIP: use the .indexOf() method (search the web for documentation if you're not sure)

var itemToFind = 7;
var arr = [3, 5, 61, 7, 123];
var index; // ONLY EDIT THIS LINE

console.log(index);

/*
EXPECTED RESULT
---------------
3
*/
22 changes: 22 additions & 0 deletions week-2/Homework/L-extra/level-1/6-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// An array of mentor names has been provided to you, as well as a function to tidy up strings.
// Declare a new array (`mentorsTidy`) containing:
// - every item from `mentors` run through the `tidyUpString` function
// TIP: Use the .map() method

function tidyUpString(str) {
return str
.trim()
.toLowerCase()
.replace("/", "");
}

var mentors = ["/Daniel ", "irina ", " Gordon", "ashleigh "];
var mentorsTidy; // ONLY EDIT THIS LINE

console.log(mentorsTidy);

/*
EXPECTED RESULT
---------------
["daniel", "irina", "gordon", "ashleigh"]
*/
17 changes: 17 additions & 0 deletions week-2/Homework/L-extra/level-2/1-boolean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// The code is valid but the program does not produce the expected result
// Why doesn't it work?
// Fix it.

var isHappy = "false";

if (isHappy) {
console.log("I am happy");
} else {
console.log("I am not happy");
}

/*
EXPECTED RESULT
---------------
I am not happy
*/
27 changes: 27 additions & 0 deletions week-2/Homework/L-extra/level-2/2-validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Complete the function to check if the variable `num` satisfies the following requirements:
// - is a number
// - is a positive number
// - is less than or equal to 100
// Tip: write other small functions for each requirement

function validate(num) {}

/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */

console.log(validate(10));
console.log(validate(10.5));
console.log(validate(101));
console.log(validate(-12));
console.log(validate("16"));

/*
EXPECTED RESULT
---------------
true
true
false
false
false
*/
14 changes: 14 additions & 0 deletions week-2/Homework/L-extra/level-2/3-sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Update the variable `sortedLetters`.
// It should contain the values of `letters` and `letters2` in ascending order

var letters = ["a", "n", "c", "e", "z", "f"];
var letters2 = ["w", "b", "v", "g", "l", "o"];
var sortedLetters; // ONLY EDIT THIS LINE

console.log(sortedLetters);

/*
EXPECTED RESULT
---------------
[ 'a', 'b', 'c', 'e', 'f', 'g', 'l','n', 'o', 'v', 'w', 'z' ]
*/
28 changes: 28 additions & 0 deletions week-2/Homework/L-extra/level-2/4-slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Write a function that removes an element from an array
// The function must:
// - NOT change the original array
// - return a new array with the item removed
// - remove the item at the specified index

function remove(arr, index) {
return; // complete this statement
}

/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
var numbers = [1, 2, 3];
var names = ["Irina", "Ashleigh", "Mozafar"];

var newNumbers = remove(numbers, 2);
var newNames = remove(names, 1);

console.log(newNumbers);
console.log(newNames);

/*
EXPECTED RESULT
---------------
[1, 2]
[Irina, Mozafar]
*/
28 changes: 28 additions & 0 deletions week-2/Homework/L-extra/level-2/5-indexOf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Write a function that removes an element from an array
// The function must:
// - NOT change the original array
// - return a new array with the first item matching `valueToRemove` removed
// TIP: Use the .indexOf() method

function remove(arr, valueToRemove) {
return; // complete this statement
}

/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
var numbers = [1, 2, 3];
var names = ["Irina", "Ashleigh", "Mozafar"];

var newNumbers = remove(numbers, 2);
var newNames = remove(names, "Ashleigh");

console.log(newNumbers);
console.log(newNames);

/*
EXPECTED RESULT
---------------
[1, 3]
[Irina, Mozafar]
*/
14 changes: 14 additions & 0 deletions week-2/Homework/L-extra/level-2/6-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// You have been given an array of percetages
// 1. Write a function that formats the numbers into a string with the percentage symbol appended e.g. "10%"
// 2. Declare a new array, `percentagesFormatted`, containing
// - each item in `percentages` formatted by your function

var percentages = [1, 23, 92, 18];

console.log(percentagesFormatted);

/*
EXPECTED RESULT
---------------
[1%, 23%, 92%, 18%]
*/
18 changes: 18 additions & 0 deletions week-2/Homework/L-extra/level-3/1-boolean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// The code is valid but is misleading and could be improved
// Refactor the code to make it better
// What was wrong with the original code?
// Leave comments above your changes to explain

var isHappy = "false";

if (isHappy == true) {
console.log("I am happy");
} else {
console.log("I am not happy");
}

/*
EXPECTED RESULT
---------------
I am not happy
*/
25 changes: 25 additions & 0 deletions week-2/Homework/L-extra/level-3/2-validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Complete the function to check if the variable `num` satisfies the following requirements:
// - is a number
// - is an integer (not a float)
// - is not equal any of the numbers in the array `excludedNums`
// Tip: write other small functions for each requirement

var excludedNums = [6, 14, 91, 111];

function validate(num) {}

console.log(validate(6));
console.log(validate(10.5));
console.log(validate(101));
console.log(validate(-91));
console.log(validate("16"));

/*
EXPECTED RESULT
---------------
false
false
true
true
false
*/
17 changes: 17 additions & 0 deletions week-2/Homework/L-extra/level-3/3-sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 1. Update the variable `sortedNums`.
// It should contain the values of `nums` and `nums2` in ascending order
// Tip: you might need to read the documentation for .sort (search "mdn array sort")

var nums = [10, 1, 5, 29, 100];
var nums2 = [11, 6, 3, 29, 12];
var sortedNums; // complete this statement

console.log(sortedNums);

// 2. Using code, show that the variables nums and nums2 were not changed

/*
EXPECTED RESULT
---------------
[ 1, 3, 5, 6, 10, 11, 12, 29, 29, 100 ]
*/
32 changes: 32 additions & 0 deletions week-2/Homework/L-extra/level-3/4-slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Write a function that replaces an element in an array
// The function must:
// - NOT change the original array
// - return a new array with the replacement value inserted
// - insert the replacement value at the provided index

function replace(arr, index, value) {
return; // complete this statement
}

/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
var numbers = [1, 3, 3];
var names = ["Irina", "Ashleigh", "Mozafar"];

var newNumbers = replace(numbers, 1, 2);
var newNames = replace(names, 2, "Rares");

console.log(numbers);
console.log(newNumbers);
console.log(names);
console.log(newNames);

/*
EXPECTED RESULT
---------------
[1, 3, 3]
[1, 2, 3]
[Irina, Ashleigh, Mozafar]
[Irina, Ashleigh, Rares]
*/
33 changes: 33 additions & 0 deletions week-2/Homework/L-extra/level-3/5-indexOf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Write a function that replaces an element in an array
// The function must:
// - get the index of the first item matching `valueToReplace`
// - insert `newValue` at that index
// - NOT change the original array
// - return a new array with the replacement value inserted

function replace(arr, valueToReplace, newValue) {
return; // complete this statement
}

/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
var numbers = [1, 3, 3];
var names = ["Irina", "Ashleigh", "Mozafar"];

var newNumbers = replace(arr, 3, 2);
var newNames = replace(arr, "Ashleigh", "Rares");

console.log(numbers);
console.log(newNumbers);
console.log(names);
console.log(newNames);

/*
EXPECTED RESULT
---------------
[1, 3, 3]
[1, 2, 3]
[Irina, Ashleigh, Mozafar]
[Irina, Rares, Mozafar]
*/
26 changes: 26 additions & 0 deletions week-2/Homework/L-extra/level-3/6-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 1. Write a function (`capitalise`) that capitalises the first letter of a provided string
// 2. Declare a new array (`mentorsTidy`) containing:
// - every item from `mentors` run through the `tidyUpString` function
// - every resulting item run through the `capitalise` function

function tidyUpString(str) {
return str
.trim()
.toLowerCase()
.replace("/", "");
}

function capitalise(str) {
// complete this function
}

var mentors = ["/Daniel ", "irina ", " Gordon", "ashleigh "];
var mentorsTidyAndCapitalised;

console.log(mentorsTidyAndCapitalised);

/*
EXPECTED RESULT
---------------
["Daniel", "Irina", "Gordon", "Ashleigh"]
*/
Loading