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

// Example 1
let a;
let a = 1;
console.log(a);


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

let hello = sayHello();
console.log(hello);


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

sayHelloToUser();

sayHelloToUser("some value");

// Example 4
let arr = [1,2,3];
console.log(arr[3]);
let arr = [1, 2, 3];
console.log(arr);
25 changes: 24 additions & 1 deletion 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,33 @@
The list of numbers should start with 0. n is being passed in as a parameter.
*/

// function evenNumbers(n) {
// // TODO

// while(n ){
// console.log();
// }
// }

//experiment

function evenNumbers(n) {
// TODO
// TODO
let numArray = [0,1,2,3,4,5,6,7,8,9];
let counter = 0;
let stringVal = [];

while (counter<n) {
stringVal[counter] = numArray[counter] * 2;
counter++;

}

console.log(stringVal);

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 loop will run more than once

}


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
7 changes: 7 additions & 0 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ const BIRTHDAYS = [

function findFirstJulyBDay(birthdays) {
// TODO
let i = 0;
while (i < birthdays.length) {
if (birthdays[i].includes("July")) {
return birthdays[i];
}
i++;
}
}

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

20 changes: 18 additions & 2 deletions 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,25 @@
*/

function evenNumbersSum(n) {
// TODO
// TODO
let numArray = [0,1,2,3,4,5,6,7,8,9,10];
let counter = 0;
let sum = 0;

do {
// if (numArray[counter] % 2 == 0) {
sum += counter * 2;

// } else {

// }
counter++;

} while (counter < n);

return sum;
}

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


// Change the below code to use a for loop instead of a while loop.
let i = 0;
while(i < 26) {
// let i = 0;
for (let i = 0; i < 26;i++) {
console.log(String.fromCharCode(97 + i));
i++;
// i++;
}
// The output shouldn't change.
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 @@ -28,6 +28,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
9 changes: 8 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,14 @@ let tubeStations = [
"Oxford Street",
"Tottenham Court Road"
];

for (let i = 0; i < tubeStations.length; i++) {
console.log(tubeStations[i]);
}

// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
let newString = "";
for (let i = 0; i < str.length; i++) {
newString = str[i].toUpperCase();
console.log(newString);
}
7 changes: 7 additions & 0 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@

function getTemperatureReport(cities) {
// TODO
let tempArray = [];
for (let i = 0; i < cities.length; i++) {
let temp = temperatureService(cities[i]);
// console.log(temp);
tempArray[i] = "The temperature in " + cities[i] + " is " + temp + " degrees";
}
return tempArray
}


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

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

do {
randomNumber = generateRandomNumber();
}
while (randomNumber <= 50);

return randomNumber;

}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
40 changes: 40 additions & 0 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {

let newArray = [];
for (let i = 0; i < allArticleTitles.length;i++) {

if (allArticleTitles[i].length <= 65) {
newArray.push(allArticleTitles[i]);
}
// console.log(newArray[i]);

}
return newArray;
// TODO
}

Expand All @@ -15,6 +26,19 @@ function potentialHeadlines(allArticleTitles) {
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let spaceCounter = [];
let spaceCounterSize = [];
for (let i = 0; i < allArticleTitles.length; i++) {
spaceCounter.push(allArticleTitles[i].split(" "));
// console.log(spaceCounter);
spaceCounterSize.push(spaceCounter[i].length);
}
// console.log(spaceCounter);

// console.log(spaceCounterSize);
// console.log(Math.min.apply(Math,spaceCounterSize));

return allArticleTitles[spaceCounterSize.indexOf(Math.min.apply(Math,spaceCounterSize))];
}

/*
Expand All @@ -24,14 +48,30 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO

let newArray = [];
for (let i = 0; i < allArticleTitles.length; i++) {
if (/\d/.test(allArticleTitles[i])) {
newArray.push(allArticleTitles[i]);
}
}
// console.log(newArray);
return newArray;
}

// return /\d/.test(str);
// /\d/.test(allArticleTitles[i]))
/*
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 average = 0;
for (let i = 0; i < allArticleTitles.length; i++) {
average += allArticleTitles[i].length;
}
return parseInt(average / allArticleTitles.length);
}


Expand Down
89 changes: 65 additions & 24 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"];

const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
[179.19, 180.33, 176.28, 175.64, 172.99], // AAPL
[340.69, 342.45, 334.69, 333.20, 327.29], // MSFT
[3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN
[2951.88, 2958.13, 2938.33, 2928.30, 2869.45], // GOOGL
[1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA
[179.19, 180.33, 176.28, 175.64, 172.99], // AAPL
[340.69, 342.45, 334.69, 333.2, 327.29], // MSFT
[3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN
[2951.88, 2958.13, 2938.33, 2928.3, 2869.45], // GOOGL
[1101.3, 1093.94, 1067.0, 1008.87, 938.53], // TSLA
];

/*
Expand All @@ -34,7 +34,20 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
// TODO
let sum = 0;
let averageVal = 0;
let averagesArray = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
for (let j = 0; j < closingPricesForAllStocks[i].length; j++) {
sum += closingPricesForAllStocks[i][j];
}
averageVal = sum / closingPricesForAllStocks[i].length;
averagesArray.push(parseFloat(averageVal.toFixed(2)));
sum = 0;
}
console.log(averagesArray);
return averagesArray;
}

/*
Expand All @@ -48,7 +61,24 @@ 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 difference = 0;
let differenceArray = [];



for (let i = 0; i < closingPricesForAllStocks.length; i++) {
for (let j = 0; j < closingPricesForAllStocks[i].length; j++) {
difference = closingPricesForAllStocks[i][closingPricesForAllStocks[i].length-1] - closingPricesForAllStocks[i][0];
}
console.log(difference);

differenceArray.push(parseFloat(difference.toFixed(2)));
difference = 0;
}
console.log(differenceArray);
return differenceArray;
}

/*
Expand All @@ -64,31 +94,42 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
// TODO

let highestValue = 0;
let highestValueArray = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
for (let j = 0; j < closingPricesForAllStocks[i].length; j++) {
highestValue = Math.max.apply(Math, closingPricesForAllStocks[i]);
}
highestValueArray.push("The highest price of " + stocks[i].toUpperCase() + " in the last 5 days was " + highestValue.toFixed(2));

}
console.log(highestValueArray);
return highestValueArray;
}


/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the average price for each stock", () => {
expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
[176.89, 335.66, 3405.66, 2929.22, 1041.93]
);
expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual([
176.89, 335.66, 3405.66, 2929.22, 1041.93,
]);
});

test("should return the price change for each stock", () => {
expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
[-6.2, -13.4, 23.9, -82.43, -162.77]
);
expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual([
-6.2, -13.4, 23.9, -82.43, -162.77,
]);
});

test("should return a description of the highest price for each stock", () => {
expect(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)).toEqual(
[
"The highest price of AAPL in the last 5 days was 180.33",
"The highest price of MSFT in the last 5 days was 342.45",
"The highest price of AMZN in the last 5 days was 3421.37",
"The highest price of GOOGL in the last 5 days was 2958.13",
"The highest price of TSLA in the last 5 days was 1101.30"
]
);
expect(
highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)
).toEqual([
"The highest price of AAPL in the last 5 days was 180.33",
"The highest price of MSFT in the last 5 days was 342.45",
"The highest price of AMZN in the last 5 days was 3421.37",
"The highest price of GOOGL in the last 5 days was 2958.13",
"The highest price of TSLA in the last 5 days was 1101.30",
]);
});
5 changes: 5 additions & 0 deletions 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
Loading