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,7 +12,7 @@
// Example 1
let a;
console.log(a);

// This is because no value is assigned to the variable "a". So attempting to print its value would print "undefined"

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

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

// The function "sayHello()" does not have any return value. Therefore, the value of the function when
// invoked would be "undefined", which is saved into hello.

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

sayHelloToUser();

// Here, we are only calling the function without entering the input to the function.

// Example 4
let arr = [1,2,3];
console.log(arr[3]);
// There is no element in index 3 of the array "arr". Out of bound indexing of the array leads to undefined value.
8 changes: 8 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,14 @@

function evenNumbers(n) {
// TODO
let result = "";
let i = 0
while(i < n){
result += `${2*i},`;
i++;
}
return result.slice(0, -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 ind = 0;
for (let i = 0; i < birthdays.length; i++){
if (birthdays[i].includes("July")){
ind = i;
break;
}
}
return birthdays[ind];
}

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

function evenNumbersSum(n) {
// TODO
let i = 0;
let evenArray = [];
let result = 0;
do {
evenArray.push(result);
result += 2;
i += 1;
} while (i < n);
let val = 0;
// console.log(evenArray);
for (let j=0; j<evenArray.length; j++){
val += evenArray[j];
}
return val;
}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
1 change: 1 addition & 0 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// 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++;
}
Expand Down
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 j = 0; j < WRITERS.length; j++){
console.log(`${WRITERS[j]} is ${AGES[j]} 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 (const station of tubeStations){
console.log(station);
}

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

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


Expand Down
7 changes: 7 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,13 @@ function generateRandomNumber() {

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

} while (val <= 50);
// return `We now have ${val}, which is greater than 50`;
return val;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
48 changes: 48 additions & 0 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let articleArr = [];
for (let i = 0; i < allArticleTitles.length; i++){
let title = allArticleTitles[i];
if(title.length <= 65){
articleArr.push(title);
}
}
return articleArr;
}

/*
Expand All @@ -15,15 +23,50 @@ function potentialHeadlines(allArticleTitles) {
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let titleLength = [];
for (let i = 0; i < allArticleTitles.length; i++){
let titleArray = allArticleTitles[i].split(" ");
titleLength.push(titleArray.length);
}
const minLength = Math.min(...titleLength);
const minIndex = titleLength.indexOf(minLength);
return allArticleTitles[minIndex];
}

/*
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 isNumber(char) {
if (typeof char !== 'string') {
return true;
}

if (char.trim() === '') {
return false;
}

return !isNaN(char);
}

function headlinesWithNumbers(allArticleTitles) {
// TODO

const arr = [];
for (let i = 0; i < allArticleTitles.length; i++) {
let article = allArticleTitles[i];
for (let j = 0; j < article.length; j++) {
let articleChar = article[j];
if (isNumber(articleChar)) {
arr.push(article);
break;
}
}
}
return arr;

}

/*
Expand All @@ -32,6 +75,11 @@ function headlinesWithNumbers(allArticleTitles) {
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO
let numChar = 0;
for (let i = 0; i < allArticleTitles.length; i++){
numChar += allArticleTitles[i].length;
}
return Math.round(numChar/allArticleTitles.length);
}


Expand Down
22 changes: 21 additions & 1 deletion 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
THESE EXERCISES ARE QUITE HARD. JUST DO YOUR BEST, AND COME WITH QUESTIONS IF YOU GET STUCK :)

Imagine we a working for a finance company. Below we have:
Imagine we are working for a finance company. Below we have:
- an array of stock tickers
- an array of arrays containing the closing price for each stock in each of the last 5 days.
For example, CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[2] contains the prices for the last 5 days for STOCKS[2] (which is amzn)
Expand Down Expand Up @@ -35,6 +35,16 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let result = [];
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];
}
result.push(parseFloat((total/stock.length).toFixed(2)));
}
return result;
}

/*
Expand All @@ -49,6 +59,12 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let result = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++){
let stock = closingPricesForAllStocks[i];
result.push(Math.round((stock[stock.length-1] - stock[0])*100)/100);
}
return result;
}

/*
Expand All @@ -65,6 +81,10 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
const sortedClosingPrices = closingPricesForAllStocks.map(prices => prices.sort((a, b) => b - a));
return sortedClosingPrices.map((value, index) => {
return `The highest price of ${stocks[index].toUpperCase()} in the last 5 days was ${(value[0].toFixed(2))}`;
})
}


Expand Down