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: 6 additions & 2 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
// Example 1
let a;
console.log(a);

//In this example, we see undefined because our variable doesn't have any value.

// Example 2
function sayHello() {
let message = "Hello";
// In this example, We don't have return, so I would write return in this line. => return message;
}


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

Expand All @@ -29,8 +31,10 @@ function sayHelloToUser(user) {
}

sayHelloToUser();

//In this example,We do not have an argument because this is a function that has parameters, so outside the function it should have a value.
//I mean => sayHelloToUser(" we should write some string because od user")for example : sayHelloToUser("dear volunteer");

// Example 4
let arr = [1,2,3];
console.log(arr[3]);
//We have an array with the indexes o, 1, and 2 here, so there isn't index 3. we can Write arr[0] or arr[1] or arr [2]
10 changes: 8 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,15 @@
*/

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! I like this solution. Usually the standard way to look at even/odd number is using %2, but this is really nice too

}

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
11 changes: 10 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,16 @@ const BIRTHDAYS = [
];

function findFirstJulyBDay(birthdays) {
// TODO

let i = 0;

while( i <= birthdays.length ){
if (birthdays[i].includes("July")){
return birthdays[i]
}
i++
}

}

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

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

return result
}

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) {
console.log(String.fromCharCode(97 + i));
i++;
// 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.
5 changes: 4 additions & 1 deletion 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const AGES = [
];

// TODO - Write for loop code here

for(i = 0;i <= WRITERS.length;i++ ){
const result=`${WRITERS[i]} is ${AGES[i]} years old `
console.log(result)
}
/*
The output should look something like this:

Expand Down
7 changes: 7 additions & 0 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ let tubeStations = [
"Tottenham Court Road"
];

for(const element of tubeStations ){
console.log(element)
}


// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
for(const element of str){
console.log(element.toLocaleUpperCase())
}
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
let temperature=[];
// for(let i = 0;i < cities.length; i++){
for(const city of cities){
temperature.push(`The temperature in ${city} is ${temperatureService(city)} degrees`)
}
return temperature;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when you have an array that needs to be transformed, input and output have the same size, you can also use map

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}


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 @@ -10,7 +10,12 @@ function generateRandomNumber() {
}

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need this i ?

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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
32 changes: 28 additions & 4 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
let arr =[];
for (let article of allArticleTitles){

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could also use select

if (article.length <= 65){
arr.push(article)
}
}
return arr;
}

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could use the same logic here, as in here and store only the shortest title

arr.push(allArticleTitles[i].split(" ").length);
}
return allArticleTitles[arr.indexOf(Math.min(...arr))];
}

/*
Expand All @@ -23,15 +33,29 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let arr=[]
for (let article of allArticleTitles){

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use select function here, passing the condition to filter in your array

for (let char of article){
if (char>="0" && char<="9"){

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is nice! Another way to check it, would be to see if any character can be converted into an integer.
!!parseInt("a")

the double ! (bang) would return a boolean value

arr.push (article);
break;
}

}
}
return arr
}

/*
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 article of allArticleTitles){
sum+=article.length;
}
return Math.round(sum/allArticleTitles.length)
}


Expand Down
24 changes: 21 additions & 3 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let averageArr=[];
for (let closingPricesForStock of closingPricesForAllStocks){
let sum =0;
for (let item of closingPricesForStock){
sum=sum+item;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be a great opportunity to use a reduce function. where your accumulator is the sum

}
averageArr.push(Number((sum/closingPricesForStock.length).toFixed(2)));

}
return averageArr;
}

/*
Expand All @@ -48,7 +57,12 @@ 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 arr=[];
for (let closingPricesForStock of closingPricesForAllStocks){
arr.push(Number((closingPricesForStock[closingPricesForStock.length-1]-closingPricesForStock[0]).toFixed(2)));

}
return arr;
}

/*
Expand All @@ -64,7 +78,11 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let arr=[];
for (let i=0;i<closingPricesForAllStocks.length;i++){
arr.push (`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${(Math.max(...closingPricesForAllStocks[i])).toFixed(2)}`)
}
return arr;
}


Expand Down
6 changes: 5 additions & 1 deletion 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
*/

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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
17 changes: 16 additions & 1 deletion 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@
*/

function getHighestRatedInEachGenre(books) {
// TODO
const result = books.reduce((acc, cur) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be simplified.You did a really good job with :

      if (!acc[groupByGenre]) {
        acc[groupByGenre] = [];
      }

basically your accumulator is an empty object in the beginning, and you are registering the items grouping them by genre. But we may save some time if, instead of saving an array and then sorting it, we save only the highest value.
How do we know if a value is the highest?
Well, we don't know in the beginning, so we can push it the first rating in the first loop, and then on each cycle, we check if that value is higher than our previous value, and if so we override it!

if (cur.rating > acc[groupByGenre]){

acc[groupByGenre] = cur.rating
}

then our acc will look something like: acc={children: 10, fiction: 8, cooking: 9}

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
);
}
return arr;
}


Expand Down
6 changes: 5 additions & 1 deletion 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
*/

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

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