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
8 changes: 4 additions & 4 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
*/

// Example 1
let a;
let a; // The a variable has declared and need to be assigned to a value. let a = 12; or let a = "Hello";
console.log(a);


// Example 2
function sayHello() {
let message = "Hello";
let message = "Hello"; // The function doesn't have return, therefore it is undefined.
}

let hello = sayHello();
Expand All @@ -28,9 +28,9 @@ function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}

sayHelloToUser();
sayHelloToUser(); // There is no value for the parameter, user is undefined.


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
console.log(arr[3]); //The arr[3] refers to an item that does not exist.
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 arr = [];
let i = 0;
while(i < n) {
arr.push(i * 2);
i++;
}
console.log(arr.join());

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 one 👍

}

evenNumbers(3); // should output 0,2,4
Expand Down
10 changes: 9 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,15 @@ 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"
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 sum = 0;
do {
sum += (i * 2);
i++;
}
while(n > i);
return sum;
}

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


// Change the below code to use a for loop instead of a while loop.
let i = 0;
while(i < 26) {

for(let i = 0; i < 26; i++) {
console.log(String.fromCharCode(97 + i));
i++;
}
// The output shouldn't change.
5 changes: 4 additions & 1 deletion 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ const AGES = [
49
];

// TODO - Write for loop code here
for(i = 0; i < WRITERS.length; i++){
let ageOfWriters = WRITERS[i] + " is " + AGES[i] + " years old";
console.log(ageOfWriters);
}

/*
The output should look something like this:
Expand Down
7 changes: 6 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,12 @@ let tubeStations = [
"Oxford Street",
"Tottenham Court Road"
];

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

// 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());
}
8 changes: 7 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
*/

function getTemperatureReport(cities) {
// TODO

let temperatureOfCity = [];
for(let city of cities){
let temperature = temperatureService(city);
temperatureOfCity.push('The temperature in '+ city + ' is '+ temperature +' degrees');
}
return temperatureOfCity;
}


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

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

do{
number = generateRandomNumber();
}
while (number <= 50)
return number;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
35 changes: 29 additions & 6 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,27 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
newArticleArray =[];
for(let title of allArticleTitles){

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This impl is correct, but you can try to implement it in a simpler way using Array filter method

if(title.length <= 65){
newArticleArray.push(title);
}
}
return newArticleArray;
}

/*
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 shortestTitle = allArticleTitles[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.

Hint, you might be able to implement this using sorting algorithm, available in Array

for(let title of allArticleTitles){
if(title.split(" ").length < shortestTitle.split(" ").length){
shortestTitle = title;
}
}
return shortestTitle;
}

/*
Expand All @@ -23,15 +34,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 newArrayOfHeadlines = [];
for(let allTitles of allArticleTitles){

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Similarly, you can try to use Array.filter to implement this

if(allTitles.match(/\d/)){
newArrayOfHeadlines.push(allTitles);
}
}
return newArrayOfHeadlines;
}


/*
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 numberOfCharacters = 0;
for(let article of allArticleTitles){
numberOfCharacters += article.length;
}
let averageCharacters = Math.round(numberOfCharacters / allArticleTitles.length);
return averageCharacters;
}


Expand Down
39 changes: 34 additions & 5 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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 ===== */
const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"];

Expand All @@ -33,9 +34,24 @@ 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 getAveragePrices(closingPricesForAllStocks) {
// TODO
}

function getAverage(closingPrices){
let sum = 0;
for(let i = 0; i < closingPrices.length; i++){
sum += closingPrices[i];
}
return sum / closingPrices.length;
};

function getAveragePrices(closingPricesForAllStocks){
let averagePrices = [];
for(let closingPrices of closingPricesForAllStocks){
let averagePrice = getAverage(closingPrices);
averagePrice = Math.round(averagePrice * 100)/100;
averagePrices.push(averagePrice);
}
return averagePrices;
};

/*
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,13 @@ 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 priceChanges = [];
for(let closingPrices of closingPricesForAllStocks){
let priceChange = closingPrices[closingPrices.length - 1] -closingPrices[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.

Likewise, can you try to factor out this into a function?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I have updated it.

priceChange = Math.round(priceChange * 100)/100;
priceChanges.push(priceChange);
}
return priceChanges
}

/*
Expand All @@ -64,7 +86,14 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let descriptions = [];
for(let i = 0; i < closingPricesForAllStocks.length; i++){
let highestPrice = Math.max(...closingPricesForAllStocks[i]);
let stockTicker = stocks[i].toUpperCase();
let description = `The highest price of ${stockTicker} in the last 5 days was ${highestPrice.toFixed(2)}`;
descriptions.push(description);
}
return descriptions;
}


Expand Down
6 changes: 5 additions & 1 deletion 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
*/

function factorial(input) {
// TODO
let result = 1;
for(let i = input; i > 0; i--){
result *= i;
}
return result;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
9 changes: 8 additions & 1 deletion 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
*/

function getHighestRatedInEachGenre(books) {
// TODO
const highestRated = [];
for(let book of books){
const {title, genre, rating} = book;
if(!highestRated[genre] || rating > highestRated[genre].rating){

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hmm, is highestRated[genre] correct?

is highestRated an array or object?

highestRated[genre] = {title, rating};
}
}
return Object.values(highestRated).map((book) => book.title);
}


Expand Down
8 changes: 7 additions & 1 deletion 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
*/

function generateFibonacciSequence(n) {
// TODO

let sequence = [0, 1];
for(let i = 2; i < n; i++){
let nextNumber = sequence[i - 1] + sequence[i - 2];
sequence.push(nextNumber);
}
return sequence
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This is a **private** repository. Please request access from your Teachers, Budd

## Testing your work

- Each of the *.js files in the `1-exercises` folder can be run from the terminal using the `node` command with the path to the file. For example, `node 1-exercises/A-undefined/exercise.js` can be run from the root of the project.
- Each of the *.js files in the `1-exercises` folder can be run from the terminal using the `node` command with the path to the file. For example, node 1-exercises/A-undefined/exercise.js`` can be run from the root of the project.
- To run the tests in the `2-mandatory` folder, run `npm run test` from the root of the project (after having run `npm install` once before).
- To run the tests in the `3-extra` folder, run `npm run extra-tests` from the root of the project (after having run `npm install` once before).

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": {
"git-it": "^1.2.4"
}
}