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

//Because we didn't assign a value for a variable a when we first declaring it.

// Example 2
function sayHello() {
Expand All @@ -21,16 +21,19 @@ function sayHello() {

let hello = sayHello();
console.log(hello);
//Because the function is not returning anything.


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

sayHelloToUser();
//we didn't pass argument to the function when we are calling it.


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
//Because we don't have index 3.
18 changes: 14 additions & 4 deletions 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
// TODO
let arr = [];
let i = 0;
while (i % 2 === 0 && n > 0 && n > arr.length) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hello Rahwa
Regarding this function I believe you ought to have another look.
Your output should be one string with commas e.g. 0,2,4

See if you can redo this function with a 'string' in mind. You want to build a string from "" and add even numbers to this string until 'n' is reached.

arr.push(i);
i += 2;

}

return arr.toString(); // changes the numbers to strings

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

function evenNumbers(n) {
  let counter = 0;
  let evenNum = 0;
  while (counter < n) {
    console.log(evenNum);
    evenNum += 2
    counter++;
  }
}

The above prints every value on a different line. As an exercise let do this to print them as a comma separated string:

  1. Keep track of all the even numbers in the loop (hint: use array)
  2. After loop, console.log the array using the join: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

}

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

console.log(evenNumbers(3)); // should output 0,2,4
console.log(evenNumbers(0)); // should output nothing
console.log(evenNumbers(10)); // should output 0,2,4,6,8,10,12,14,16,18
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 @@ -18,6 +18,15 @@ const BIRTHDAYS = [

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

}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"


18 changes: 16 additions & 2 deletions 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,24 @@
Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0)
*/

function evenNumbersSum(n) {
function evenNumbersSum(num) {
// TODO
let i = 0;
let sum = 0;
let number = 0;

do{
sum = sum + number;
number = number + 2;
i = i + 1;


}
while(i < num);
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(evenNumbersSum(10)); // should output 90

5 changes: 5 additions & 0 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ while(i < 26) {
i++;
}
// The output shouldn't change.


for(let i = 0; i < 26; i++) {
console.log(String.fromCharCode(97 + i));
}
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 @@ -27,6 +27,10 @@ const AGES = [
];

// TODO - Write for loop code here
for(i=0; i < WRITERS.length; 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.

  1. let i=0 ...
  2. " years old"
  3. `${WRITERS[i]} is ${AGES[i]} years old`



/*
The output should look something like this:
Expand Down
9 changes: 8 additions & 1 deletion 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(tubes of tubeStations) {
console.log(tubes);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

let tube ...

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

let letter...


8 changes: 8 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
arrayOfStrings = [];
for(let i = 0; i < cities.length; i++) {
arrayOfStrings.push("The temperature in " + cities[i] + " is " + temperatureService(cities[i]) + " degrees");

}
return arrayOfStrings;
}



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

function temperatureService(city) {
Expand All @@ -32,6 +39,7 @@ function temperatureService(city) {
return temparatureMap.get(city);
}


test("should return a temperature report for the user's cities", () => {
let usersCities = [
"London",
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 @@ -7,10 +7,17 @@
function generateRandomNumber() {
console.log("Generating number...");
return Math.round(Math.random() * 100);

}

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

}
while(number <= 50)
return number;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: put a newline after do {

}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
50 changes: 45 additions & 5 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,73 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
// TODO
let newArrayTitle = [];
for (let i = 0; i < allArticleTitles.length; i++) {
if (allArticleTitles[i].length <= 65) {
newArrayTitle.push(allArticleTitles[i]);
}
}
return newArrayTitle;
}



/*
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
}
let newString = ''
for (let i = 0; i < allArticleTitles.length; i++) {
if (newString.length < 1 || newString.split(" ").length > allArticleTitles[i].split(" ").length) {
newString = allArticleTitles[i];
}

}

return newString;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cool solution ⭐

/*
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 newClicks = [];

for (let i = 0; i < allArticleTitles.length; i++) {
for (let letter of allArticleTitles[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.

Hi Rahwa, as we are discovering there are many different ways of doing the same thing in JavaScript.
See if you a Google a solution whereby you can determine whether a character is a number using 'two' comparisons only!
In future, that method, will save you some typing!
:) :)

if (letter === "0"||
letter === "1"||
letter === "2"||
letter === "3"||
letter === "4"||
letter === "5"||
letter === "6"||
letter === "7"||
letter === "8"||
letter === "9") {
newClicks.push(allArticleTitles[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.

I like that you've solved the problem correctly with the ways you knew 🥇

There is a function isNaN that can check if a string is not a number, which can help to do this step for you.


}
}
return newClicks;
}

/*
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 totalLength = 0;

for (let i = 0; i < allArticleTitles.length; i++) {
totalLength += allArticleTitles[i].length
}
return (Math.round(totalLength / allArticleTitles.length))
}


Expand Down
32 changes: 30 additions & 2 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let newAverage =[]
let sum = 0;
for (let i = 0; i < CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS.length; 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.

In the function, instead of using CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, we need to use the parameter closingPricesForAllStocks, so that we can call the function for different arrays of prices.

for (let j = 0; j < CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i].length; j++) {
sum += CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i][j];
}
newAverage.push(
(sum / CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i].length).toFixed(2) *

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

As an alternative multiply the number by 100, then Math.round() it; then divide by 100

That way, you don't have to convert a number to a string , then multiply by 1 to convert it to a number again

1
);

sum = 0;
}
return newAverage;

}

/*
Expand All @@ -48,9 +63,13 @@ 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
// TODO
let newPriceChange = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
newPriceChange.push((closingPricesForAllStocks[i][4] - closingPricesForAllStocks[i][0]).toFixed(2) * 1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

See my above comment regarding Math.round()

}
return newPriceChange;
}

/*
As part of a financial report, we want to see what the highest price was for each stock in the last 5 days.
Implement the below function, which
Expand All @@ -65,6 +84,15 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
highestPrice =[];
for(let i=0; i < closingPricesForAllStocks.length; i++) {
let highestNum = closingPricesForAllStocks[i].sort(function(a,b){

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!!

return b-a;} )[0];
highestPrice.push(
"The highest price of " + stocks[i].toUpperCase() + " " + "in the last 5 days was " + highestNum.toFixed(2));

}
return highestPrice;
}


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

function factorial(input) {
// TODO

for (let i = input - 1; i >= 1; i--) {
input= input*i;
console.log(input)
}
return input;

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 one! ⭐

}

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

function getHighestRatedInEachGenre(books) {
// TODO
let bookTitles=[];
for(let i=0; i<books.length; i++){
if(books[i].rating > 4.8){
bookTitles.push(books[i].title)
}

}
return bookTitles;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 It's great that you've solved the problem by adding the highest rating manually. I am so happy you started looking into the extra exercises too.

For finding the highest rating with the code (so that you can use the function with other types of books array and rating that 4.8 is not their highest rating), primary a for loop over books before what you wrote, can find the max rating first, then you can replace it with the 4.8 in your above code. For that, you can use a variable with the max rating of 0 (minimum rating) and then if you find a book with more ratings than that, you update the maximum rating till you visit all the book ratings.

}


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

function generateFibonacciSequence(n) {
// TODO

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


return fib;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💯

}


/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the first 10 numbers in the Fibonacci Sequence", () => {
expect(generateFibonacciSequence(10)).toEqual(
Expand Down