Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
Open
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
19 changes: 13 additions & 6 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,32 @@
// Example 1
let a;
console.log(a);

// a has been declared by no value has been assigned.

// Example 2
function sayHello() {
let message = "Hello";
let message = "Hello";
}

let hello = sayHello();
console.log(hello);

// function sayHello has been declared by no value returned from the function
// hello has been assigned the function sayHello which does not return anything
// There is nothing for console.log to output so response is undefined.

// Example 3
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
console.log(`Hello ${user}`);
}

sayHelloToUser();

// function sayHelloToUser is being called without an argument
// sayHelloToUser expects an argument so will output undefined in the logged response
// for variable 'user' after outputting 'Hello '

// Example 4
let arr = [1,2,3];
let arr = [1, 2, 3];
console.log(arr[3]);

// there are only 3 entries ranging from [0] to [2] in the array 'arr'
// arr [3] doesn't have a value (because it's the 4th array element) so will show undefined.
8 changes: 6 additions & 2 deletions 1-exercises/B-array-literals/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
*/

let numbers = []; // add numbers from 1 to 10 into this array
let mentors; // Create an array with the names of the mentors: Daniel, Irina and Rares

for (let i = 0; i < 10; i++) {
numbers.push(i);
}
let mentors = []; // Create an array with the names of the mentors: Daniel, Irina and Rares
let names = ["Daniel", "Irina", "Rares"];
mentors.push(...names);
/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
Expand Down
4 changes: 2 additions & 2 deletions 1-exercises/C-array-get-set/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/

function first(arr) {
return; // complete this statement
return arr[0]; // complete this statement
}

function last(arr) {
return; // complete this statement
return arr[arr.length - 1]; // complete this statement
}

/*
Expand Down
2 changes: 2 additions & 0 deletions 1-exercises/C-array-get-set/exercises2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

let numbers = [1, 2, 3]; // Don't change this array literal declaration
numbers.push(4);
numbers[0] = 2;

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
23 changes: 10 additions & 13 deletions 1-exercises/D-for-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@
*/

const WRITERS = [
"Virginia Woolf",
"Zadie Smith",
"Jane Austen",
"Bell Hooks",
"Yukiko Motoya"
]

const AGES = [
59,
40,
41,
63,
49
"Virginia Woolf",
"Zadie Smith",
"Jane Austen",
"Bell Hooks",
"Yukiko Motoya",
];

const AGES = [59, 40, 41, 63, 49];

// TODO - Write for loop code here
for (let i = 0; i < WRITERS.length; i++) {
console.log(`${WRITERS[i]} is ${AGES[i]} years old`);
}

/*
The output should look something like this:
Expand Down
31 changes: 21 additions & 10 deletions 1-exercises/E-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,30 @@
*/

const BIRTHDAYS = [
"January 7th",
"February 12th",
"April 3rd",
"April 5th",
"May 3rd",
"July 11th",
"July 17th",
"September 28th",
"November 15th"
"January 7th",
"February 12th",
"April 3rd",
"April 5th",
"May 3rd",
"July 11th",
"July 17th",
"September 28th",
"November 15th",
];

function findFirstJulyBDay(birthdays) {
// TODO
// TODO
// for (let i = 0; i < BIRTHDAYS.length; i++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to remove old comments. Remember you still have old versions of your code in your version history - so you should be able to inspect the old version if you want to.

// if (BIRTHDAYS[i].includes("July")) {
// return BIRTHDAYS[i];
// }
// }

for (let birthday of BIRTHDAYS) {
if (birthday.includes("July")) {
return birthday;
}
}
}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
70 changes: 34 additions & 36 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Imagine we're making a weather app!

We have a list of cities that the user wants to track.
We also already have a temperatureService function which will take a city as a parameter and return a temparature.
We also already have a temperatureService function which will take a city as a parameter and return a temperature.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Praise: Good Spot


Implement the function below:
- take the array of cities as a parameter
Expand All @@ -12,52 +12,50 @@
*/

