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
16 changes: 10 additions & 6 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,31 @@
*/

// Example 1
let a;
let a=1; // we dont have value for a
console.log(a);


// Example 2
function sayHello() {
function sayHello() {
let message = "Hello";
}
return message;
} //for this we should return message.

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


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

}

sayHelloToUser();
// here is we can give user value.
sayHelloToUser(user);


// Example 4
let arr = [1,2,3];
let arr = [1,2,3,4];
console.log(arr[3]);
//in this example we dont have arr[3],because we just have [0],[1].[2]and we dont have it.
21 changes: 15 additions & 6 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@

Using a while loop, complete the function below so it logs (using console.log) the first n even numbers as a comma-seperated string.
The list of numbers should start with 0. n is being passed in as a parameter.
*/

*/
function evenNumbers(n) {
// TODO
let i = 0;
let even =[];
while (i < n) {
if (i % 2 == 0) {
// TODO
even.push(i);
}
i++;
}
return even;
}

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
let result=evenNumbers(5);
console.log(result); // should output 0,2,4
console.log(evenNumbers(0)); // should output nothing.
console.log(evenNumbers(20)); // should output 0,2,4,6,8,10,12,14,16,18
18 changes: 14 additions & 4 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,19 @@ const BIRTHDAYS = [
"September 28th",
"November 15th"
];
let text = "";
let input = "";
function findFirstJulyBDay(birthdays,input)
{
for (let b of BIRTHDAYS)
{
if(b.includes(input))
{
return b;
}
}
return "Item was not found!"
} // TODO

function findFirstJulyBDay(birthdays) {
// TODO
}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
console.log(findFirstJulyBDay(BIRTHDAYS,"July")); // should output "July 11th"
18 changes: 13 additions & 5 deletions 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0)
*/

function evenNumbersSum(n) {
// TODO
function evenNumbersSum(num)
{
let sum = 0;
let i = 0;
do
if(i%2==0){
sum += i;
}while (++i < num); // TODO
console.log("************************");
return sum;
}

console.log(evenNumbersSum(3)); // should output 6
console.log(evenNumbersSum(0)); // should output 0
console.log(evenNumbersSum(10)); // should output 90
console.log("Result = ",evenNumbersSum(5)); // should output 6
console.log("Result = ",evenNumbersSum(0)); // should output 0
console.log("Result = ",evenNumbersSum(20)); // should output 90
6 changes: 3 additions & 3 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@


// Change the below code to use a for loop instead of a while loop.
let i = 0;
while(i < 26) {

for(let i = 0;i < 26; i++) {
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(i=0;i<WRITERS.length;i++){
console.log(`${WRITERS[i]} is ${AGES[i]} years old`);
}

/*
The output should look something like this:
Expand Down
18 changes: 12 additions & 6 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
A for-of loop is a easy and way of looping through the elements of an array, string or any other "iterable object" (think sequence of elements).
A for-of loop is an easy way of looping through the elements of an array, string or any other "iterable object" (think sequence of elements).
*/

// TODO Use a for-of loop to output each of the tube stations below.
Expand All @@ -9,8 +9,14 @@ let tubeStations = [
"Picadilly Circus",
"Oxford Street",
"Tottenham Court Road"
];


// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
];
for (let tubeStation of tubeStations) {
console.log(tubeStation);
console.log("**********************");
}
// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";

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

function getTemperatureReport(cities) {
// TODO
}
const temperatures = [];
for (let city of cities){
temperatures.push(
`The temperature in ${city} is ${temperatureService(city)} degrees`);// TODO
}

return temperatures;

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

function temperatureService(city) {
Expand Down
11 changes: 9 additions & 2 deletions 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ function generateRandomNumber() {
}

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
}

let number = 0;
do {
number = generateRandomNumber();

}
while (number<= 50); // TODO - implement using a do-while loop

return number;
}
/* ======= TESTS - DO NOT MODIFY ===== */

test("Returned value should always be greater than 50", () => {
Expand Down
36 changes: 29 additions & 7 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,57 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
}
let headLinesLessThanSixtyFiveArray = [];
for (let title of allArticleTitles) {
if (title.length <= 65) {
headLinesLessThanSixtyFiveArray.push(title);
}
}
return headLinesLessThanSixtyFiveArray;
}

/*
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
return allArticleTitles.filter(e => typeof e === 'string')
.sort((a, b) => a.length - b.length)[0];
}

/*
The editor of the FT has realised that headlines which have numbers in them get more clicks!
Implement the function below to return a new array containing all the headlines which contain a number.
(Hint: remember that you can also loop through the characters of a string if you need to)
*/

function headlinesWithNumbers(allArticleTitles) {
// TODO
let articlesWithNumbers = [];
for (let article of allArticleTitles) {
const replaced = article.replace(/\D/g, '');
if (replaced) {
articlesWithNumbers.push(article);
}
}
return articlesWithNumbers;
}


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


/* ======= List of Articles - DO NOT MODIFY ===== */
Expand Down
23 changes: 19 additions & 4 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
}
return closingPricesForAllStocks.map(
(item) => {
return +(item.reduce((b, d) => b + d, 0) / item.length).toFixed(2);

}
);
}
// TODO


/*
We also want to see what the change in price is from the first day to the last day for each stock.
Expand All @@ -49,6 +56,7 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
return closingPricesForAllStocks.map((x) => +(x[4] - x[0]).toFixed(2));
}

/*
Expand All @@ -64,8 +72,15 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
}
return closingPricesForAllStocks.map(
(item, a) =>
`The highest price of ${stocks[
a
].toUpperCase()} in the last 5 days was ${Math.max(...item).toFixed(2)}`
);
}
// TODO



/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
12 changes: 10 additions & 2 deletions 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@
Using a loop, complete the function below so it returns the factorial of the number being passed in.
*/

function factorial(input) {
function factorial(num) {
if ( num === 1)
return 1;
for (var i = num - 1; i >= 1; i--) {
num *= i;
}
return num;
}
factorial(5);
// TODO
}


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

Expand Down
17 changes: 14 additions & 3 deletions 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,21 @@
Implement a function which takes the array of books as a parameter, and returns an array of book titles.
Each title in the resulting array should be the highest rated book in its genre.
*/
let highest=[];
let books = 0;
function getHighestRatedInEachGenre(books,generate) {
let highest=[];
let books = 0;
for(let highest of books ){
if (highest<rate)
highest=rate;
};
return highest;// TODO
}

getHighestRatedInEachGenre(books);
// TODO

function getHighestRatedInEachGenre(books) {
// TODO
}


/* ======= Book data - DO NOT MODIFY ===== */
Expand Down