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
8 changes: 8 additions & 0 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
let a;
console.log(a);

// We are seeing "undefined" for a here because even though we declared an "a" variable before but no value has been assigned to "a".


// Example 2
function sayHello() {
Expand All @@ -22,6 +24,8 @@ function sayHello() {
let hello = sayHello();
console.log(hello);

// We are seeing "undefined" here because there is no return value from hello which is a function.


// Example 3
function sayHelloToUser(user) {
Expand All @@ -30,7 +34,11 @@ function sayHelloToUser(user) {

sayHelloToUser();

// We are seeing "undefined" here because no argument is being passed into the function sayHelloToUser(user).

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

// We are seeing "undefined" here because there is no arr[3] in the array, the last item in the array is arr[2].

10 changes: 10 additions & 0 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

function evenNumbers(n) {
// TODO
let counter = 0;
let arr = [];
let num = 0;
while (n>counter) {
arr[counter] = num;
num = num+2;
counter++;
}

console.log (arr);
}

evenNumbers(3); // should output 0,2,4
Expand Down
9 changes: 9 additions & 0 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ const BIRTHDAYS = [

function findFirstJulyBDay(birthdays) {
// TODO
let counter = 0;
while (birthdays[counter].includes("July") === false) {
counter++;
}

return birthdays[counter];



}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
12 changes: 12 additions & 0 deletions 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@

function evenNumbersSum(n) {
// TODO
let sum = 0;
let num = 0;
let counter = 0;
do {
sum += num;
num += 2;
counter++;

} while (counter<n);

return sum;
}


Copy link

Choose a reason for hiding this comment

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

I think You need the loop to add up the first n even numbers,
starting at 0. So if n=3, you need to add up 0+2+4. If n=10 you
need to add up 0+2+4+6+8+10+12+14+16+18.

console.log(evenNumbersSum(3)); // should output 6
console.log(evenNumbersSum(0)); // should output 0
console.log(evenNumbersSum(10)); // should output 90
4 changes: 1 addition & 3 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@


// Change the below code to use a for loop instead of a while loop.
let i = 0;
while(i < 26) {
for ( let i = 0; i < 26; i++) {
console.log(String.fromCharCode(97 + i));
i++;
}
// The output shouldn't change.
4 changes: 4 additions & 0 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const AGES = [

// TODO - Write for loop code here

for (let counter = 0; counter < WRITERS.length; counter++) {
console.log (`${WRITERS[counter]} is ${AGES[counter]} years old`);
}

/*
The output should look something like this:

Expand Down
9 changes: 9 additions & 0 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ let tubeStations = [
"Tottenham Court Road"
];

for (let item of tubeStations) {
console.log(item);
}


// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";

for (let item of str) {
let converted = item.toUpperCase();
console.log(converted);
}
10 changes: 10 additions & 0 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,20 @@
- Hint: you can call the temperatureService function from your function
*/

cities1 = ['London', 'Paris','Barcelona','Dubai','Mumbai', 'São Paulo','Lagos'];

function getTemperatureReport(cities) {
// TODO
let arr = [];
for (let item of cities) {
let temp = temperatureService(item);
arr.push (`The temperature in ${item} is ${temp} degrees`);
}
return arr;
Comment on lines +18 to +23
Copy link

Choose a reason for hiding this comment

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

really nice solution & use of template literals :) could you think of renaming your arr variable to something more descriptive? For example let cityTemperatures

}

getTemperatureReport(cities1);


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

Expand Down
22 changes: 14 additions & 8 deletions 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@

// This function shouldn't be changed
function generateRandomNumber() {
console.log("Generating number...");
return Math.round(Math.random() * 100);
console.log("Generating number...");
return Math.round(Math.random() * 100);
}

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
// TODO - implement using a do-while loop
let result = 0;
do {
result = generateRandomNumber();
} while (result <= 50);

return result;
Copy link

Choose a reason for hiding this comment

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

really good use of a do-while loop, well done

}

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

test("Returned value should always be greater than 50", () => {
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
});
96 changes: 67 additions & 29 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let fitTitleArr = [];
if (allArticleTitles == null) {
return fitTitleArr;
}
for (let title of allArticleTitles) {
if (title.length <= 65) {
fitTitleArr.push(title);
}
}
return fitTitleArr;
// TODO
}

/*
Expand All @@ -14,7 +24,15 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let countArr = [];
for (let title of allArticleTitles) {
let titleArr = title.split(" ");
countArr.push(titleArr);
}
let result = countArr.sort((a, b) => a.length - b.length)[0];
return result.join(" ");

// TODO
}

/*
Expand All @@ -23,59 +41,79 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
// TODO
let numTitleArr = [];
let conditions = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];
for (let title of allArticleTitles) {
if (conditions.some((el) => title.includes(el))) {
Copy link

Choose a reason for hiding this comment

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

this works, but you could use regex so you don't have to type out an array of the conditions :) for example:

 if (/\d/.test(title)) {
      numTitleArr.push(title);
 }

\d is used to test for numbers present - this is a good site for playing around with regex https://regex101.com/

numTitleArr.push(title);
}
}
return numTitleArr;
}

/*
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 sum = 0;
let totalOfArticle = allArticleTitles.length;
for (let item of allArticleTitles) {
let strLength = item.length;
sum += strLength;
}
let average = sum / totalOfArticle;
return Math.round(average);
Copy link

Choose a reason for hiding this comment

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

nice solution!

}



/* ======= 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