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
12 changes: 6 additions & 6 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@
*/

// Example 1
let a;
let a; // a is not assigned any value
console.log(a);


// Example 2
function sayHello() {
let message = "Hello";
function sayHello() { // there is no return statement, and the message variable is defined inside the function so it is not read,
let message = "Hello"; // there are no parameters for the function
}

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


// Example 3
function sayHelloToUser(user) {
function sayHelloToUser(user) { // we don't pass any parameters into the function
console.log(`Hello ${user}`);
}

sayHelloToUser();


// Example 4
// Example 4 // there is nothing in the array at index 3
let arr = [1,2,3];
console.log(arr[3]);
16 changes: 12 additions & 4 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@
*/

function evenNumbers(n) {
// TODO
let i = 0;
let evenNum = '';
while (n>0) {
evenNum= evenNum + i + ',';
i+=2;
n--;
}
return evenNum;
}
console.log(evenNumbers(3))

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
// 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
7 changes: 6 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,12 @@ const BIRTHDAYS = [
];

function findFirstJulyBDay(birthdays) {
// TODO
let firstJulyBirthday= '';
for (let i=0; i<birthdays.length; i++){
if (birthdays[i] === '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.

This checks for the 11th of July only, is there a way we could use includes method on arrays to check for any day in July?

Here's the docs for includes https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

firstJulyBirthday = birthdays[i];
}
return firstJulyBirthday
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nice use for for loop here


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

function evenNumbersSum(n) {
// TODO
let evenNum=0;
let sum =0;
while (n>0) {
sum = sum+evenNum;
evenNum=evenNum+2;
n--;
}
return sum
}

console.log(evenNumbersSum(3)); // should output 6
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
12 changes: 8 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,13 @@


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

for (let i=0; i<26; i++){
console.log(String.fromCharCode(97 + i));
i++;
}
// The output shouldn't change.
11 changes: 10 additions & 1 deletion 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ const AGES = [
49
];

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




/*
The output should look something like this:
Expand Down
7 changes: 7 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,13 @@ let tubeStations = [
"Tottenham Court Road"
];

for (let i=0; i<tubeStations.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.

Line 14 and 21 are for loops but not for-of loops as they do not contain the keyword "of". Could you replace these for loops with for-of loops. Examples of for-of loops can be found on MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

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].toLocaleUpperCase())
}
7 changes: 6 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
*/

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


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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

For this function, we want to return when the number is greater than 50 only. Is it possible for 50 to be returned and if so, could you change this line to only return greater than 50 and not 50?

return number;
}

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


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

test("Returned value should always be greater than 50", () => {
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
Expand Down
46 changes: 40 additions & 6 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
The home page of the web site has a headline section, which only has space for article titles which are 65 characters or less.
Implement the function below, which will return a new array containing only article titles which will fit.
*/


function potentialHeadlines(allArticleTitles) {
// TODO
let newArr =[];
for (let i=0; i<allArticleTitles.length; i++){
if (allArticleTitles[i].length <= 65 ){
newArr.push(allArticleTitles[i])
}
}
return newArr;
}

/*
Expand All @@ -14,25 +22,51 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
}

let countLength = allArticleTitles[0].split(" ").length;
let shortTitle;

for (let i=0; i<allArticleTitles.length; i++){
if (countLength > allArticleTitles[i].split(" ").length) {
countLength = allArticleTitles[i].split(" ").length
console.log(allArticleTitles[i])
shortTitle = allArticleTitles[i]
}
}
return shortTitle
}


/*
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 headlinesWithNumbers(allArticleTitles) {
// TODO
let newArr=[]
for (let i=0; i<allArticleTitles.length; i++){
if (allArticleTitles[i].match(/[0-9]/g)){
newArr.push(allArticleTitles[i])
}
}
return newArr
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice use of regex here


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



Expand All @@ -50,7 +84,7 @@ const ARTICLE_TITLES = [
"Brussels urges Chile's incoming president to endorse EU trade deal",
];

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

test("should only return potential headlines", () => {
expect(new Set(potentialHeadlines(ARTICLE_TITLES))).toEqual(new Set([
Expand Down
31 changes: 27 additions & 4 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let average =[]
let stockSum =0;
for (let i=0; i<closingPricesForAllStocks.length; i++){
stockSum=0
for (let j=0; j<closingPricesForAllStocks[i].length; j++){

@teniolao teniolao Jan 21, 2022

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 names and readable code. great work Timea.

stockSum = stockSum + closingPricesForAllStocks[i][j];
}
average.push(Number((stockSum/closingPricesForAllStocks[i].length).toFixed(2)))
}
return average
}
//console.log(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS))

/*
We also want to see what the change in price is from the first day to the last day for each stock.
Expand All @@ -48,8 +58,16 @@ 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 priceChangeArr=[]

for (price of closingPricesForAllStocks) {
let priceChange = Number((price[price.length -1] - price[0]).toFixed(2));
priceChangeArr.push(priceChange);
}
return priceChangeArr;
}

// console.log(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS))

/*
As part of a financial report, we want to see what the highest price was for each stock in the last 5 days.
Expand All @@ -64,11 +82,16 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
highestPriceLast5Days = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
highestPriceLast5Days.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${Math.max(...closingPricesForAllStocks[i]).toFixed(2)}`);
}
return highestPriceLast5Days;
}
//console.log(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS,STOCKS))


/* ======= TESTS - DO NOT MODIFY ===== */
// /* ======= 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]
Expand Down