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: 5 additions & 3 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// Example 1
let a;
console.log(a);
// does not have an assigned value


// Example 2
Expand All @@ -21,16 +22,17 @@ function sayHello() {

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

// a value was not returned

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

sayHelloToUser();

// there is no any parameter when sayHelloToUser() is called

// Example 4
let arr = [1,2,3];
let arr = [1, 2, 3];
console.log(arr[3]);
// ther is no value in the arr[3]
7 changes: 6 additions & 1 deletion 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
*/

function evenNumbers(n) {
// TODO
let i = 0;
while (i < n * 2) {
console.log(i)
i += 2
}
}


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
4 changes: 3 additions & 1 deletion 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const BIRTHDAYS = [
];

function findFirstJulyBDay(birthdays) {
// TODO
while (BIRTHDAYS.filter(item => item.match(/^July/g))) {
return BIRTHDAYS.find(item => item.includes('July'))
}
}

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

function evenNumbersSum(n) {
// TODO
let i = 0, j = 0, k = 0
if (n !== 0) {
do {
i += 2
k += i
j++
} while (j < n - 1)
return k
} else {
return 0
}
}

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


// 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: 3 additions & 1 deletion 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const AGES = [
];

// 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
7 changes: 6 additions & 1 deletion 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ let tubeStations = [
"Oxford Street",
"Tottenham Court Road"
];

for (let element of tubeStations) {
console.log(element)
}

// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
for (let s of str) {
console.log(s.toUpperCase())
}
10 changes: 7 additions & 3 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@
*/

function getTemperatureReport(cities) {
// TODO
let arr = []

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Just want to share an interesting thing, you can actually use const in here!
Using const makes the variable cannot be reassigned, but doesn't mean it cannot be modified. (with arr.push() in this example)

You can learn more in this short video: https://www.youtube.com/watch?v=RE6qf3As-XU

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Because the array and the object both are "By Reference", so they could be modified..

const arr = [1,2,3,4,5]
arr.push(2)
console.log(arr) //[ 1, 2, 3, 4, 5, 2 ]

const obj = {}
obj.text='hello'
console.log(obj) //{ text: 'hello' }

for (let i = 0; i < cities.length; i++) {
arr.push(`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`)
}
return arr
}


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

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

temparatureMap.set('London', 10);
temparatureMap.set('Paris', 12);
Expand All @@ -28,7 +32,7 @@ function temperatureService(city) {
temparatureMap.set('Mumbai', 29);
temparatureMap.set('São Paulo', 23);
temparatureMap.set('Lagos', 33);

return temparatureMap.get(city);
}

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

// 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
let n;
do {
n = generateRandomNumber();
} while (n < 50);
return n;
}

/* ======= 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);
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);
});
21 changes: 17 additions & 4 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
return allArticleTitles.filter(item => item.length < 65)
}

/*
Expand All @@ -14,7 +14,11 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let wordNums = []
for (let i = 0; i < allArticleTitles.length; i++) {
wordNums.push(allArticleTitles[i].split(' ').length)
}
return allArticleTitles[wordNums.indexOf(Math.min(...wordNums))]
}

/*
Expand All @@ -23,15 +27,24 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let NumberArray = []
const regex = /[0-9]/g;
for (let i = 0; i < allArticleTitles.length; i++) {
if (allArticleTitles[i].search(regex) >= 0) { NumberArray.push(allArticleTitles[i]) }
}
return NumberArray
}

/*
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
let sum = 0
for (let i = 0; i < allArticleTitles.length; i++) {
sum += allArticleTitles[i].length
}
return Math.round(sum / allArticleTitles.length)
}


Expand Down
26 changes: 23 additions & 3 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let arr = []
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
let sum = 0
for (let j = 0; j < closingPricesForAllStocks[i].length; j++) {
sum += closingPricesForAllStocks[i][j]
}
arr.push(Number((sum / closingPricesForAllStocks[i].length).toFixed(2)))
}
return arr
}

/*
Expand All @@ -48,7 +56,15 @@ function getAveragePrices(closingPricesForAllStocks) {
The price change value should be rounded to 2 decimal places, and should be a number (not a string)
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let arr = []
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
let fiftyDay = closingPricesForAllStocks[i][closingPricesForAllStocks[i].length - 1]
let firstDay = closingPricesForAllStocks[i][0]
let diffNum = 0
diffNum = fiftyDay - firstDay
arr.push(Number(diffNum.toFixed(2)))
}
return arr
}

/*
Expand All @@ -64,7 +80,11 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let arr = []
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
arr.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${(Math.max(...closingPricesForAllStocks[i])).toFixed(2)}`)
}
return arr
}


Expand Down
6 changes: 5 additions & 1 deletion 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
*/

function factorial(input) {
// TODO
let sum = 1
for (; input > 0; input--) {
sum *= input
}
return sum
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
Loading