Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
7 changes: 4 additions & 3 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
*/

// Example 1
let a;
let a = "ahmed"; // in this example we shuold add value to the variable.
console.log(a);


// Example 2
function sayHello() {
let message = "Hello";
return message; //in the examplewe need to write return to avoide the undefined.
}

let hello = sayHello();
Expand All @@ -28,9 +29,9 @@ function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}

sayHelloToUser();
sayHelloToUser("Omer"); // for this example we miss to add value when we call the function.


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
console.log(arr[3]);// because the arr has 3 item and the firest item start with 0 and 1 and 2 but there is no for 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.

I think you mean similar to me, //in this example we don't have arr[3], because we just have [0],[1].[2]and we don't have it.

7 changes: 7 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,13 @@

function evenNumbers(n) {
// TODO
let myArray = [];
let i = 0;
while (n > myArray.length) {
myArray.push(i);
i += 2;
}
console.log(myArray);
}

evenNumbers(3); // should output 0,2,4
Expand Down
9 changes: 8 additions & 1 deletion 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
Loops can be useful when working with arrays.
In the below example, imagine we've defined an array holding the birthdays of your closest friends.
Use a while loop to search through the array until you find the first birthday in July, then return that birthday from the function.
Use a while loop to search through the array until you find the first birthday in
July, then return that birthday from the function.
*/

const BIRTHDAYS = [
Expand All @@ -18,6 +19,12 @@ const BIRTHDAYS = [

function findFirstJulyBDay(birthdays) {
// TODO

let num = 0;
while (num < birthdays.length) {
if (birthdays[num].includes("July")) return birthdays[num];
num++;
}
}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
13 changes: 12 additions & 1 deletion 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@

function evenNumbersSum(n) {
// TODO
}
const ourArray = [];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey Danti, I can see the approach your going for here, but it looks like you forgot to include some code - this file doesn't run and references a missing function sumEvens

let i = 0;

do {
ourArray.push(i);
i++;
} while (i < 10);
return i;
}

console.log(sumEvens(ourArray));


console.log(evenNumbersSum(3)); // should output 6
console.log(evenNumbersSum(0)); // should output 0
Expand Down
7 changes: 4 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,10 @@


// Change the below code to use a for loop instead of a while loop.
let i = 0;
while(i < 26) {
// let i = 0;
// while(i < 26)
for (let i = 0; i < 26; i++) {
console.log(String.fromCharCode(97 + i));
i++;
// i++;
}
// The output shouldn't change.
6 changes: 6 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,12 @@ 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
6 changes: 6 additions & 0 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ let tubeStations = [
"Oxford Street",
"Tottenham Court Road"
];
for (i of tubeStations) {
console.log(i);
}


// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
for (i of str) {
console.log(i);
}
7 changes: 7 additions & 0 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@

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



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

function temperatureService(city) {
Expand Down
7 changes: 7 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,13 @@ function generateRandomNumber() {

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
let generatedNumber;

do {
generatedNumber = generateRandomNumber();
} while(generatedNumber <= 50);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The exercie says number greater than 50 not greater than or equal so please try to stick in with reqirements. I know as small issue but it will help you to stick to exact instructions, you do generatedNumber < 50 not this generatedNumber <= 50


return generatedNumber;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
53 changes: 53 additions & 0 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let headlines = [];

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

return headlines;
}

/*
Expand All @@ -15,6 +24,22 @@ function potentialHeadlines(allArticleTitles) {
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let fewestWordsSoFar;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Always a good idea to assign the number var to 0 to avoid random number being put into the variable by the js compiler
so make like this let fewestWordsSoFar = 0;

let titleWithFewestWords;

for(let title of allArticleTitles) {

// working out the number of words in the title by splitting on the space character
// this will generate an array. Read more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
let numWords = title.split(' ').length;

if(fewestWordsSoFar === undefined || numWords < fewestWordsSoFar) {
fewestWordsSoFar = numWords;
titleWithFewestWords = title;
}
}

return titleWithFewestWords;
}

/*
Expand All @@ -24,6 +49,27 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let articlesWithNumbers = [];

for(let title of allArticleTitles) {
// Making use of the new function created below
if(doesTitleContainANumber(title)) {
articlesWithNumbers.push(title);
}
}

return articlesWithNumbers;
}

// Creating another function to help break this problem down into smaller parts
function doesTitleContainANumber(title) {
for(let character of title) {
if(character >= '0' && character <= '9') {
return true;
}
}

return false;
}

/*
Expand All @@ -32,6 +78,13 @@ function headlinesWithNumbers(allArticleTitles) {
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO
let totalCharacters = 0;

for(let title of allArticleTitles) {
totalCharacters += title.length;
}

return Math.round(totalCharacters / allArticleTitles.length);
}


Expand Down
67 changes: 66 additions & 1 deletion 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let averages = [];

for(let pricesForStock of closingPricesForAllStocks) {
averages.push(getAveragePricesForStock(pricesForStock));
}

return averages;
}

function getAveragePricesForStock(pricesForStock) {
let total = 0;

for(let price of pricesForStock) {
total += price;
}

return roundTo2Decimals(total / pricesForStock.length);
}

function roundTo2Decimals(num) {
return Math.round(num * 100) / 100;
}

/*
Expand All @@ -48,7 +69,18 @@ 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
// TODOlet changes = [];

for(let pricesForStock of closingPricesForAllStocks) {
changes.push(getPriceChangeForStock(pricesForStock))
}

return changes;
}

function getPriceChangeForStock(pricesForStock) {
let priceChange = pricesForStock[pricesForStock.length - 1] - pricesForStock[0]
return roundTo2Decimals(priceChange);
}

/*
Expand All @@ -65,6 +97,39 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let descriptions = [];

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

return descriptions;
}

function getHighestPrice(pricesForStock) {
// initialising to 0, as we're expecting this value to be overriden by the first price in the array
let highestPriceSoFar = 0;

for(let price of pricesForStock) {
// if this price is higher than the highest price we've seen so far, it becomes the new highest price
if(price > highestPriceSoFar) {
highestPriceSoFar = price;
}
}

return highestPriceSoFar;
}

function highestPriceDescriptionsAlternate(closingPricesForAllStocks, stocks) {
let descriptions = [];

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

return descriptions;
}


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

function factorial(input) {
// TODO
let result = 1;

let i = input
do {
result *= i;
i--;
} while(i > 0);

return result;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
14 changes: 14 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,20 @@

function getHighestRatedInEachGenre(books) {
// TODO
let highestRated = {};

for(let book of books) {
// if this is the first time we're seeing this genre OR the rating we've seen is not as high as the current book
if(highestRated[book.genre] === undefined || highestRated[book.genre].rating < book.rating) {
// then this book is now the highest rated in the genre so far
highestRated[book.genre] = book;
}
}

// Here we just want to get the highest rated books (the values of the object)
// and then get the title for each one
return Object.values(highestRated)
.map(book => book.title);
}


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

function generateFibonacciSequence(n) {
// TODO
let fibonacciSequence = [];

// just initialise the first 2 values in the sequence
fibonacciSequence[0] = 0;
fibonacciSequence[1] = 1;

// the remaining numbers can be calculated
for (i = 2; i < n; i++) {
// each number is equal to the sum of the previous 2 numbers
fibonacciSequence[i] = fibonacciSequence[i - 2] + fibonacciSequence[i - 1];
}

return fibonacciSequence;
}

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