function getTemperatureReport(cities) {
// TODO
const weatherInCities = [];
for (const city of cities) {
let cityTemperature = temperatureService(city);
let cityStatement = `The temperature in ${city} is ${cityTemperature} degrees`;
weatherInCities.push(cityStatement);
}
return weatherInCities;
}


/* ======= TESTS - DO NOT MODIFY ===== */

function temperatureService(city) {
let temparatureMap = new Map();

temparatureMap.set('London', 10);
temparatureMap.set('Paris', 12);
temparatureMap.set('Barcelona', 17);
temparatureMap.set('Dubai', 27);
temparatureMap.set('Mumbai', 29);
temparatureMap.set('São Paulo', 23);
temparatureMap.set('Lagos', 33);
return temparatureMap.get(city);
let temparatureMap = new Map();

temparatureMap.set("London", 10);
temparatureMap.set("Paris", 12);
temparatureMap.set("Barcelona", 17);
temparatureMap.set("Dubai", 27);
temparatureMap.set("Mumbai", 29);
temparatureMap.set("São Paulo", 23);
temparatureMap.set("Lagos", 33);

return temparatureMap.get(city);
}

test("should return a temperature report for the user's cities", () => {
let usersCities = [
"London",
"Paris",
"São Paulo"
]

expect(getTemperatureReport(usersCities)).toEqual([
"The temperature in London is 10 degrees",
"The temperature in Paris is 12 degrees",
"The temperature in São Paulo is 23 degrees"
]);
let usersCities = ["London", "Paris", "São Paulo"];

expect(getTemperatureReport(usersCities)).toEqual([
"The temperature in London is 10 degrees",
"The temperature in Paris is 12 degrees",
"The temperature in São Paulo is 23 degrees",
]);
});

test("should return a temperature report for the user's cities (alternate input)", () => {
let usersCities = [
"Barcelona",
"Dubai"
]

expect(getTemperatureReport(usersCities)).toEqual([
"The temperature in Barcelona is 17 degrees",
"The temperature in Dubai is 27 degrees"
]);
let usersCities = ["Barcelona", "Dubai"];

expect(getTemperatureReport(usersCities)).toEqual([
"The temperature in Barcelona is 17 degrees",
"The temperature in Dubai is 27 degrees",
]);
});

test("should return an empty array if the user hasn't selected any cities", () => {
expect(getTemperatureReport([])).toEqual([]);
});
expect(getTemperatureReport([])).toEqual([]);
});
105 changes: 75 additions & 30 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,122 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
const charLimit = 65;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent - great use of a variable name charLimit! No magical numbers :)

const articlesUnderLimit = [];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good variable name


for (const articleTitle of allArticleTitles) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: What you've done is fine, but it would also be appropriate to use a filter here such as

articlesUnderLimit = allArticleTitles.filter((articleTitle) => articleTitle <= charLimit);

if (articleTitle.length <= charLimit) {
articlesUnderLimit.push(articleTitle);
}
}
return articlesUnderLimit;
}

/*
The editor of the FT likes short headlines with only a few words!
Implement the function below, which returns the title with the fewest words.
(you can assume words will always be seperated by a space)
(you can assume words will always be separated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
// TODO

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Remove the TODO comments once you've done the work, in production code TODO comments are often used to note places where further work still needs to be done so leaving it in can cause confusion

let shortestTitle = "";
let shortestSpaceCount = 0;
for (const title of allArticleTitles) {
if (shortestTitle === "") {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Why do we need to have a check for blank strings specifically?

shortestTitle = title;
shortestSpaceCount = title.split(" ").length - 1;
} else {
let currentSpaceCount = title.split(" ").length - 1;
if (currentSpaceCount < shortestSpaceCount) {
shortestTitle = title;
shortestSpaceCount = currentSpaceCount;
}
}
}
return shortestTitle;
}

/*
The editor of the FT has realised that headlines which have numbers in them get more clicks!
Implement the function below to return a new array containing all the headlines which contain a number.
(Hint: remember that you can also loop through the characters of a string if you need to)
*/

function containsNumbers(str) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Praise: Good separation of logic into a new function

return /[0-9]/.test(str);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: This works and is OK, however in the scope of work in this course I'd recommend avoiding regex's. Instead this could be done by using the string includes function.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes

}

