Skip to content

Commit d19fa0c

Browse files
author
Anna Aitchison
committed
Add code from arrays and crud workshop
0 parents  commit d19fa0c

13 files changed

Lines changed: 1320 additions & 0 deletions

File tree

arrays/0.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Predict and explain...
2+
// Try predicting and explaining what will get logged to the console when the code runs
3+
// To check your prediction, play computer using the Python Visualiser: https://pythontutor.com/render.html#mode=display
4+
5+
6+
const ingredients = ["olive oil","tomatoes",'garlic','onion', 'carrot'];
7+
let ingredientsCopy = ingredients;
8+
ingredientsCopy.push('pasta','salt','pepper');
9+
const otherRecipe = ["olive oil","tomatoes",'garlic','onion', 'carrot','pasta','salt','pepper'];
10+
11+
console.log(ingredients === ingredientsCopy);
12+
console.log(ingredients === otherRecipe);
13+
console.log(otherRecipe === ingredientsCopy);
14+
console.log(otherRecipe.length === ingredients.length);

arrays/100.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// 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+
//
5+
6+
const cities = ["Manchester", "London","Birmingham","Cape Town","Glasgow"];
7+
const result = cities.push("Liverpool","Sheffield");
8+
9+
console.log(`The result is ${result}`);
10+
console.log("cities now looks like this: ",cities);

arrays/200.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// 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
4+
5+
const nums = [10,3,5,6,];
6+
7+
nums[6] = 50;
8+
9+
console.log(nums.length);

arrays/300.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// the head of an array is the first element of an array
2+
// the tail of an array is the last element of an array
3+
4+
5+
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'];
6+
7+
function getHead(arr) {
8+
return arr.shift();
9+
};
10+
11+
function getTail(arr) {
12+
return arr.pop();
13+
};
14+
15+
console.log(`The first letter of the alphabet is ${getHead(alphabet)}`);
16+
console.log(`The last letter of the alphabet is ${getTail(alphabet)}`);
17+
console.log(alphabet.length);
18+
// Explain why alphabet length is now 24
19+
// How could we change the functions we defined to return the same values, but not change the length of alphabet?

arrays/600.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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;
17+
}
18+
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+

arrays/700.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 tests
5+
// Fix anything that doesn't work
6+
7+
function countWords(text) {
8+
return text.split('').length;
9+
}
10+
11+
12+
test('should count the words in a string of text', () => {
13+
14+
const text = "Here is a plain sentence with some information! Try to find the word count";
15+
expect(countWords(text)).toBe(14);
16+
17+
});

arrays/800.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/900.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.
6+
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?
17+
18+
const previousWords = [];
19+
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+
}
29+
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;
37+
}

crud/albums.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"albumId": "10",
4+
"artistName": "Beyoncé",
5+
"collectionName": "Lemonade",
6+
"artworkUrl100": "http://is1.mzstatic.com/image/thumb/Music20/v4/23/c1/9e/23c19e53-783f-ae47-7212-03cc9998bd84/source/100x100bb.jpg",
7+
"releaseDate": "2016-04-25T07:00:00Z",
8+
"primaryGenreName": "Pop",
9+
"url": "https://www.youtube.com/embed/PeonBmeFR8o?rel=0&controls=0&showinfo=0"
10+
},
11+
{
12+
"albumId": "11",
13+
"artistName": "Beyoncé",
14+
"collectionName": "Dangerously In Love",
15+
"artworkUrl100": "http://is1.mzstatic.com/image/thumb/Music/v4/18/93/6d/18936d85-8f6b-7597-87ef-62c4c5211298/source/100x100bb.jpg",
16+
"releaseDate": "2003-06-24T07:00:00Z",
17+
"primaryGenreName": "Pop",
18+
"url": "https://www.youtube.com/embed/ViwtNLUqkMY?rel=0&controls=0&showinfo=0"
19+
}
20+
]

crud/index.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<title></title>
7+
<meta name="description" content="" />
8+
<meta name="viewport" content="width=device-width, initial-scale=1" />
9+
<link rel="stylesheet" href="" />
10+
</head>
11+
<body>
12+
<!--[if lt IE 7]>
13+
<p class="browsehappy">
14+
You are using an <strong>outdated</strong> browser. Please
15+
<a href="#">upgrade your browser</a> to improve your experience.
16+
</p>
17+
<![endif]-->
18+
19+
<script src="" async defer></script>
20+
</body>
21+
</html>

0 commit comments

Comments
 (0)