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
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.acceptSuggestionOnEnter": "on",
"editor.wordWrap": "on"
}
11 changes: 6 additions & 5 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
*/

// Example 1
let a;
let a; // a is not assigned to any value
console.log(a);


// Example 2
function sayHello() {
function sayHello() { // function sayHello() is not returning anything
let message = "Hello";
}

Expand All @@ -28,9 +27,11 @@ function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}

sayHelloToUser();
sayHelloToUser(); // function sayHelloToUser() is expecting a parameter but was never passed


// Example 4
let arr = [1,2,3];
let arr = [1,2,3]; // Index 3 is not defined in the array.
console.log(arr[3]);


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 @@ -4,11 +4,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.
*/

// n % 2 == 0
function evenNumbers(n) {
// TODO
let x = 1;
let y = 0;
let array = [];
while (x <= n) {
array.push(y);
x++;
y += 2;
}
console.log(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
11 changes: 11 additions & 0 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@ const BIRTHDAYS = [
"November 15th"
];



function findFirstJulyBDay(birthdays) {
// TODO
let count = 0;
let result = [];
while(count < birthdays.length){

Choose a reason for hiding this comment

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

I would recommend a FOR loop here, rather than a WHILE loop.

if(birthdays[count].includes("July")){
result.push(birthdays[count])
}
count ++;
}
return result[0];
}

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

function evenNumbersSum(n) {
// TODO
let sum = 0;
let x = 1;
let y = 0;
do {
sum = sum + y;
x++;
y += 2;
} while (x <= n)
return sum;
}




console.log(evenNumbersSum(3)); // should output 6
console.log(evenNumbersSum(0)); // should output 0
console.log(evenNumbersSum(10)); // should output 90
15 changes: 10 additions & 5 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) {
console.log(String.fromCharCode(97 + i));
i++;
}
// let i = 0;
// while(i < 26) {
// // console.log(String.fromCharCode(97 + i));
// i++;
// }
// The output shouldn't change.

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

}
12 changes: 12 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,18 @@ const AGES = [
];

// TODO - Write for loop code here
let len;
if (WRITERS.length === AGES.length) {
len = WRITERS.length;

} else {
return;
}

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


/*
The output should look something like this:
Expand Down
10 changes: 9 additions & 1 deletion 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ let tubeStations = [
"Oxford Street",
"Tottenham Court Road"
];

for (const 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 (const chr of str) {


console.log(chr.toUpperCase());
}

13 changes: 12 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@
*/

function getTemperatureReport(cities) {
// TODO
// TODO++
let strArr = []

for (let i = 0; i < cities.length; i++) {

// console.log(`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees` );
let textToDisplay = `The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`;
strArr.push(textToDisplay);


}
return strArr;
}


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

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
let result;
do {
result = generateRandomNumber();
} while (result <= 50);

Choose a reason for hiding this comment

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

This is just a matter of opinion, but woulnt it be more effiecient just writing:

Suggested change
} while (result <= 50);
while(result <= 50)
{
result = generateRandomNumber();
}

return result;


}


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

test("Returned value should always be greater than 50", () => {
Expand All @@ -22,3 +30,4 @@ test("Returned value should always be greater than 50", () => {
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50);
});

32 changes: 30 additions & 2 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let articleTitles = []
for (let i = 0; i < allArticleTitles.length; i++) {
if (allArticleTitles[i].length <= 65) {
articleTitles.push(allArticleTitles[i]);
}
}
return articleTitles;
}

/*
Expand All @@ -14,7 +21,14 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let shortestTitle = allArticleTitles[0];
for (let i = 0; i < allArticleTitles.length; i++) {
if (allArticleTitles[i].match(/\s+/g).length <
shortestTitle.match(/\s+/g).length) {
shortestTitle = allArticleTitles[i];
}
}
return shortestTitle;
}

/*
Expand All @@ -23,7 +37,13 @@ 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 titlesWithNum = [];
for (let i = 0; i < allArticleTitles.length; i++) {
if (/[0-9]/.test(allArticleTitles[i])) { // this reg exp checks numbers from 0 - 9
titlesWithNum.push(allArticleTitles[i]);
}
}
return titlesWithNum;
}

/*
Expand All @@ -32,6 +52,14 @@ function headlinesWithNumbers(allArticleTitles) {
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO
let average = 0;
let sum= 0;
for (let i = 0; i < allArticleTitles.length; i++) {
sum+= allArticleTitles[i].length;

}
average = Math.round(sum / allArticleTitles.length);
return average;
}


Expand Down
17 changes: 16 additions & 1 deletion 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 averageArray = [];
let averagePrice;

for (const array of closingPricesForAllStocks) {
let num = 0;
for (const number of array) {
num += number;
}

averagePrice = num / 5;
averageArray.push(parseFloat(averagePrice.toFixed(2)));
}
return averageArray;


}

/*
Expand Down Expand Up @@ -76,7 +91,7 @@ test("should return the average price for each stock", () => {
});

test("should return the price change for each stock", () => {
expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
[-6.2, -13.4, 23.9, -82.43, -162.77]
);
});
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
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 highestRated = {};
for(let book of books) {
if(highestRated[book.genre] === undefined || highestRated[book.genre].rating < book.rating) {
highestRated[book.genre] = book;
}
}
return Object.values(highestRated)
.map(book => book.title);
}


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

function generateFibonacciSequence(n) {
// TODO
let fibonacciSequence = [];
fibonacciSequence[0] = 0;
fibonacciSequence[1] = 1;
for (i = 2; i < n; i++) {
fibonacciSequence[i] = fibonacciSequence[i - 2] + fibonacciSequence[i - 1];
}

return fibonacciSequence;
}

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