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: 7 additions & 0 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// Example 1
let a;
console.log(a);
// it doesn't have value.



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

let hello = sayHello();
console.log(hello);
//it doesn't return value



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


sayHelloToUser();
//it hasn't value for user parameter


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
//array hasn't this value
10 changes: 9 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,17 @@
*/

function evenNumbers(n) {
// TODO
let arr = [];
let i = 0;
while (i%2 === 0 && n > 0 && n > arr.length) {
arr.push(i);
i+=2;
}
return arr.toString();
}

console.log(evenNumbers(10));

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
10 changes: 7 additions & 3 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ const BIRTHDAYS = [
"September 28th",
"November 15th"
];

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"
console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
13 changes: 10 additions & 3 deletions 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0)
*/

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

return sum;
}

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


// 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++;
// }

for (let i = 0; i < 26; i++) {
console.log(String.fromCharCode(97 + i));
}
// The output shouldn't change.
3 changes: 3 additions & 0 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ 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
11 changes: 10 additions & 1 deletion 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@ let tubeStations = [
];


// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
for(let station of tubeStations){
console.log(station);
}

// TODO Use a for-of loop to capitalise and output each letter in the string separately.
let str = "codeyourfuture";


for (let letters of str){
console.log(letters.toUpperCase());
}
6 changes: 5 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
*/

function getTemperatureReport(cities) {
// TODO
let report=[]
for(let i = 0; i < cities.length; i++){
report[i]=`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`;

@Nomes27 Nomes27 Oct 2, 2022

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 use of template literals! here you can use push, for example report.push(The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees)

}
return report;
}


Expand Down
8 changes: 8 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,14 @@ function generateRandomNumber() {

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
let counter=0;
do{
counter=generateRandomNumber();
}
while(counter<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.

nice do-while loop implementation :) remember to use <= here rather than just < as the returned number needs to be greater than 50

return counter;


}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
40 changes: 35 additions & 5 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
if(allArticleTitles.lengh !== 0){
allArticleTitles = ARTICLE_TITLES.filter((el) => el.length <= 65);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

here you are using a mixture of allArticleTitles which is passed into the function and ARTICLE_TITLES (just use allArticlesTitles). Good use of the filter array method, but you don't need the if statement, for example if you do let shortTitles = allArticleTitles.filter((el) => el.length <= 65) and then return this, it will return an empty array if allArticleTitles.length is 0 :)

return allArticleTitles;

}
else return allArticleTitles=[];
}

/*
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 arr = allArticleTitles;
let newArr = arr.sort((a, b) => a.length - b.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.

good use of sort here, to sort an array without mutating the original array you can use the spread operator: let newArr = [...allArticleTitles].sort((a, b) => a.length - b.length);. We could use a different array name here to make the code more readable, for example changing newArr to something like fewestWordsArr

return newArr[0];

}

/*
Expand All @@ -23,19 +32,40 @@ 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 arr = [];

for (let moreClick of allArticleTitles) {
let choosen = "";
for (let letter of moreClick) {
if (!isNaN(letter) && letter !== " ") {
choosen = moreClick;
break;
}
}
if (choosen.length !== 0) {
arr.push(choosen);
}
}
return arr;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

regex is really useful for functions like this - this is a good site to learn more & have a play around https://regex101.com/
for the above you can do something like (/\d/.test(stringYouWantToTest)) the \d checks for any digit, returning true if a digit is found

}

/*
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 total = 0;
let average = 0;
for (let articleTitle of allArticleTitles) {
total += articleTitle.length;
}
average = total / allArticleTitles.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.

really nice solution well done, you could just define average on line 63, for example let average = total / allArticleTitles.length; and get rid of your definition on line 59 (as it's not really doing anything at the moment)

return Math.round(average);
// TODO
}



/* ======= List of Articles - DO NOT MODIFY ===== */
const ARTICLE_TITLES = [
"Streaming wars drive media groups to spend more than $100bn on new content",
Expand Down
32 changes: 31 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 newAverage =[]
let sum = 0;
for (let i = 0; i < CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS.length; i++) {
for (let j = 0; j < CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i].length; j++) {
Comment on lines +40 to +41

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 solution, well done on using multiple loops!

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) *
1
);

sum = 0;
}
return newAverage;

}

/*
Expand All @@ -48,7 +63,12 @@ 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);
}
return newPriceChange;
}

/*
Expand All @@ -65,9 +85,19 @@ 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){
return b-a;} )[0];
highestPrice.push(
"The highest price of " + stocks[i].toUpperCase() + " " + "in the last 5 days was " + highestNum.toFixed(2));

}
return highestPrice;

@Nomes27 Nomes27 Oct 2, 2022

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 :) The Math.max method is useful for finding the highest number, you can pass in an array like so:
let highestNum = Math.max(...closingPricesForAllStocks[i])

}



/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the average price for each stock", () => {
expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
Expand Down