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
24 changes: 20 additions & 4 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,41 @@
// 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.
//we can change Example 1 like below :
a=10;
console.log(a);


// Example 2
function sayHello() {
let message = "Hello";
}

}//because this function doesn't return message so it shows undefined.
let hello = sayHello();
console.log(hello); // this will print a variable which is hold the value of sayHello funcation call but cuz the
//function doesn't return message , so it again shows undefined. but if the function fix and return message
//these lines will be show "Hello". we can fix it as bellow.

function sayHello(){
let message="Hello";
return message;
}
hello=sayHello();
console.log(hello);



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

sayHelloToUser();
sayHelloToUser();// in this example sayHelloToUser function has a parameter so because an argument doesn't pass to function call it will show "Hello undefined".
//we can fix it as bellow :
sayHelloToUser("Leila");//it will show "Hello Leila"


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
console.log(arr[3]);//this array has 3 element and include : 0 1 2 indexes so index 3 doesnt exist in this array
//and will show undefined
12 changes: 11 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,17 @@
*/

function evenNumbers(n) {
// TODO
let counter = 0;
let show = "";
while (counter < n * 2) {
if (counter % 2 == 0) {
show = show + counter + ",";
}

counter++
}
console.log(show);

}

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

function findFirstJulyBDay(birthdays) {
// TODO
let counter = 5;
while (counter < BIRTHDAYS.length) {
return birthdays[counter];
}
counter++;
}

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

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

console.log(sum);
}

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.
6 changes: 5 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,11 @@ const AGES = [
];

// TODO - Write for loop code here

for(let i=0;i<WRITERS.length;i++){

console.log(`${WRITERS[i]} is ${AGES[i]} 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 (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
6 changes: 6 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,12 @@ 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 ===== */
Expand Down
24 changes: 24 additions & 0 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
if (allArticleTitles.length !== 0) {
allArticleTitles = ARTICLE_TITLES.filter((el) => el.length <= 65);
return allArticleTitles;
} else return (allArticleTitles = []);
}

/*
Expand All @@ -15,6 +19,17 @@ function potentialHeadlines(allArticleTitles) {
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let size = allArticleTitles.length;
for (let i = 0; i < size; i++) {
for (let j = i + 1; j < size; j++) {
if (allArticleTitles[i].length > allArticleTitles[j].length) {
let temp = allArticleTitles[i];
allArticleTitles[i] = allArticleTitles[j];
allArticleTitles[j] = temp;
}
}
}
return allArticleTitles[0];
}

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

/*
Expand All @@ -32,6 +50,12 @@ function headlinesWithNumbers(allArticleTitles) {
*/
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
20 changes: 18 additions & 2 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
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 +55,8 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO

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

/*
Expand All @@ -64,7 +72,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 Down
8 changes: 8 additions & 0 deletions 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@

function factorial(input) {
// TODO
if (input === 0 || input === 1) {
return 1;
} else {
for (let i = input - 1; i >= 1; i--) {
input = input * i;
}
return input;
}
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
10 changes: 9 additions & 1 deletion 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@
*/

function generateFibonacciSequence(n) {
// TODO
let fib = [0, 1];
let data = [];

for (let i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
data.push(fib[i]);
}

return fib;
}

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