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

// a is not initialized

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

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

// The function does not have a return value

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

sayHelloToUser();

// The user in the function is not defined

// Example 4
let arr = [1,2,3];
console.log(arr[3]);
// There is no index 3 in arr variable
7 changes: 7 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,13 @@

function evenNumbers(n) {
// TODO
let str = "";
let i = 0;
while (i < n) {
str = str + i * 2 + ",";
i++;
}
console.log(str.slice(0,str.length-1));
}

evenNumbers(3); // should output 0,2,4
Expand Down
8 changes: 8 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,14 @@ const BIRTHDAYS = [

function findFirstJulyBDay(birthdays) {
// TODO
let i = 0
while (i < birthdays.length) {
if (birthdays[i].includes("July")) {
return birthdays[i];
}
i++;
}

}

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

function evenNumbersSum(n) {
// TODO
let sum= 0;
let i = 0;
do {
sum += i * 2;
i = i + 1;
} while (i < n);
return sum;
}

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

7 changes: 3 additions & 4 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) {
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.

3 changes: 3 additions & 0 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ 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
5 changes: 4 additions & 1 deletion 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ let tubeStations = [


// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
// let str = "codeyourfuture";
for (const station of tubeStations) {
console.log(station);
}
5 changes: 5 additions & 0 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

function getTemperatureReport(cities) {
// TODO
let arr = [];
for (let i = 0; i < cities.length; i++) {
arr.push("The temperature in " + cities[i] + " is " +temperatureService(cities[i]) +" degrees");
}
return arr;
}


Expand Down
5 changes: 5 additions & 0 deletions 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ function generateRandomNumber() {

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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
103 changes: 74 additions & 29 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let arr = [];
for (let i = 0; i < allArticleTitles.length; i++) {
if (allArticleTitles[i].length <= 65) {
arr.push(allArticleTitles[i]);
}
}
return arr;
// TODO
}

/*
Expand All @@ -14,68 +21,106 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let strLength = [];
for (let i = 0; i < allArticleTitles.length; i++) {
let strArr = allArticleTitles[i].split(" ");
strLength.push(strArr.length);
}
let minLength = Math.min(...strLength);
let minimumIndex = strLength.indexOf(minLength);
return allArticleTitles[minimumIndex];

// TODO
}

/*
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 isNum(c) {
if (typeof c !== "string") {
return true;
}
if (c.trim() === "") {
return false;
}
return !isNaN(c);
}
function headlinesWithNumbers(allArticleTitles) {
// TODO
let arrWithNum = [];
for (let i = 0; i < allArticleTitles.length; i++) {
let singleArt = allArticleTitles[i];
for (let j = 0; j < singleArt.length; j++) {
if (isNum(singleArt[j])) {
arrWithNum.push(singleArt);
break;
}
}
}
return arrWithNum;
// TODO
}

/*
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);
// TODO
}



/* ======= 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);
});
76 changes: 52 additions & 24 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"];

const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
[179.19, 180.33, 176.28, 175.64, 172.99], // AAPL
[340.69, 342.45, 334.69, 333.20, 327.29], // MSFT
[3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN
[2951.88, 2958.13, 2938.33, 2928.30, 2869.45], // GOOGL
[1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA
[179.19, 180.33, 176.28, 175.64, 172.99], // AAPL
[340.69, 342.45, 334.69, 333.2, 327.29], // MSFT
[3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN
[2951.88, 2958.13, 2938.33, 2928.3, 2869.45], // GOOGL
[1101.3, 1093.94, 1067.0, 1008.87, 938.53], // TSLA
];

/*
Expand All @@ -34,7 +34,17 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let avePrice = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
let stock = closingPricesForAllStocks[i];
let total = 0;
for (let j = 0; j < stock.length; j++) {
total += stock[j];
}
avePrice.push(parseFloat((total / stock.length).toFixed(2)));
}
return avePrice;
// TODO
}

/*
Expand All @@ -48,7 +58,14 @@ 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 changeInPrice = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
let stock = closingPricesForAllStocks[i];
let change = stock[stock.length - 1] - stock[0];
changeInPrice.push(parseFloat(change.toFixed(2)));
}
return changeInPrice;
// TODO
}

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


/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the average price for each stock", () => {
expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
[176.89, 335.66, 3405.66, 2929.22, 1041.93]
);
expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual([
176.89, 335.66, 3405.66, 2929.22, 1041.93,
]);
});

test("should return the price change for each stock", () => {
expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
[-6.2, -13.4, 23.9, -82.43, -162.77]
);
expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual([
-6.2, -13.4, 23.9, -82.43, -162.77,
]);
});

test("should return a description of the highest price for each stock", () => {
expect(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)).toEqual(
[
"The highest price of AAPL in the last 5 days was 180.33",
"The highest price of MSFT in the last 5 days was 342.45",
"The highest price of AMZN in the last 5 days was 3421.37",
"The highest price of GOOGL in the last 5 days was 2958.13",
"The highest price of TSLA in the last 5 days was 1101.30"
]
);
expect(
highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)
).toEqual([
"The highest price of AAPL in the last 5 days was 180.33",
"The highest price of MSFT in the last 5 days was 342.45",
"The highest price of AMZN in the last 5 days was 3421.37",
"The highest price of GOOGL in the last 5 days was 2958.13",
"The highest price of TSLA in the last 5 days was 1101.30",
]);
});
Loading