Skip to content

Commit ae7da7e

Browse files
authored
Array workshop tidy (#6)
* rename files to remove gaps in order * correct task instructions * fix formatting * remove whitespace * fix array formatting
1 parent 645c062 commit ae7da7e

10 files changed

Lines changed: 98 additions & 104 deletions

File tree

arrays/100.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// Predict and explain...
2-
// What will nums.length evalute to after running the code here
3-
// Check your prediction and explanation by running the code. Use mdn documentation for mdn to help you make your prediction
4-
//
2+
// What will get logged to the console when running the code below?
3+
// Check your prediction and explanation by running the code. Use the MDN documentation for .push to help build your understanding of these outcomes
54

6-
const cities = ["Manchester", "London","Birmingham","Cape Town","Glasgow"];
7-
const result = cities.push("Liverpool","Sheffield");
5+
const cities = ["Manchester", "London", "Birmingham", "Cape Town", "Glasgow"];
6+
const result = cities.push("Liverpool", "Sheffield");
87

98
console.log(`The result is ${result}`);
10-
console.log("cities now looks like this: ",cities);
9+
console.log("cities now looks like this: ", cities);

arrays/200.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Predict and explain...
2-
// What will nums.length evalute to after running the code here
2+
// What will nums.length evaluate to after running the code here?
33
// Check your prediction and explanation by running the code
44

5-
const nums = [10,3,5,6,];
5+
const nums = [10, 3, 5, 6];
66

77
nums[6] = 50;
88

arrays/300.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// the head of an array is the first element of an array
22
// the tail of an array is the last element of an array
33

4-
54
const alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
65

76
function getHead(arr) {
@@ -15,5 +14,6 @@ function getTail(arr) {
1514
console.log(`The first letter of the alphabet is ${getHead(alphabet)}`);
1615
console.log(`The last letter of the alphabet is ${getTail(alphabet)}`);
1716
console.log(alphabet.length);
17+
1818
// Explain why alphabet length is now 24
1919
// How could we change the functions we defined to return the same values, but not change the length of alphabet?

arrays/0.js renamed to arrays/400.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Try predicting and explaining what will get logged to the console when the code runs
33
// To check your prediction, play computer using the Python Visualiser: https://pythontutor.com/render.html#mode=display
44

5-
65
const ingredients = ["olive oil","tomatoes",'garlic','onion', 'carrot'];
76
let ingredientsCopy = ingredients;
87
ingredientsCopy.push('pasta','salt','pepper');

arrays/500.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// list is an array that holds a mix of data types
2+
// we need to collect just the numbers and not the strings
3+
// before running the tests, make a prediction and explanation about what you expect the function to return
4+
// fix anything that doesn't work
5+
6+
function collectNumbers(list) {
7+
const numbersOnly = [];
8+
for (const item of list) {
9+
if (item === 'string') {
10+
numbersOnly.push(item);
11+
}
12+
}
13+
return numbersOnly;
14+
}
15+
16+
// NOTE: you do not need to change anything in the test
17+
test('only collects numbers in the array',() => {
18+
const currentOutput = collectNumbers([10.1, "hello", 6.1, 8.0, 9.7, 10.1, "hi", 3.5, "oops"]);
19+
const targetOutput = [10.1, 6.1, 8.0, 9.7, 10.1, 3.5];
20+
21+
expect(currentOutput).toEqual(targetOutput);
22+
});
23+
24+

arrays/600.test.js

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
1-
2-
3-
// list is an array that holds a mix of data types
4-
// we need to collect just the numbers and not the strings
5-
// before runnning the tests, make a prediction and explanation about what you expect the function to return
6-
// fix anything that doesn't work
7-
8-
function collectNumbers(list) {
9-
10-
const numbersOnly = [];
11-
for (const item of list) {
12-
if (item === 'string') {
13-
numbersOnly.push(item);
14-
}
15-
}
16-
return numbersOnly;
1+
// Predict and explain...
2+
// Below is a function countWords and a test that checks that it counts the words in a string correctly
3+
// At the moment, it isn't working. Try to reason about what happens when the function is called.
4+
// Check your prediction and explanation using the test
5+
// Fix anything that doesn't work
6+
7+
function countWords(text) {
8+
return text.split('').length;
179
}
1810

19-
test('only collects numbers in the array',() => {
20-
const currentOutput = collectNumbers([10.1,"hello",6.1,8.0, 9.7, 10.1,"hi", 3.5,"oops"]);
21-
const targetOutput = [10.1,6.1,8.0, 9.7, 10.1, 3.5,];
22-
23-
expect(currentOutput).toEqual(targetOutput);
24-
});
25-
26-
11+
// NOTE: you do not need to change anything in the test
12+
test('should count the words in a string of text', () => {
13+
const text = "Here is a plain sentence with some information! Try to find the word count";
14+
expect(countWords(text)).toBe(14);
15+
});

arrays/700.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Predict and explain...
2+
// We've seen for..of loops used for arrays.
3+
// Below we try to use it with a variable which isn't an array.
4+
// What do you think will get logged?
5+
// Don't run the code until you've predicted and explained the whole file.
6+
// Then check your prediction and explanation by running the code
7+
8+
const sentence = "I really enjoy ice cream";
9+
10+
for (const part of sentence) {
11+
console.log(part);
12+
}
13+
14+
// What's the difference between what was written above, and what's below?
15+
// How will they behave differently?
16+
17+
const parts = sentence.split(" ");
18+
19+
for (const part of parts) {
20+
console.log(part);
21+
}

arrays/700.test.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

arrays/800.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
1-
// Predict and explain...
2-
// We've seen for..of loops used for arrays.
3-
// Below we try to use it with a variable which isn't an array.
4-
// What do you think will get logged?
5-
// Don't run the code until you've predicted and explained the whole file.
6-
// Then check your prediction and explanation by running the code
1+
// Below, we have two functions which have the same aim.
2+
// Both functions take a word as a parameter.
3+
// If the function has been called with that word before (regardless of case), the previous version of the word is returned.
4+
// So if you called the function with "hello", then "HELLO", the second call will return "hello".
5+
// If the word has not been used before, it will just be returned as-is.
76

8-
const sentence = "I really enjoy ice cream";
7+
// One of these functions has side-effects.
8+
// When you call it, it does something other than just returning a value based only on its parameters.
9+
//
10+
// The other doesn't have side effects.
11+
// When you call it with the same arguments, it always does exactly the same thing,
12+
// and you can see everything it does in its return value.
13+
//
14+
// Which function has side-effects? Which doesn't?
15+
// Try to write tests for both functions.
16+
// Which was easier to test? What issues did you run into writing tests?
917

10-
for (const part of sentence) {
11-
console.log(part);
12-
}
13-
14-
// What's the difference between what was written above, and what's below?
15-
// How will they behave differently?
18+
const previousWords = [];
1619

17-
const parts = sentence.split(" ");
20+
function getPreviousCaseOfWordOne(word) {
21+
for (const previousWord of previousWords) {
22+
if (previousWord.toLowerCase() === word.toLowerCase()) {
23+
return previousWord;
24+
}
25+
}
26+
previousWords.push(word);
27+
return word;
28+
}
1829

19-
for (const part of parts) {
20-
console.log(part);
30+
function getPreviousCaseOfWordTwo(word, words) {
31+
for (const previousWord of words) {
32+
if (previousWord.toLowerCase() === word.toLowerCase()) {
33+
return previousWord;
34+
}
35+
}
36+
return word;
2137
}

arrays/900.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)