This repository was archived by the owner on Jul 22, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 443
Array workshop tidy #385
Open
Liam310
wants to merge
6
commits into
main
Choose a base branch
from
array_workshop_tidy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Array workshop tidy #385
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
46d02d0
rename 0.js -> 400.js
Liam310 a98c30e
correct task instructions
Liam310 82a835a
fix formatting
Liam310 68d790e
remove whitespace
Liam310 547b2a0
rename files to remove gaps in order
Liam310 c8c3303
fix typo
Liam310 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,9 @@ | ||
| // Predict and explain... | ||
| // What will nums.length evalute to after running the code here | ||
| // Check your prediction and explanation by running the code. Use mdn documentation for mdn to help you make your prediction | ||
| // | ||
| // What will get logged to the console when running the code below? | ||
| // Check your prediction and explanation by running the code. Use the MDN documentation for .push to help build your understanding of these outcomes | ||
|
|
||
| const cities = ["Manchester", "London","Birmingham","Cape Town","Glasgow"]; | ||
| const result = cities.push("Liverpool","Sheffield"); | ||
| const cities = ["Manchester", "London", "Birmingham", "Cape Town", "Glasgow"]; | ||
| const result = cities.push("Liverpool", "Sheffield"); | ||
|
|
||
| console.log(`The result is ${result}`); | ||
| console.log("cities now looks like this: ",cities); | ||
| console.log("cities now looks like this: ", cities); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // list is an array that holds a mix of data types | ||
| // we need to collect just the numbers and not the strings | ||
| // before running the tests, make a prediction and explanation about what you expect the function to return | ||
| // fix anything that doesn't work | ||
|
|
||
| function collectNumbers(list) { | ||
| const numbersOnly = []; | ||
| for (const item of list) { | ||
| if (item === 'string') { | ||
| numbersOnly.push(item); | ||
| } | ||
| } | ||
| return numbersOnly; | ||
| } | ||
|
|
||
| // NOTE: you do not need to change anything in the test | ||
| test('only collects numbers in the array',() => { | ||
| const currentOutput = collectNumbers([10.1,"hello",6.1,8.0, 9.7, 10.1,"hi", 3.5,"oops"]); | ||
| const targetOutput = [10.1,6.1,8.0, 9.7, 10.1, 3.5,]; | ||
|
|
||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,15 @@ | ||
|
|
||
|
|
||
| // list is an array that holds a mix of data types | ||
| // we need to collect just the numbers and not the strings | ||
| // before runnning the tests, make a prediction and explanation about what you expect the function to return | ||
| // fix anything that doesn't work | ||
|
|
||
| function collectNumbers(list) { | ||
|
|
||
| const numbersOnly = []; | ||
| for (const item of list) { | ||
| if (item === 'string') { | ||
| numbersOnly.push(item); | ||
| } | ||
| } | ||
| return numbersOnly; | ||
| // Predict and explain... | ||
| // Below is a function countWords and a test that checks that it counts the words in a string correctly | ||
| // At the moment, it isn't working. Try to reason about what happens when the function is called. | ||
| // Check your prediction and explanation using the test | ||
| // Fix anything that doesn't work | ||
|
|
||
| function countWords(text) { | ||
| return text.split('').length; | ||
| } | ||
|
|
||
| test('only collects numbers in the array',() => { | ||
| const currentOutput = collectNumbers([10.1,"hello",6.1,8.0, 9.7, 10.1,"hi", 3.5,"oops"]); | ||
| const targetOutput = [10.1,6.1,8.0, 9.7, 10.1, 3.5,]; | ||
|
|
||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
|
|
||
|
|
||
| // NOTE: you do not need to change anything in the test | ||
| test('should count the words in a string of text', () => { | ||
| const text = "Here is a plain sentence with some information! Try to find the word count"; | ||
| expect(countWords(text)).toBe(14); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // Predict and explain... | ||
| // We've seen for..of loops used for arrays. | ||
| // Below we try to use it with a variable which isn't an array. | ||
| // What do you think will get logged? | ||
| // Don't run the code until you've predicted and explained the whole file. | ||
| // Then check your prediction and explanation by running the code | ||
|
|
||
| const sentence = "I really enjoy ice cream"; | ||
|
|
||
| for (const part of sentence) { | ||
| console.log(part); | ||
| } | ||
|
|
||
| // What's the difference between what was written above, and what's below? | ||
| // How will they behave differently? | ||
|
|
||
| const parts = sentence.split(" "); | ||
|
|
||
| for (const part of parts) { | ||
| console.log(part); | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,37 @@ | ||
| // Predict and explain... | ||
| // We've seen for..of loops used for arrays. | ||
| // Below we try to use it with a variable which isn't an array. | ||
| // What do you think will get logged? | ||
| // Don't run the code until you've predicted and explained the whole file. | ||
| // Then check your prediction and explanation by running the code | ||
| // Below, we have two functions which have the same aim. | ||
| // Both functions take a word as a parameter. | ||
| // If the function has been called with that word before (regardless of case), the previous version of the word is returned. | ||
| // So if you called the function with "hello", then "HELLO", the second call will return "hello". | ||
| // If the word has not been used before, it will just be returned as-is. | ||
|
|
||
| const sentence = "I really enjoy ice cream"; | ||
| // One of these functions has side-effects. | ||
| // When you call it, it does something other than just returning a value based only on its parameters. | ||
| // | ||
| // The other doesn't have side effects. | ||
| // When you call it with the same arguments, it always does exactly the same thing, | ||
| // and you can see everything it does in its return value. | ||
| // | ||
| // Which function has side-effects? Which doesn't? | ||
| // Try to write tests for both functions. | ||
| // Which was easier to test? What issues did you run into writing tests? | ||
|
|
||
| for (const part of sentence) { | ||
| console.log(part); | ||
| } | ||
|
|
||
| // What's the difference between what was written above, and what's below? | ||
| // How will they behave differently? | ||
| const previousWords = []; | ||
|
|
||
| const parts = sentence.split(" "); | ||
| function getPreviousCaseOfWordOne(word) { | ||
| for (const previousWord of previousWords) { | ||
| if (previousWord.toLowerCase() === word.toLowerCase()) { | ||
| return previousWord; | ||
| } | ||
| } | ||
| previousWords.push(word); | ||
| return word; | ||
| } | ||
|
|
||
| for (const part of parts) { | ||
| console.log(part); | ||
| function getPreviousCaseOfWordTwo(word, words) { | ||
| for (const previousWord of words) { | ||
| if (previousWord.toLowerCase() === word.toLowerCase()) { | ||
| return previousWord; | ||
| } | ||
| } | ||
| return word; | ||
| } |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debated whether to remove this trailing comma, but decided that its presence didn't contribute anything meaningful to the task which is ultimately about understanding how JS handles empty items of an array.
(if there were two trailing commas then that would have in impact on the array at the point of its initial declaration, but one has no effect)