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

// Example 1
let a;
let a; // there is no value has been added to a therefore a is undefined.
console.log(a);


// Example 2
function sayHello() {
function sayHello() { // there is no parameter passed to the function's argument.
let message = "Hello";
}

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

sayHelloToUser();
sayHelloToUser(); // again there is no value been passed when calling the function. it should look like sayHelloToUser(value);


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
let arr = [1,2,3];
console.log(arr[3]); // there are only 2 elements in this array (starting from 0), so the output will be undefined.
16 changes: 12 additions & 4 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
The list of numbers should start with 0. n is being passed in as a parameter.
*/

const evenArray = [];

function evenNumbers(n) {
// TODO
let i = 0;
while (evenArray.length < n) {
evenArray.push(i);
i += 2;
}

console.log(evenArray.toString());
}

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(3); // should output 0,2,4
// evenNumbers(0); // should output nothing
evenNumbers(20); // should output 0,2,4,6,8,10,12,14,16,18
7 changes: 5 additions & 2 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ const BIRTHDAYS = [
];

function findFirstJulyBDay(birthdays) {
// TODO
// let i = 0;
// while (i < BIRTHDAYS.length){
return BIRTHDAYS.find(BIRTHDAYS => BIRTHDAYS === "July 11th");
// } // TODO
}

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

Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0)
*/

let i = 0;
let total = 0;
const evenArray = [];
// let sum = 0;
function evenNumbersSum(n) {
// TODO

do {
if (i%2 === 0){
evenArray.push(i);
total = total + i;
}
i++;

} while (evenArray.length < n);

return total;
}

console.log(evenNumbersSum(3)); // should output 6
console.log(evenNumbersSum(0)); // should output 0
console.log(evenNumbersSum(10)); // should output 90
// console.log(evenNumbersSum(3)); // should output 6
console.log(evenNumbersSum(10)); // should output 0
// console.log(evenNumbersSum(10)); // should output 90
6 changes: 2 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,7 @@


// 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++;
for (let i = 0; i < 26; i++) {
console.log(String.fromCharCode(97 + i));
}
// The output shouldn't change.
23 changes: 10 additions & 13 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,20 @@
*/

const WRITERS = [
"Virginia Woolf",
"Zadie Smith",
"Jane Austen",
"Bell Hooks",
"Yukiko Motoya"
]

const AGES = [
59,
40,
41,
63,
49
"Virginia Woolf",
"Zadie Smith",
"Jane Austen",
"Bell Hooks",
"Yukiko Motoya",
];

const AGES = [59, 40, 41, 63, 49];

// TODO - Write for loop code here

for (let i = 0; i < AGES.length; i++) {
console.log(`${WRITERS[i]} is ${AGES[i]} years old`);
}
/*
The output should look something like this:

Expand Down
10 changes: 10 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,16 @@ let tubeStations = [
"Tottenham Court Road"
];

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




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

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

function getTemperatureReport(cities) {
// TODO
let tempReport = [];
for (let city of cities){
let temp = temperatureService(city);
tempReport.push(`The temperature in ${city} is ${temp} degrees`);
}
return tempReport;
}


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 generateNum;

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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
39 changes: 35 additions & 4 deletions 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 articleArr = [];
for (const element of allArticleTitles) {
if (element.length <= 65){
articleArr.push(element);
}
return articleArr;
}

/*
Expand All @@ -14,24 +19,49 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let fewestWords = (a, b) => a.length <= b.length ? a : b;
return allArticleTitles.reduce(fewestWords);
}

/*
The editor of the FT has realised that headlines which have numbers in them get more clicks!
Implement the function below to return a new array containing all the headlines which contain a number.
(Hint: remember that you can also loop through the characters of a string if you need to)
*/

function checkTitleContainNum(title){
for(let character of title){
if(character >= '0' && character <= '9'){
return true;
}
else {
return false;
}
}
}
function headlinesWithNumbers(allArticleTitles) {
// TODO
let articleWNum = [];

for (let title of allArticleTitles){
if(checkTitleContainNum(title)){
articleWNum.push(title);
}
}
return articleWNum;
}


/*
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 totalChar = 0;

for(let title of allArticleTitles){
totalChar += title.length;
}
return Math.round(totalChar / allArticleTitles.length);
}


Expand Down Expand Up @@ -79,3 +109,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);
});

70 changes: 67 additions & 3 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,27 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let averages = [];

for (let pricesForStock of closingPricesForAllStocks) {
averages.push(getAveragePricesForStock(pricesForStock));
}

return averages;
}

function getAveragePricesForStock(pricesForStock) {
let total = 0;

for (let price of pricesForStock) {
total += price;
}

return roundTo2Decimals(total / pricesForStock.length);
}

function roundTo2Decimals(num) {
return Math.round(num * 100) / 100;
}

/*
Expand All @@ -48,7 +68,19 @@ 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 changes = [];

for (let pricesForStock of closingPricesForAllStocks) {
changes.push(getPriceChangeForStock(pricesForStock));
}

return changes;
}

function getPriceChangeForStock(pricesForStock) {
let priceChange =
pricesForStock[pricesForStock.length - 1] - pricesForStock[0];
return roundTo2Decimals(priceChange);
}

/*
Expand All @@ -64,9 +96,41 @@ 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 = getHighestPrice(closingPricesForAllStocks[i]);
descriptions.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPrice.toFixed(2)}`);
}

return descriptions;
}

function getHighestPrice(pricesForStock) {

let highestPriceSoFar = 0;

for(let price of pricesForStock) {

if(price > highestPriceSoFar) {
highestPriceSoFar = price;
}
}

return highestPriceSoFar;
}

function highestPriceDescriptionsAlternate(closingPricesForAllStocks, stocks) {
let descriptions = [];

for(let i = 0; i < closingPricesForAllStocks.length; i++) {
let highestPrice = Math.max(...closingPricesForAllStocks[i]);
descriptions.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPrice.toFixed(2)}`);
}

return descriptions;



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