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

// The variable is not assigned to any value.

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

//function doesn't return a value
let hello = sayHello();
console.log(hello);

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

sayHelloToUser();

// when function called to argument has given.

// Example 4
let arr = [1,2,3];
console.log(arr[3]);
// there is only 0,1,2 index in this array only.
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
let i = 0;
let st = "";
while (i < n * 2 && i % 2 === 0){
st+=i

i+=2
}
console.log(st)
}

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

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

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

function evenNumbersSum(n) {
// TODO
let i = 0;
let sum = 0;
do {
if (i % 2 === 0) {
sum += i;
}
i++
}
while (i < n * 2);
return 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++;
for (let i = 0; i < 26 ; i++) {
console.log(String.fromCharCode(97 + i));
}
// let i = 0;
// while(i < 26) {
// 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
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 (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.toUpperCase());
}
5 changes: 5 additions & 0 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

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
22 changes: 18 additions & 4 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
return allArticleTitles.filter(title => title.length <= 65);
}

/*
Expand All @@ -14,7 +14,14 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let shortestTitle = allArticleTitles[0];

allArticleTitles.forEach(title => {
if (title.length < shortestTitle.length) {
shortestTitle = title
}
})
return shortestTitle
}

/*
Expand All @@ -23,15 +30,22 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
return allArticleTitles.filter(title => {
const numbersInTitle = title.match(/[1-9]/g)
return numbersInTitle != null ? true : false
})
}

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 use of the ES6 higher functions and arrow function as well as using RegEx .
Will done , Milad!


/*
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
const articleTitleLengths = allArticleTitles.map(title => title.length)

const sumOfTitleLengths = articleTitleLengths.reduce(function(partialSum, a) {return partialSum + a}, 0)

return Math.round(sumOfTitleLengths / allArticleTitles.length)
}


Expand Down
19 changes: 16 additions & 3 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
return closingPricesForAllStocks.map(stockPrices => {
const sumOfStockPrices = stockPrices.reduce(function(partialSum, a) {return partialSum + a}, 0)

return Number((sumOfStockPrices / stockPrices.length).toFixed(2));
})
}

/*
Expand All @@ -48,7 +52,9 @@ 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
return closingPricesForAllStocks.map(prices => {
return Number((prices[prices.length -1] - prices[0]).toFixed(2));
})
}

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

for (let i = 0; i < closingPricesForAllStocks.length; i++){
const maxPrice = Math.max(...closingPricesForAllStocks[i])
highestPrices.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${maxPrice.toFixed(2)}`)
}

return highestPrices
}


Expand Down
9 changes: 7 additions & 2 deletions 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@

function factorial(input) {
// TODO
}
let factorial = input;

/* ======= TESTS - DO NOT MODIFY ===== */
for (let i = input-1; i > 1; i--) {
factorial *= i;
}
return factorial;
}
// /* ======= TESTS - DO NOT MODIFY ===== */

test("3! should be 6", () => {
expect(factorial(3)).toEqual(6);
Expand Down