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

/* a is not defined */

// Example 2
function sayHello() {
let message = "Hello";
}

/* there is no return */
let hello = sayHello();
console.log(hello);

/* hello is a variable not a function */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In javascript, functions can be assigned to variables e.g.

const hello = sayHello
hello()

So the reason that it is undefined is because you assign hello to the result of calling sayHello(); as sayHello() doesn't return anything, hello is undefined.


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

sayHelloToUser();

/*needs a parameter*/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

technically, it's an argument (parameter is user, argument is the value assigned to user when invoking a function)


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
/* there is no index 3 */
7 changes: 7 additions & 0 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@

function evenNumbers(n) {
// TODO
i = 0;
let even = [];
while (i < 2 * n){
even.push(i);}
i += 2
Comment on lines +12 to +14

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Think your syntax is off here; you're incrementing i after the while loop is done.

Also, for checking even numbers, the modulo % operator is what you're looking for.

return even.toString()
}
console.log(evenNumbers(3));

evenNumbers(3); // should output 0,2,4
evenNumbers(0); // should output nothing
Expand Down
7 changes: 6 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,12 @@ const BIRTHDAYS = [
];

function findFirstJulyBDay(birthdays) {
// TODO
let i=0;
while(!(birthdays[i].includes("July"))){

i++;
}
return birthdays[i]; // TODO
}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
10 changes: 9 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,15 @@
*/

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

return (sumEven);// TODO
}

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


// Change the below code to use a for loop instead of a while loop.
for ( let i = 0; i < 26; i++ ){
console.log(String.fromCharCode(97 + i));
}

let i = 0;
while(i < 26) {
console.log(String.fromCharCode(97 + i));
Expand Down
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-1;i++){

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

as you're starting from 0 and just using a < instead of <=, you just need to check if i<WRITERS.length instead of .length-1

console.log ( WRITERS[i]+" is "+AGES[i]+" years old")
}
/*
The output should look something like this:

Expand Down
6 changes: 5 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,11 @@ let tubeStations = [
"Oxford Street",
"Tottenham Court Road"
];

for (let tubeStation of tubeStations){
console.log (tubeStation.toUpperCase())
}

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

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



/* ======= TESTS - DO NOT MODIFY ===== */
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 @@ -11,6 +11,13 @@ function generateRandomNumber() {

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop

let num = 0;
do {
num = generateRandomNumber();
} while (num < 50);

return num;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
42 changes: 41 additions & 1 deletion 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let newArr = [];
for (let title of allArticleTitles) {
if (title.length < 65) newArr.push(title);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just to keep things readable, I'd advise always using {} with if statements even if they're 1 line:

if(title.length < 65) {
   newArr.push(title)
}

}
return newArr;

}

/*
Expand All @@ -15,6 +20,25 @@ function potentialHeadlines(allArticleTitles) {
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
for (var i = 0; i < allArticleTitles.length; i++) {
// Last i elements are already in place
for (var j = 0; j < allArticleTitles.length - i - 1; j++) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

similar to above, you don't need to -1 here

// Checking if the item at present iteration
// is greater than the next iteration
if (
allArticleTitles[j].split(" ").length >
allArticleTitles[j + 1].split(" ").length
) {
// If the condition is true then swap them
var temp = allArticleTitles[j];
allArticleTitles[j] = allArticleTitles[j + 1];
allArticleTitles[j + 1] = temp;
}
}
}

return allArticleTitles[0];

}

/*
Expand All @@ -24,6 +48,15 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let newArr = [];
for (let title of allArticleTitles) {
if (/[0-9]/.test(title) === true) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

you could simplify this a little with a shorthand character class in regex /\d/.test)

newArr.push(title);
}
}

return newArr;

}

/*
Expand All @@ -32,6 +65,13 @@ function headlinesWithNumbers(allArticleTitles) {
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO
let sum = 0;

for (let title of allArticleTitles) {
sum += title.length;
}

return Math.round(sum / allArticleTitles.length);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what if articleTitle.length is 0?

}


Expand Down
42 changes: 38 additions & 4 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,24 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
}
let arrayWithAveragePrices = [];
//looping through array of arrays and calling findAveragePrice method on each array
for (let arrayWithPrices of closingPricesForAllStocks) {
arrayWithAveragePrices.push(findAveragePrice(arrayWithPrices));
}
return arrayWithAveragePrices;
}

//function to find average price in array
function findAveragePrice(array) {
let sum = 0;
for (let price of array) {
sum += price;
}
return Number((sum / array.length).toFixed(2));
}



/*
We also want to see what the change in price is from the first day to the last day for each stock.
Expand All @@ -48,7 +64,14 @@ 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 changedPrices = [];
for (let arrayWithPrices of closingPricesForAllStocks) {
let changedPrice =
arrayWithPrices[arrayWithPrices.length - 1] - arrayWithPrices[0];
changedPrices.push(Number(changedPrice.toFixed(2)));
}
return changedPrices;

}

/*
Expand All @@ -64,7 +87,18 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let arrayWithStrings = []; //creating new array

// looping through array with prices
for (let i = 0; i < stocks.length; i++) {
let stockName = stocks[i].toUpperCase(); //capitalising the name of stock
let biggestPrice = Math.max(...closingPricesForAllStocks[i]); //getting the max value in array
let convertedPrice = biggestPrice.toFixed(2); //making 2 decimals and leaving it as a String
arrayWithStrings.push(
`The highest price of ${stockName} in the last 5 days was ${convertedPrice}`
); //adding a String to a new array
}
return arrayWithStrings;
}


Expand Down