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: 6 additions & 3 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,29 @@

// Example 1
let a;
console.log(a);
console.log(a);
// there's no values assigned to the variable.


// Example 2
function sayHello() {
let message = "Hello";
}
// there's no output.

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

// because the function sayhello does not return anything so this is not defined.

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

sayHelloToUser();

// no argument.

// Example 4
let arr = [1,2,3];
console.log(arr[3]);
// there's no 4th(arr index 3) element in the arr.
8 changes: 7 additions & 1 deletion 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
*/

function evenNumbers(n) {
// TODO
let result = [];
let i = 0;
while(i < n) {
result.push(i * 2);
i++;
}
console.log(result.join());
}

evenNumbers(3); // should output 0,2,4
Expand Down
8 changes: 7 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,13 @@ 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"
11 changes: 10 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,16 @@
*/

function evenNumbersSum(n) {
// TODO
let sum = 0;
let i = 0;
do {
sum += (i * 2);
i++;
} while (i < n);

return sum;


}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
3 changes: 1 addition & 2 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

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

for (let station of tubeStations) {
console.log(station);
}

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

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

}
return report;
}



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

function temperatureService(city) {
Expand Down
7 changes: 6 additions & 1 deletion 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ function generateRandomNumber() {
}

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

} while (generatedNumber <= 50);
return generatedNumber;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
24 changes: 21 additions & 3 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 headlines = [];
for (let title of allArticleTitles) {
if (title.length <= 65)
headlines.push(title)

}
return headlines;
}

/*
Expand All @@ -23,15 +29,27 @@ 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 articleWithNumbers = [];

for ( let title of allArticleTitles) {
if (character >= "0" && character <= "9")
articleWithNumbers.push(title)
}

return articleWithNumbers;
}

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

for (let title of allArticleTitles) {
totalCharacters += title.length;
}
return Math.round(totalCharacters / allArticleTitles.length);
}


Expand Down
17 changes: 16 additions & 1 deletion 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,23 @@
Each title in the resulting array should be the highest rated book in its genre.
*/

// From the solution :
function getHighestRatedInEachGenre(books) {
// TODO
// this will be an object where the property will be a genre, and the value will be an object representing the book
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