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
7 changes: 7 additions & 0 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// Example 1
let a;
console.log(a);
//a was not assigned any value



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

let hello = sayHello();
console.log(hello);
//The variable "sayHello" were not passed any value


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

sayHelloToUser();
//Again the function "sayHelloToUser", has not been passed any arguments


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
// In this example above, there are no index value 3 in the array.
// Arrays work with zero indexing and therefore a index of 3 would indicate a fourth value in the array
// and this array only has three values and result to an undefined error.
8 changes: 8 additions & 0 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

function evenNumbers(n) {
// TODO
let i = 0;
let evenArray = [];

while (i < n) {
evenArray.push(i * 2);
i++;
}
console.log(evenArray.join(', '));
}

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

function findFirstJulyBDay(birthdays) {
// TODO
//TODO
let i = 0;
let myBdayCheck;

while (i < birthdays.length - 1) {
myBdayCheck = birthdays[i];

if (myBdayCheck.startsWith("July")) {
return myBdayCheck;
}
i++;
}
}


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

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

do {
sum = sum + i * 2;
i++;
} while (i < n);
return sum;
}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
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) {

for (let i = 0; i < 26; i++) {
console.log(String.fromCharCode(97 + i));
i++;

}
// The output shouldn't change.
5 changes: 5 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,11 @@ 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
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 (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());
}
10 changes: 9 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@
Implement the function below:
- take the array of cities as a parameter
- return an array of strings, which is a statement about the temperature of each city.
For example, "The temperature in London is 10 degrees"
For example, ""
- Hint: you can call the temperatureService function from your function
*/

function getTemperatureReport(cities) {
// TODO

let tempArray = [];
for (let city of cities) {
tempArray.push(`The temperature in ${city} is ${temperatureService(city)} degrees`);
}
return tempArray;

}



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

function temperatureService(city) {
Expand Down
6 changes: 6 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,12 @@ function generateRandomNumber() {

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

return num;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
63 changes: 61 additions & 2 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let fitTitles = [];
for (let articleTitle of allArticleTitles) {

if (articleTitle.length <= 65) {
fitTitles.push(articleTitle);
}

}

return fitTitles;
}

/*
Expand All @@ -15,7 +25,26 @@ function potentialHeadlines(allArticleTitles) {
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
}
let fewest = allArticleTitles[0];
let prevCount = allArticleTitles[0].split(" ").length;

for(let i = 1; i < allArticleTitles.length; i++) {

let currentCount = allArticleTitles[i].split(" ").length;

if(currentCount < prevCount) {
fewest = allArticleTitles[i];
}

prevCount = currentCount;
}

return fewest;

}




/*
The editor of the FT has realised that headlines which have numbers in them get more clicks!
Expand All @@ -24,14 +53,44 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
}
let numberHeadlines = [];

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

for( let j = 0; j < titleChecker.length; j++) {

let charChecker = Number(titleChecker[j])

if(Number.isInteger(charChecker) && charChecker > 0 ){
numberHeadlines.push(titleChecker);
break;
}
}
}
return numberHeadlines;
}




/*
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 averageNum = 0;
let titleSum = 0;

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

let averageNum = titleSum / allArticleTitles.length;

return Math.round(averageNum);
}


Expand Down
37 changes: 37 additions & 0 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO

let averageStockArr = [];
for(let i = 0; i < closingPricesForAllStocks.length; i++){
let sumOfStock = 0;
for( let j = 0; j < closingPricesForAllStocks[i].length; j++ ) {
sumOfStock += closingPricesForAllStocks[i][j];
}
let averageSum = sumOfStock / closingPricesForAllStocks.length;
averageStockArr.push(parseFloat(averageSum.toFixed(2)));
}
return averageStockArr;
}

/*
Expand All @@ -49,6 +60,17 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO

let priceChangeArr = [];

for(let i = 0; i < closingPricesForAllStocks.length; i++){
let last = closingPricesForAllStocks[i].length - 1;
let firstPrice = closingPricesForAllStocks[i][0];
let lastPrice = closingPricesForAllStocks[i][last];
let averagePriceSum = lastPrice - firstPrice;
priceChangeArr.push(parseFloat(averagePriceSum.toFixed(2)));
}
return priceChangeArr;
}

/*
Expand All @@ -65,9 +87,24 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let highestStockPrice = [];

for(let i = 0; i < closingPricesForAllStocks.length; i++){
let highTotal = 0;
for(let j = 0; j < closingPricesForAllStocks[i].length; j++){
if(closingPricesForAllStocks[i][j] > highTotal){
highTotal = closingPricesForAllStocks[i][j];
}
}
highestStockPrice.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highTotal.toFixed(2)}`);
}


return highestStockPrice;
}



/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the average price for each stock", () => {
expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
Expand Down
15 changes: 11 additions & 4 deletions 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@
*/

function factorial(input) {
// TODO
// TODO

let sum = input;

for (let i = input - 1; i >= 1; i--) {
sum = sum * i;
}
return sum;
}

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

test("3! should be 6", () => {
expect(factorial(3)).toEqual(6);
expect(factorial(3)).toEqual(6);
});

test("5! should be 120", () => {
expect(factorial(5)).toEqual(120);
expect(factorial(5)).toEqual(120);
});

test("10! should be 3628800", () => {
expect(factorial(10)).toEqual(3628800);
expect(factorial(10)).toEqual(3628800);
});
Loading