function headlinesWithNumbers(allArticleTitles) {
// TODO
let numberArticles = [];
for (const title of allArticleTitles) {
if (containsNumbers(title)) {
numberArticles.push(title);
}
}
return numberArticles;
}

/*
The Financial Times wants to understand what the average number of characters in an article title is.
Implement the function below to return this number - rounded to the nearest integer.
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO
// TODO
let charCount = 0;
const articleTitleCount = allArticleTitles.length;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't generally need to store allArticlesTitles.length in a variable. Absolutely fine to write allArticleTitles.length inline.

for (const articleTitle of allArticleTitles) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (const articleTitle of allArticleTitles) {
for (const articleTitle of allArticleTitles.length) {

charCount += articleTitle.length;
}
const averageCharCount = Math.round(charCount / articleTitleCount);
return averageCharCount;
}



/* ======= List of Articles - DO NOT MODIFY ===== */
const ARTICLE_TITLES = [
"Streaming wars drive media groups to spend more than $100bn on new content",
"Amazon Prime Video India country head: streaming is driving a TV revolution",
"Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights",
"British companies look to muscle in on US retail investing boom",
"Libor to take firm step towards oblivion on New Year's Day",
"Audit profession unattractive to new recruits, says PwC boss",
"Chinese social media users blast Elon Musk over near miss in space",
"Companies raise over $12tn in 'blockbuster' year for global capital markets",
"The three questions that dominate investment",
"Brussels urges Chile's incoming president to endorse EU trade deal",
"Streaming wars drive media groups to spend more than $100bn on new content",
"Amazon Prime Video India country head: streaming is driving a TV revolution",
"Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights",
"British companies look to muscle in on US retail investing boom",
"Libor to take firm step towards oblivion on New Year's Day",
"Audit profession unattractive to new recruits, says PwC boss",
"Chinese social media users blast Elon Musk over near miss in space",
"Companies raise over $12tn in 'blockbuster' year for global capital markets",
"The three questions that dominate investment",
"Brussels urges Chile's incoming president to endorse EU trade deal",
];

/* ======= TESTS - DO NOT MODIFY ===== */

test("should only return potential headlines", () => {
expect(new Set(potentialHeadlines(ARTICLE_TITLES))).toEqual(new Set([
"British companies look to muscle in on US retail investing boom",
"Libor to take firm step towards oblivion on New Year's Day",
"Audit profession unattractive to new recruits, says PwC boss",
"The three questions that dominate investment"
]));
expect(new Set(potentialHeadlines(ARTICLE_TITLES))).toEqual(
new Set([
"British companies look to muscle in on US retail investing boom",
"Libor to take firm step towards oblivion on New Year's Day",
"Audit profession unattractive to new recruits, says PwC boss",
"The three questions that dominate investment",
])
);
});

test("should return an empty array for empty input", () => {
expect(potentialHeadlines([])).toEqual([]);
expect(potentialHeadlines([])).toEqual([]);
});

test("should return the title with the fewest words", () => {
expect(titleWithFewestWords(ARTICLE_TITLES)).toEqual("The three questions that dominate investment");
expect(titleWithFewestWords(ARTICLE_TITLES)).toEqual(
"The three questions that dominate investment"
);
});

test("should only return headlines containing numbers", () => {
expect(new Set(headlinesWithNumbers(ARTICLE_TITLES))).toEqual(new Set([
"Streaming wars drive media groups to spend more than $100bn on new content",
"Companies raise over $12tn in 'blockbuster' year for global capital markets"
]));
expect(new Set(headlinesWithNumbers(ARTICLE_TITLES))).toEqual(
new Set([
"Streaming wars drive media groups to spend more than $100bn on new content",
"Companies raise over $12tn in 'blockbuster' year for global capital markets",
])
);
});

test("should return the average number of characters in a headline", () => {
expect(averageNumberOfCharacters(ARTICLE_TITLES)).toEqual(65);
expect(averageNumberOfCharacters(ARTICLE_TITLES)).toEqual(65);
});
Loading