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
9 changes: 5 additions & 4 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

// Example 1
let a;
console.log(a);
console.log(a); // A: We haven't initialised the variable with a value.


// Example 2
Expand All @@ -20,17 +20,18 @@ function sayHello() {
}

let hello = sayHello();
console.log(hello);
console.log(hello); // A: We aren't returning anything from the function body and thus the default return is undefined.



// Example 3
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
console.log(`Hello ${user}`); // A: We aren't providing any arguments for the function call and thus user is undefined.
}

sayHelloToUser();


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
console.log(arr[3]); // A: arr[3] is the 4th item of the array which hasn't been assigned a value or the index 3 and thus it's undefined.
9 changes: 9 additions & 0 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@

function evenNumbers(n) {
// TODO
let i = 0, str = ``;
while (n > 0) {
(n === 1) ? str += `${i}` :
str += `${i},`;
i += 2;
n--;
}
console.log(str);
}


evenNumbers(3); // should output 0,2,4
evenNumbers(0); // should output nothing
evenNumbers(10); // should output 0,2,4,6,8,10,12,14,16,18
23 changes: 23 additions & 0 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,31 @@ const BIRTHDAYS = [
"November 15th"
];

// COUNTER VARIABLE
let i = 0;

// HELPER FUNCTION
function notInJuly (date) {

return date.search("July") === -1;

}

function findFirstJulyBDay(birthdays) {
// TODO

while (i < birthdays.length) {

if (notInJuly(birthdays[i])) {

i++;
continue;

}

return birthdays[i];
}
}


console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hi Tresor. BIRTHDAYS is not defined

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,6 +8,18 @@

function evenNumbersSum(n) {
// TODO

let arr = [], i = 0;
do {
if (i % 2 !== 0) {
i++;
continue;
} else {
arr.push(i);
i += 2;
}
} while (arr.length < n);
return arr.reduce((a, b) => a + b);
}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
12 changes: 7 additions & 5 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@


// Change the below code to use a for loop instead of a while loop.
let i = 0;
while(i < 26) {
console.log(String.fromCharCode(97 + i));
i++;
}


for (let i = 0; i < 26; i++) {
console.log(String.fromCharCode(97 + i));
}


// The output shouldn't change.
9 changes: 9 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,15 @@ const AGES = [

// TODO - Write for loop code here

function showWritersAge(name, age) {
for (let i = 0; i < name.length; i++) {
console.log(`${name[i]} is ${age[i]} years old.`);
}
return;
}

showWritersAge(WRITERS, AGES);

/*
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 i = 0; i < tubeStations.length; i++) {
console.log(tubeStations[i]);
}


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


for (let i = 0; i < str.length; i++) {
console.log(str[i].toUpperCase());
}
24 changes: 13 additions & 11 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.

Implement the function below:
- take the array of cities as a parameter
Expand All @@ -13,23 +13,25 @@

function getTemperatureReport(cities) {
// TODO
return cities.map((value, index) => `The temperature in ${value} is ${temperatureService(value)} degrees`);
}



/* ======= 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);
let temperatureMap = new Map();

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

return temparatureMap.get(city);
return temperatureMap.get(city);
}

test("should return a temperature report for the user's cities", () => {
Expand Down
7 changes: 7 additions & 0 deletions 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ function generateRandomNumber() {

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop

let num = 0;
do {
num = generateRandomNumber();
} while (num < 51);
return num;
}


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

test("Returned value should always be greater than 50", () => {
Expand Down
23 changes: 22 additions & 1 deletion 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,38 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO

return allArticleTitles.filter( value => value.length <= 65);
}

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

let shortestTitle = allArticleTitles[0];
allArticleTitles
.forEach((element, index) => {shortestTitle.length > element.length ? shortestTitle = element : 0;
});
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 isTitleWithNumber (element) {
return (element.match(/\d/gi) != null);
}

function headlinesWithNumbers(allArticleTitles) {
// TODO
return allArticleTitles.filter(isTitleWithNumber);
}

/*
Expand All @@ -32,6 +46,13 @@ function headlinesWithNumbers(allArticleTitles) {
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO

let amountOfTitles = 0, sumOfChar = 0;
for (let title of allArticleTitles) {
amountOfTitles++;
sumOfChar += title.length;
}
return Math.round(sumOfChar / amountOfTitles);
}


Expand Down
16 changes: 16 additions & 0 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO

return closingPricesForAllStocks.map((value, index) => {
let sum = 0, counter = 0;
for (let item of value) {
sum += item;
counter++;
}
return parseFloat((sum/counter).toFixed(2));
})
}

/*
Expand All @@ -49,6 +58,8 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO

return closingPricesForAllStocks.map(value => parseFloat((value[4] - value[0]).toFixed(2)));
}

/*
Expand All @@ -65,6 +76,11 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO

return closingPricesForAllStocks.map((value, index) => {
value.sort((a, b) => b-a);
return `The highest price of ${stocks[index].toUpperCase()} in the last 5 days was ${value[0].toFixed(2)}`;
})
}


Expand Down
6 changes: 6 additions & 0 deletions 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

function factorial(input) {
// TODO

let product = 1;
for (let i = input; i > 0; i -= 1) {
product *= i;
}
return product;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
16 changes: 16 additions & 0 deletions 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@

function getHighestRatedInEachGenre(books) {
// TODO

const genre = [];
books.forEach((book) => {
if (!genre.includes(book.genre)) {
genre.push(book.genre);
}
});
const bestBooksOfGenres = [];
genre.forEach((genre) =>
bestBooksOfGenres.push(
books
.filter((book) => book.genre === genre)
.sort((bookA, bookB) => bookB.rating - bookA.rating)[0].title
)
);
return bestBooksOfGenres;
}


Expand Down
9 changes: 9 additions & 0 deletions 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@

function generateFibonacciSequence(n) {
// TODO

const fibonSequence = [0, 1];
for (let i = n - 2; i > 0; i -= 1) {
fibonSequence.push(
fibonSequence[fibonSequence.length - 2] +
fibonSequence[fibonSequence.length - 1]
);
}
return fibonSequence;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ This is a **private** repository. Please request access from your Teachers, Budd

- Each of the *.js files in the `1-exercises` folder can be run from the terminal using the `node` command with the path to the file. For example, `node 1-exercises/A-undefined/exercise.js` can be run from the root of the project.
- To run the tests in the `2-mandatory` folder, run `npm run test` from the root of the project (after having run `npm install` once before).
- To run the tests in the `3-extra` folder, run `npm run extra-tests` from the root of the project (after having run `npm install` once before).

- To run the tests in the `3-extra` folder, run `npm run extra-tests` from the root of the project (after having run `npm install` once before)
## Instructions for submission

For your homework, we'll be using [**test driven development**](https://medium.com/@adityaalifnugraha/test-driven-development-tdd-in-a-nutshell-b9e05dfe8adb) to check your answers. Test driven development (or TDD) is the practice of writing tests for your code first, and then write your code to pass those tests. This is a very useful way of writing good quality code and is used in a lot of industries. You don't have to worry about knowing how this works, but if you're curious, engage with a volunteer to find out more! :)
Expand Down