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 @@ -11,26 +11,26 @@

// Example 1
let a;
console.log(a);
console.log(a); //a has not been defined, just declared


// Example 2
function sayHello() {
let message = "Hello";
let message = "Hello"; //nothing is being returned
}

let hello = sayHello();
console.log(hello);
console.log(hello); //it calls a function which doesn't return anything


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

sayHelloToUser();
sayHelloToUser(); //no parameter entered


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
let arr = [1, 2, 3];
console.log(arr[3]); //indexes start at 0, there is no 3rd index set
11 changes: 9 additions & 2 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
*/

function evenNumbers(n) {
// TODO
let i = 0;
let evens = [];
while (n > evens.length) {
evens[0] = 0;
i += 2;
evens.push(i);
}
console.log(`${evens}`);
}

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(10); // should output 0,2,4,6,8,10,12,14,16,18
8 changes: 7 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,13 @@ const BIRTHDAYS = [
];

function findFirstJulyBDay(birthdays) {
// TODO
let i = 0;
while (i < birthdays.length) {
i++
if (birthdays[i] === 'July 11th') {
return birthdays[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 @@ -7,7 +7,15 @@
*/

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

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


// 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++;
// }
for (let i = 0; i < 26; i++) {
console.log(String.fromCharCode(97 + i));
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,6 +27,9 @@ const AGES = [
];

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

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

let stations = '';
for (tubes of tubeStations) {
stations += tubes + ', ';
}
console.log(stations);

// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
let newStr = '';
for (letter of str) {
newStr += letter.toUpperCase();
}
console.log(newStr);
17 changes: 11 additions & 6 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@
For example, "The temperature in London is 10 degrees"
- Hint: you can call the temperatureService function from your function
*/

ct = ['London', 'Paris', 'Barcelona', 'Dubai', 'Mumbai', 'São Paulo', 'Lagos'];
function getTemperatureReport(cities) {
// TODO

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


console.log(getTemperatureReport(ct));
/* ======= 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 +33,7 @@ function temperatureService(city) {
temparatureMap.set('Mumbai', 29);
temparatureMap.set('São Paulo', 23);
temparatureMap.set('Lagos', 33);

return temparatureMap.get(city);
}

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

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

return randomNum;
}
console.log(getRandomNumberGreaterThan50());

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

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

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

/*
Expand All @@ -23,15 +33,27 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
withNums = []
for (let i = 0; i < allArticleTitles.length; i++) {
if (/\d/.test(allArticleTitles[i])) {
withNums.push(allArticleTitles[i]);
}
}
return withNums;
}


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


Expand All @@ -49,7 +71,10 @@ const ARTICLE_TITLES = [
"The three questions that dominate investment",
"Brussels urges Chile's incoming president to endorse EU trade deal",
];

// console.log(potentialHeadlines(ARTICLE_TITLES))
// console.log(titleWithFewestWords(ARTICLE_TITLES))
// console.log(headlinesWithNumbers(ARTICLE_TITLES))
//console.log(averageNumberOfCharacters(ARTICLE_TITLES))
/* ======= TESTS - DO NOT MODIFY ===== */

test("should only return potential headlines", () => {
Expand Down
41 changes: 35 additions & 6 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,27 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Solve the smaller problems, and then build those solutions back up to solve the larger problem.
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO

function getSum(arr) {
let sum = 0;
for (let i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}

function getAveragePrices(closingPricesForAllStocks) {
let sum = 0;
let average = 0
let result = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
sum = getSum(closingPricesForAllStocks[i])
average = sum / 5;
result.push(Math.round(average * 100) / 100);
}
return result;
}
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.
Implement the below function, which
Expand All @@ -47,10 +64,18 @@ function getAveragePrices(closingPricesForAllStocks) {
(Apple's price on the 5th day) - (Apple's price on the 1st day) = 172.99 - 179.19 = -6.2
The price change value should be rounded to 2 decimal places, and should be a number (not a string)
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
function findDifference(arr) {
return arr[4] - arr[0];
}

function getPriceChanges(closingPricesForAllStocks) {
let result = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
result.push(Number(findDifference(closingPricesForAllStocks[i]).toFixed(2)));
}
return result;
}
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.
Implement the below function, which
Expand All @@ -64,9 +89,13 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let result = []
for (let i = 0; i < closingPricesForAllStocks.length && i < stocks.length; i++) {
result.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${(Math.max(...closingPricesForAllStocks[i])).toFixed(2)}`)
}
return result;
}

highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS);

/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the average price for each stock", () => {
Expand Down
11 changes: 10 additions & 1 deletion 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@
*/

function factorial(input) {
// TODO
let result = 1;
if (input === 0 || input === 1) {
return input;
} else {
for (let i = input; i >= 1; i--) {
result = result * i;
}
}
return result;
}
console.log(factorial(3))

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

Expand Down
7 changes: 6 additions & 1 deletion 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
Implement a function which takes the array of books as a parameter, and returns an array of book titles.
Each title in the resulting array should be the highest rated book in its genre.
*/
//get books by genre
//search highest rated using ge
//return highest rated
function getGenre(books){

}
function getHighestRatedInEachGenre(books) {
// TODO

}


Expand Down
13 changes: 11 additions & 2 deletions 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@
*/

function generateFibonacciSequence(n) {
// TODO
}
if (n === 1) {
return [0, 1];
}
else {
let result = generateFibonacciSequence(n - 1);
result.push(result[result.length - 1] + result[result.length - 2]);
return result;
}
};
console.log(generateFibonacciSequence(10));
//I used google

/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the first 10 numbers in the Fibonacci Sequence", () => {
Expand Down