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
6 changes: 4 additions & 2 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// Example 1
let a;
console.log(a);
//here by console.log we can print a's value. (a) has declared but has not initialized so it will return undefined.


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

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

//because this function doesn't return message

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

sayHelloToUser();

//function call has no argument

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

function evenNumbers(n) {
// TODO
const even = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18];

let string = "";
let count = 0;
while (count < n) {
string += `${even[count]},`;
count++;
}

console.log(string);
}

evenNumbers(3); // should output 0,2,4
Expand Down
26 changes: 15 additions & 11 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
*/

const BIRTHDAYS = [
"January 7th",
"February 12th",
"April 3rd",
"April 5th",
"May 3rd",
"July 11th",
"July 17th",
"September 28th",
"November 15th"
"January 7th",
"February 12th",
"April 3rd",
"April 5th",
"May 3rd",
"July 11th",
"July 17th",
"September 28th",
"November 15th",
];

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

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

function evenNumbersSum(n) {
// TODO
const even = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18];

let string = "";
let count = 0;

do {
string += `${even[count]},`;
count++;
} while (count < n);

return string;
}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
6 changes: 2 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,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++;
for (let i = 0; i < 26; i++) {
console.log(String.fromCharCode(97 + i));
}
// The output shouldn't change.
27 changes: 11 additions & 16 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,24 @@
*/

const WRITERS = [
"Virginia Woolf",
"Zadie Smith",
"Jane Austen",
"Bell Hooks",
"Yukiko Motoya"
]

const AGES = [
59,
40,
41,
63,
49
"Virginia Woolf",
"Zadie Smith",
"Jane Austen",
"Bell Hooks",
"Yukiko Motoya",
];

// TODO - Write for loop code here
const AGES = [59, 40, 41, 63, 49];

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

/*
The output should look something like this:

Virginia Woolf is 59 years old
Zadie Smith is 40 years old
Jane Austen is 41 years old
Bell Hooks is 63 years old
Yukiko Motoya is 49 years old
*/
*/
17 changes: 12 additions & 5 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@

// TODO Use a for-of loop to output each of the tube stations below.
let tubeStations = [
"Aldgate",
"Baker Street",
"Picadilly Circus",
"Oxford Street",
"Tottenham Court Road"
"Aldgate",
"Baker Street",
"Picadilly Circus",
"Oxford Street",
"Tottenham Court Road",
];

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

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

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

function getTemperatureReport(cities) {
// TODO
let report = [];


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


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

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

do {
counter = generateRandomNumber();
} while ( counter<=50);
return counter;
}

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

test("Returned value should always be greater than 50", () => {
Expand Down
26 changes: 23 additions & 3 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
if (allArticleTitles.length !== 0 ){
allArticleTitles = ARTICLE_TITLES.filter((el) => el.length <=65)
return allArticleTitles;
}
else return (allArticleTitles=[]);
}

/*
Expand All @@ -14,7 +18,16 @@ 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++){
for (let j = i + 1 ; j < allArticleTitles.length;j++){
if (allArticleTitles [i].length> allArticleTitles[j].length){
let sahar = allArticleTitles [i];
allArticleTitles[i]=allArticleTitles[j];
allArticleTitles[j] = sahar ;
}
}
}
return allArticleTitles[0];
}

/*
Expand All @@ -24,14 +37,21 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
return allArticleTitles.filter((element) => {
return /\d/.test(element);
}

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

for (let i = 0; i < allArticleTitles.length; i++) {
sum = sum + allArticleTitles[i].length;
}
return Math.round(sum / allArticleTitles.length);
}


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

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:
- an array of stock tickers
- an array of arrays containing the closing price for each stock in each of the last 5 days.
Expand All @@ -26,15 +25,20 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
For example, the first element of the resulting array should contain Apple’s (aapl) average stock price for the last 5 days.
The second element should be Microsoft's (msft) average price, and so on.
The average value should be rounded to 2 decimal places, and should be a number (not a string)

Hint 1: To calculate the average of a set of values, you can add them together and divide by the number of values.
So the average of 5, 10 and 20 is (5 + 10 + 20) / 3 = 11.67
Hint 2: If the problem seems complex, try breaking it down into smaller problems.
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
return closingPricesForAllStocks.map(
(element) =>
+(
element.reduce((total, len) => total + len) / element.length
).toFixed(2)
);

}

/*
Expand All @@ -49,6 +53,8 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO

return closingPricesForAllStocks.map((element)=>+(element[4]-element[0]).toFixed(2));
}

/*
Expand All @@ -64,7 +70,15 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO

let highPrice=[];

stocks.forEach((stock,item) => {
highPrice.push(
`The highest price of ${stock.toUpperCase()} in the last 5 days was ${Math.max(...closingPricesForAllStocks[item]).toFixed(2)}`
);
});
return highPrice;
}


Expand All @@ -91,4 +105,4 @@ test("should return a description of the highest price for each stock", () => {
"The highest price of TSLA in the last 5 days was 1101.30"
]
);
});
});