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
7 changes: 5 additions & 2 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);

/* we didn't assign any value for our variable */

// Example 2
function sayHello() {
Expand All @@ -21,6 +21,7 @@ function sayHello() {

let hello = sayHello();
console.log(hello);
/* there is no parameter in our function */


// Example 3
Expand All @@ -29,8 +30,10 @@ function sayHelloToUser(user) {
}

sayHelloToUser();

/* we didn't give any value to our parameter in function */

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

/* there is no index number 3 in this array we have only 0,1,2 */
10 changes: 9 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,15 @@
*/

function evenNumbers(n) {
// TODO
let number = 0;
let arr = [];
while (n > 0) {
arr.push(number);
number += 2;
n--;
}
arr = arr.join(",");
console.log(arr);
}

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

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

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

function evenNumbersSum(n) {
// TODO
let number = 0;
let total = 0;
do {
total += number;
number += 2;
n--;
} while (n > 0);
return total;
}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
13 changes: 8 additions & 5 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) {
console.log(String.fromCharCode(97 + i));
i++;
}
// let i = 0;
// while(i < 26) {
// console.log(String.fromCharCode(97 + i));
// i++;
// }
// The output shouldn't change.
for (i = 0; i < 26; i++) {
console.log(String.fromCharCode(97 + i));
}
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 (i = 0; i <= 5; i++) {
console.log(`${WRITERS[i]} is ${AGES[i]} years old`);
}

/*
The output should look something like this:
Expand Down
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 el of tubeStations) {
console.log(el);
}

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

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

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


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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
40 changes: 39 additions & 1 deletion 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,36 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
return allArticleTitles.filter(items => items.length <= 65);
// let headlines = [];

// for(let title of allArticleTitles) {
// if(title.length <= 65) {
// headlines.push(title);
// }
// }

// 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
//TODO
let arr = [];
for (let i = 0; i < allArticleTitles.length; i++) {
arr.push(allArticleTitles[i].split(" ").length);
}
// return allArticleTitles[arr.indexOf(Math.min(...arr))];
const fewestWords = Math.min(...arr);
const indexOfArticle = arr.indexOf(fewestWords);
const articleTitle = allArticleTitles[indexOfArticle];
return articleTitle;

}

/*
Expand All @@ -24,6 +45,18 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
// let arrayNum = []
// for (let el of ARTICLE_TITLES) {
// for (let ele of el){
// if (ele.includes(Number)) {
// return arrayNum.push(ele.includes(Number));
// }
// }
// return arrayNum
// }
return allArticleTitles.filter((element) =>
[...element].find((number) => number >= "0" && number <= "9")
);
}

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


Expand Down
40 changes: 38 additions & 2 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 calculateAverage(arr) {
let sum = 0;
for (let price of arr) {
sum += price;
}
const average = sum / arr.length;
return average;
}


function getAveragePrices(closingPricesForAllStocks) {
// TODO
const myArr = [];
for (let stock of closingPricesForAllStocks) {
const average = calculateAverage(stock);
const formattedAverage = Number(average.toFixed(2));
myArr.push(formattedAverage);
}
return myArr;
}


/*
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 @@ -48,7 +65,15 @@ function getAveragePrices(closingPricesForAllStocks) {
The price change value should be rounded to 2 decimal places, and should be a number (not a string)
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO

let myArra = [];
for (let price of closingPricesForAllStocks) {
let firstDay = price[0];
Comment thread
ShayanMahnam marked this conversation as resolved.
let lastDay = price[price.length - 1];
let difference = lastDay - firstDay;
myArra.push(Number(difference.toFixed(2)));
}
return myArra;
}

/*
Expand All @@ -65,6 +90,17 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let arr = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
arr.push(
Comment thread
ShayanMahnam marked this conversation as resolved.
`The highest price of ${stocks[
i
].toUpperCase()} in the last 5 days was ${Math.max(
...closingPricesForAllStocks[i]
).toFixed(2)}`
);
}
return arr;
}


Expand Down
5 changes: 5 additions & 0 deletions 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

function factorial(input) {
// TODO
let sum = 1
for (let i = 1; i <= input; i++){
sum *= i
}
return sum
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
16 changes: 16 additions & 0 deletions 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@

function getHighestRatedInEachGenre(books) {
// TODO
const result = books.reduce((acc, cur) => {
Comment thread
ShayanMahnam marked this conversation as resolved.
const groupByGenre = cur.genre;
if (!acc[groupByGenre]) {
acc[groupByGenre] = [];
}
acc[groupByGenre].push(cur);
return acc;
}, {});
const genreName = Object.keys(result);
const arr = [];
for (let i = 0; i < genreName.length; i++) {
arr.push(
result[genreName[i]].sort((a, b) => b.rating - a.rating)[0].title
Comment thread
ShayanMahnam marked this conversation as resolved.
);
}
return arr;
}


Expand Down
5 changes: 5 additions & 0 deletions 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

function generateFibonacciSequence(n) {
// TODO
let fibo = [0, 1];
for (let i = 0; i < n - 2; i++) {
fibo.push(fibo[i] + fibo[i + 1]);
}
return fibo;
}

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