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
9 changes: 4 additions & 5 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

// Example 1
let a;
console.log(a);
console.log(a); // Variable a is declared but no value is assigned to it


// Example 2
function sayHello() {
let message = "Hello";
let message = "Hello"; // there is no return statemnet in the function sayHello(), the variable message only exists inside of the function,but nothing is returned
}

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

sayHelloToUser();

sayHelloToUser(); // the parameter is not defined in the parenthesis so it returns undefined

// Example 4
let arr = [1,2,3];
console.log(arr[3]);
console.log(arr[3]); // the index 3 in the array call points to the 4th placed element inside the array, as we count the elements from index 0, index 3 is undefined

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 an excellent explanation and good use of comments.

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,9 +6,19 @@
*/

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

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
evenNumbers(1);
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;
let birthday;
while (i < birthdays.length) {
if (birthdays[i].includes("July")) {
birthday = birthdays[i];
i++
}
i++;
}
return birthday

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A well structured implementation of the function. However, the function will continue to search the presence of July up till the end even after it found the first July. The presence of i++ in line27 made the function not to give wrong output. Because it skips the next July in the array. If you try to input another July birthday after September 28th, you will see that the function will return the last July instead of the first. I suggest you end the while loop immediately you find the occurrence of July. It is also a good practice to put semi-colon after statements such as return in line 29.

}

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;
do { if (i % 2 === 0)
result += i;
i++;
}
while (i < n * 2)
return result;
}

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


// 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) {
// console.log(String.fromCharCode(97 + i));
// i++;
// }

for (let i = 0; i < 26; i++) {
console.log(String.fromCharCode(97 + i));
i++;
}

// The output shouldn't change.
4 changes: 4 additions & 0 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const AGES = [
49
];

for (let i = 0; i < 5; i++) {
console.log(`${WRITERS[i]} is ${AGES[i]} years old`)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Excellent!

// TODO - Write for loop code here

/*
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.toUpperCase());
}
9 changes: 6 additions & 3 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
*/

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good approach!

}
} return arr;
}


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

function temperatureService(city) {
Expand Down
16 changes: 15 additions & 1 deletion 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,22 @@ function generateRandomNumber() {
return Math.round(Math.random() * 100);
}

// function getRandomNumberGreaterThan50() {
// return generateRandomNumber() + 50;
// }

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
let i = 0;
let arr = [];
do {
arr.push(generateRandomNumber());
if (arr[i] > 50) {
arr = arr[i];
}
i++;
}
while (i <= arr.length);
return arr
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
38 changes: 34 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 (const element of allArticleTitles) {
if (element.length <= 65) {
arr.push(element);
}
}return arr;

}

/*
Expand All @@ -14,7 +20,15 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let arrLength = [];
let smallestNumValue;
let index;
for (const element of allArticleTitles) { //my first step was to itirate through the array and create an other array with indexes of lenght of each string
arrLength.push(element.split(" ").length);
}
smallestNumValue = Math.min(...arrLength); //once i had the array of indexes of lenght I went on to find the position of the smallest index in the array in 2 steps, first finding the value of the smallest number
index = arrLength.indexOf(smallestNumValue)//this is the second step of finding the position of the smallest index in an array, this steps finds the position of the smallest number that we identified in the previous step
return allArticleTitles[index]; //finally i return the position of the smallest number of the original array

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 approach is a smart one!

}

/*
Expand All @@ -23,19 +37,35 @@ 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 array = []
for (const element of allArticleTitles){
if ( /\d/.test(element)) {
array.push(element);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A very good use of regular expression!

} return array;
}

/*
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 arrNumOfCharacters = [];
let total = 0;
let average = 0;
for (const element of allArticleTitles) {
arrNumOfCharacters.push(element.trim().length);
}
for (const element of arrNumOfCharacters) {
total = total + element;
}

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 way you declare variables at the beginning is fantastic.

average = total / arrNumOfCharacters.length;
return Math.round(average);
}




/* ======= List of Articles - DO NOT MODIFY ===== */
const ARTICLE_TITLES = [
"Streaming wars drive media groups to spend more than $100bn on new content",
Expand Down
35 changes: 32 additions & 3 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let total = 0;
let average = 0;
arrayOfAverages = [];
for (const subArray of closingPricesForAllStocks) {
for (const element of subArray) {
total = total + element;
}
average = total / subArray.length;
total = 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.

I like the creative way of resetting the value of total.

arrayOfAverages.push(Math.round(average *100)/100);
}
return arrayOfAverages;
}

/*
Expand All @@ -48,7 +59,15 @@ 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 priceDifference = 0;
let arrayOfChanges = [];
for (const subArray of closingPricesForAllStocks) {

priceDifference = Math.round((subArray[subArray.length -1] - subArray[0]) * 100) /100;
arrayOfChanges.push(priceDifference);
}

return arrayOfChanges;
}

/*
Expand All @@ -64,7 +83,17 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let highestPriceEach = 0;
let arrayOfPrices = [];
let i = 0;
for (const subArray of closingPricesForAllStocks) {

highestPriceEach = Math.max(...subArray).toFixed(2);
arrayOfPrices.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPriceEach}`);
i++

}
return arrayOfPrices;
}


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

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

}
return sum;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
22 changes: 22 additions & 0 deletions 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,29 @@

function getHighestRatedInEachGenre(books) {
// TODO
const children = [];
const nonFiction = [];
const cooking = [];
const highestRatedTitles = [];
for (let i = 0; i < books.length; i++) {
if (books[i].genre === "children") {
children.push(books[i]);
} else if (books[i].genre === "non-fiction") {
nonFiction.push(books[i]);
} else {
cooking.push(books[i]);
}
}
children.sort((a, b) => b.rating - a.rating);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A beautiful way of using arrow function!

nonFiction.sort((a, b) => b.rating - a.rating);
cooking.sort((a, b) => b.rating - a.rating);
highestRatedTitles.push(children[0].title, nonFiction[0].title, cooking[0].title)
return highestRatedTitles;
}

// let numbers = [2, 5, 2, 5, 6, 8, 86,34, 0, 22];
// console.log(numbers.sort((a, b) => b - a))


/* ======= Book data - DO NOT MODIFY ===== */
const BOOKS = [
Expand Down Expand Up @@ -69,6 +90,7 @@ const BOOKS = [
},
]

// console.log(getHighestRatedInEachGenre(BOOKS))

/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the highest rated book in each genre", () => {
Expand Down
8 changes: 7 additions & 1 deletion 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
*/

function generateFibonacciSequence(n) {
// TODO
arr = [0];
num = 1;
for (let i = 0; i < n - 1; i++) {
arr.push(num);
num = num + arr[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.

Nice solution!

}
return arr;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
15 changes: 15 additions & 0 deletions testFolder/test 12.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function logicalCalc(array, op){
//your code here
let logic
// for (let i = 0; i < array.length; i++) {
if (op === "AND") {
logic = array.reduce((a, b) => a && b);
} else if (op === "OR") {
logic = array.reduce((a, b) => a || b);
} else if ( op === "XOR") {
logic = array.reduce((a, b) => a != b);
}
// }
return logic
}

13 changes: 13 additions & 0 deletions testFolder/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function evenNumbers(n) {
let i = 0;
let arr = [];
while (i < n * 2) {
if (i % 2 === 0) {
arr.push(i);
}
i++;
}
console.log(arr);
}

evenNumbers(10);
Loading