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

// Example 1
let a;
console.log(a);
console.log(a); //a is not defined


// Example 2
function sayHello() {
let message = "Hello";
let message = "Hello"; //the function doesn`t assigned to a value
}

let hello = sayHello();
Expand All @@ -26,11 +26,11 @@ console.log(hello);
// Example 3
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}
} //the functin doesn`t have return value

sayHelloToUser();


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
console.log(arr[3]); //there is no [3]index just 0,1,2
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
if (n > 0)
let x = "";
let i = 0;
while (i < n) {
x = x + "," + i * 2:
i++;
}
console.log(x.slice(1, x.length));
}

evenNumbers(3); // should output 0,2,4
Expand Down
14 changes: 11 additions & 3 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ const BIRTHDAYS = [
];

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

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
};
console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th
9 changes: 9 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,15 @@

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

console.log(evenNumbersSum(3)); // should output 6
Expand Down
11 changes: 7 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,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++) {
console.log(`${WRITERS[i]} is ${AGES[i]} years old`);
}

/*
The output should look something like this:
Expand Down
12 changes: 11 additions & 1 deletion 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@ let tubeStations = [
"Baker Street",
"Picadilly Circus",
"Oxford Street",
"Tottenham Court Road"
"Tottenham Court Road",
];
function outputStations(array) {
for (let i = 0; i < array.length; i++) {
console.log(`${array[i]}`);
}
}


// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
function outputStations(array) {
for (let i = 0; i < array.length; i++) {
console.log(`${array[i]}`);
}
}
9 changes: 9 additions & 0 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@

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


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 x = 0
do {
x = generateRandomNumber()
} while (x <= 50)
return x
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
14 changes: 14 additions & 0 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
return allArticleTitles.filter((article) => article.length <= 65)
}

/*
Expand All @@ -15,6 +16,12 @@ function potentialHeadlines(allArticleTitles) {
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let result = allArticleTitles[0]
for (let i = 0; i < allArticleTitles.length; i++) {
if (allArticleTitles[i].split(' ').length < result.split(' ').length)
result = allArticleTitles[i]
}
return result
}

/*
Expand All @@ -24,14 +31,21 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
return allArticleTitles.filter((article) => article.match(/(\d+)/))
}

/*
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.
*/
const reducer = (previousValue, currentValue) => previousValue + currentValue

function averageNumberOfCharacters(allArticleTitles) {
// TODO
return Math.round(
allArticleTitles.map((article) => article.length).reduce(reducer) /
allArticleTitles.length,
)
}


Expand Down
21 changes: 21 additions & 0 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ 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!
*/
const reducer = (previousValue, currentValue) => previousValue + currentValue

function getAveragePrices(closingPricesForAllStocks) {
// TODO
return closingPricesForAllStocks.map((priceList) =>
parseFloat(
(priceList.slice(priceList.length - 5).reduce(reducer) / 5).toFixed(2),
),
)
}

/*
Expand All @@ -49,6 +56,10 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
const result = closingPricesForAllStocks.map((priceList) =>
priceList.slice(priceList.length - 5),
)
return result.map((res) => parseFloat((res[4] - res[0]).toFixed(2)))
}

/*
Expand All @@ -65,6 +76,16 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
const result = closingPricesForAllStocks.map((priceList) =>
priceList.slice(priceList.length - 5),
)
return result.map(
(res, index) =>
'The highest price of ' +
stocks[index].toUpperCase() +
' in the last 5 days was ' +
Math.max(...res).toFixed(2),
)
}


Expand Down