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
6 changes: 6 additions & 0 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/*
By now, you would have already seen "undefined", either in an error message or being output from your program.
But what does it mean? undefined represents the absence of a value.
Expand All @@ -12,6 +13,7 @@
// Example 1
let a;
console.log(a);
//Answer: variable "a" has not been assigned to a value and therefore not defined


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

let hello = sayHello();
console.log(hello);
//Answer: a variable has been defined in the function but nothing is being returned from the function and it is therefore undefined.


// Example 3
Expand All @@ -29,8 +32,11 @@ function sayHelloToUser(user) {
}

sayHelloToUser();
//Answer: the function has been called without an argument for the user variable, therefore it returns "Hello, undefined".


// Example 4
let arr = [1,2,3];
console.log(arr[3]);

//Answer: there is no value in the array at position 3, as the count starts from
16 changes: 13 additions & 3 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
let seqN = []; // creating and empty array
let i = 0; // create a variable i and assigned 0
let count = 0; // create a variable count and assigned 0

while (count < n) { // created a while loop that specifies while argument is less than variable count, execute the loop
seqN.push(i); // pushing i as an element to the array seqN
i = i + 2; // we add 2 to variable i
count = count + 1; // we add 1 to count, which now reflects the amount of times the loop has been executed, counting from 0
}

console.log(seqN.join()); // seqN array is being joined together as a string and then console logged
}

evenNumbers(3); // should output 0,2,4
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
evenNumbers(10); // should output "0,2,4,6,8,10,12,14,16,18"
15 changes: 13 additions & 2 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
function findFirstJulyBDay(arrayOfBirthdays) {
let i = 0;
while (i < arrayOfBirthdays.length) {

if (arrayOfBirthdays[i].includes("July")) {
return arrayOfBirthdays[i]

}

i = i + 1;
}
}

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


12 changes: 11 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,17 @@
*/

function evenNumbersSum(n) {
// TODO
let result = 0;
let i = 0;

do {
i = i + 1;

if (i % 2 === 0) {

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 catch, keep it up

result = result + i;
}
} while (i <= n);
return result;
}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
16 changes: 11 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,15 @@


// 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))
}

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 @@ -37,3 +37,7 @@ Jane Austen is 41 years old
Bell Hooks is 63 years old
Yukiko Motoya is 49 years old
*/

for (let i = 0; i < 5; i++) {
console.log(`${WRITERS[i]} is ${AGES[i]} years old`)
}
8 changes: 8 additions & 0 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ let tubeStations = [
"Tottenham Court Road"
];

for (const element of tubeStations) {
console.log(element);
}


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

for (const letter of str) {
console.log(letter)
}
15 changes: 14 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,23 @@
- Hint: you can call the temperatureService function from your function
*/


// let citiesArr = ['London', 'São Paulo', 'Paris', 'Barecelona']

function getTemperatureReport(cities) {
// TODO
let cityWeather = [];
for (let i = 0; i < cities.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.

Perhaps linting could be applied here

cityWeather.push(
`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`
);
}
return cityWeather;
}

// console.log(getTemperatureReport('London'))




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

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

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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
26 changes: 24 additions & 2 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Imagine you are working on the Financial Times web site! They have a list of article titles stored in an array.

The home page of the web site has a headline section, which only has space for article titles which are 65 characters or less.
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
return allArticleTitles.filter(items => items.length < 65);
}

/*
Expand All @@ -15,6 +15,11 @@ function potentialHeadlines(allArticleTitles) {
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let arr = [];
for (let i = 0; i < allArticleTitles.length; i++) {
arr.push(allArticleTitles[i].split(" ").length);
}
return allArticleTitles[arr.indexOf(Math.min(...arr))];
}

/*
Expand All @@ -24,6 +29,18 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
// let arrayNum = []
// for (let el of ARTICLE_TITLES) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Removing the dead code would enhance the readability and accidental issues

// for (let ele of el){
// if (ele.includes(Number)) {
// return arrayNum.push(ele.includes(Number));
// }
// }
// return arrayNum
// }
return allArticleTitles.filter((element) =>
[...element].find((number) => number >= "0" && number <= "9")
);
}

/*
Expand All @@ -32,6 +49,11 @@ function headlinesWithNumbers(allArticleTitles) {
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO
let sum = 0;
for (let i = 0; i < allArticleTitles.length; i++) {
sum += allArticleTitles[i].length;
}
return Math.round(sum / allArticleTitles.length);
}


Expand Down Expand Up @@ -78,4 +100,4 @@ test("should only return headlines containing numbers", () => {

test("should return the average number of characters in a headline", () => {
expect(averageNumberOfCharacters(ARTICLE_TITLES)).toEqual(65);
});
});
47 changes: 44 additions & 3 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
Imagine we a working for a finance company. Below we have:
- an array of stock tickers
- an array of arrays containing the closing price for each stock in each of the last 5 days.
For example, CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[2] contains the prices for the last 5 days for STOCKS[2] (which is amzn)
For example, CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[2] contains the prices for the last 5 days for
STOCKS[2] (which is amzn)
*/

/* ======= Stock data - DO NOT MODIFY ===== */
Expand All @@ -23,7 +24,8 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Implement the below function, which
- Takes this CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS array as input (remember, it's an array of arrays)
- Returns an array containing the average price over the last 5 days for each stock.
For example, the first element of the resulting array should contain Apple’s (aapl) average stock price for the last 5 days.
For example, the first element of the resulting array should contain Apple’s (aapl) average stock
price for the last 5 days.
The second element should be Microsoft's (msft) average price, and so on.
The average value should be rounded to 2 decimal places, and should be a number (not a string)

Expand All @@ -33,9 +35,28 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Solve the smaller problems, and then build those solutions back up to solve the larger problem.
Functions can help with this!
*/

function calculateAverage(arr) {
let sum = 0;
for (let price of arr) {
sum += price;
}
const average = sum / arr.length;
return average;
}


function getAveragePrices(closingPricesForAllStocks) {
// TODO
const myArr = [];
for (let stock of closingPricesForAllStocks) {
const average = calculateAverage(stock);
const formattedAverage = Number(average.toFixed(2));
myArr.push(formattedAverage);
}
return myArr;
}
// TODO


/*
We also want to see what the change in price is from the first day to the last day for each stock.
Expand All @@ -49,6 +70,15 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let myArra = [];
for (let price of closingPricesForAllStocks) {
let firstDay = price[0];
let lastDay = price[price.length - 1];
let difference = lastDay - firstDay;
myArra.push(Number(difference.toFixed(2)));
}
return myArra;

}

/*
Expand All @@ -65,6 +95,17 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let arr = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
arr.push(
`The highest price of ${stocks[
i
].toUpperCase()} in the last 5 days was ${Math.max(
...closingPricesForAllStocks[i]
).toFixed(2)}`
);
}
return arr;
}


Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
"homepage": "https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week3#readme",
"devDependencies": {
"jest": "^26.6.3"
},
"dependencies": {
"mandatory": "^1.0.0"
}
}