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

// undefined - because variable not assigned

// Example 2
function sayHello() {
let message = "Hello";
}
// undefined - because function not called

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Here we have defined a function, but we have not done anything with it yet


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

// undefined - because function not declared

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Here it is undefined because if we look back at line 19 it doesnt look like the function has returned anything yet.


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


sayHelloToUser();

// undefined - because we put nothing like an argument

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

// undefind - becouse array isn't conclude element with index 3

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Awesome!




13 changes: 11 additions & 2 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
The list of numbers should start with 0. n is being passed in as a parameter.
*/


function evenNumbers(n) {
// TODO
}
let result = [];

let i = 0;
while (i < n) {
result.push(i * 2);
i++;
}
console.log(result.join());
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Awesome! 1 Caveat would be does the question ask for the first n? If so do we we want to include n itself. If we do how would we update the code to do so.


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

function findFirstJulyBDay(birthdays) {
// TODO

let i = 0;
while ( i < BIRTHDAYS.length) {
if (BIRTHDAYS[i].includes('July')) {
return BIRTHDAYS[i];
}
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.

👌

}

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 sum = 0;

let i = 0;
do {
sum += (i * 2);
i ++;
} 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: 4 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,10 @@


// 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.
6 changes: 6 additions & 0 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
Using a for loop, output to the console a line about the age of each writer.
*/



const WRITERS = [
"Virginia Woolf",
"Zadie Smith",
Expand All @@ -26,6 +28,10 @@ const AGES = [
49
];

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

// TODO - Write for loop code here

/*
Expand Down
8 changes: 8 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,14 @@ let tubeStations = [
"Tottenham Court Road"
];

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


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

for (let letters of str) {
console.log(letters.toUpperCase());
}
8 changes: 8 additions & 0 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
*/

function getTemperatureReport(cities) {
let report = [];
for ( let city of cities) {
let temperature = temperatureService(city);
report.push(`The temperature in ${city} is ${temperature} degrees`);
}
return report
// TODO
}

Expand All @@ -32,6 +38,8 @@ function temperatureService(city) {
return temparatureMap.get(city);
}



test("should return a temperature report for the user's cities", () => {
let usersCities = [
"London",
Expand Down
5 changes: 4 additions & 1 deletion 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ function generateRandomNumber() {
}

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

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

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


function titleWithFewestWords(allArticleTitles) {
// TODO
let fewestWordSoFar;
let titleWithFewstWords;

for (let title of allArticleTitles) {
let numWords = title.split(' ').length;
if(fewestWordSoFar === undefined || numWords < fewestWordSoFar) {
fewestWordSoFar = numWords;
titleWithFewestWords = title;
}
}
return titleWithFewestWords;
}

/*
Expand All @@ -23,15 +41,33 @@ 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 articleWithNumber = [];
for (let title of allArticleTitles) {
if (doesTitleContainANumber(title)) {
articleWithNumber.push(title);
}
}
return articleWithNumber;
}

function doesTitleContainANumber(title) {
for (let charactor of title) {
if (charactor >='0' && charactor <= '9') {
return true;
}
}
return false;
}
/*
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 totalCaracters = 0;
for (let title of allArticleTitles) {
totalCaracters +=title.length;
}
return Math.round(totalCaracters / allArticleTitles.length)
}


Expand Down
17 changes: 16 additions & 1 deletion 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,24 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let averagePriceForEachStock = [];
for ( let priceForStock of CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS) {
averagePriceForEachStock.push(getAveragePricesForStock(pricesForStock));
}
return averagePriceForEachStock
}

function getAveragePricesForStock(pricesForStock) {
let total = 0;
for (let price of pricesForStock) {
total += price;
}
return roundTo2Decimals(total/pricesForStock.length);

function roundTo2Decimals (num) {
return Math.round(num*100) /100
}
}
/*
We also want to see what the change in price is from the first day to the last day for each stock.
Implement the below function, which
Expand Down