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
20 changes: 16 additions & 4 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@
*/

function evenNumbers(n) {
// TODO
// TODO
// const arr =[];
// for (let i=0;i<(n*2);i+=2){
// arr.push(i);
// }
// return arr;
const arr = [];
let i = 0;
while (i < n * 2) {
arr.push(i);
i += 2;
}
return arr;
}

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
console.log(evenNumbers(3)); // should output 0,2,4
console.log(evenNumbers(0)); // should output nothing
console.log(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"
9 changes: 8 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,14 @@
*/

function evenNumbersSum(n) {
// TODO
let i = 0;
let sum =0;
do {
sum += i ;
i+=2;
}
while (i < n*2);
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.
4 changes: 3 additions & 1 deletion 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const AGES = [
63,
49
];

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

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

for (let x of tubeStations) {
console.log(x)
}


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

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



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

function temperatureService(city) {
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 randomNumber =0;
do {
randomNumber = generateRandomNumber()
}while (randomNumber <= 50);
return randomNumber;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
39 changes: 36 additions & 3 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,51 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
}

const arr = [];
for ( let x of allArticleTitles){
if (x.length<= 65 ) {
arr.push(allArticleTitles);
}
return arr;
}
/*
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 i = 0;
// let j;
// while (i < allArticleTitles.length) {
// j = i + 1;
// while (j < allArticleTitles.length) {
// if (allArticleTitles[j] < allArticleTitles[i]) {
// let tempStr = allArticleTitles[i];
// allArticleTitles[i] = allArticleTitles[j];
// allArticleTitles[j] = tempStr;
// }
// j++;
// }
// i++;
// }
// return allArticleTitles[0];
}


/*
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 headlinesWithNumbers(allArticleTitles) {
// TODO
// const arr=[];
// for (i=0 ;i<allArticleTitles.length;i++){
// if (allArticleTitles[i].match([0-9]/g)){
// arr.push(allArticleTitles[i]);
return allArticleTitles.filter((element) => {
return /\d/.test(element);
});
}

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


Expand Down
18 changes: 18 additions & 0 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,20 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let sum = 0;
let arr =[];
let average =[];
for ( let i=0;i<closingPricesForAllStocks.length;i++){
for (let j=0 ;j<closingPricesForAllStocks[i].lenght;j++){
sum+=closingPricesForAllStocks[i][j];
}
arr[i].push(sum);
average.push(arr[i] / closingPricesForAllStocks[i].length);
}
return average;
}


/*
We also want to see what the change in price is from the first day to the last day for each stock.
Implement the below function, which
Expand All @@ -49,6 +61,11 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO

(for let x of closingPricesForAllStocks){


}
}

/*
Expand All @@ -65,6 +82,7 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
(for )
}


Expand Down
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
8 changes: 8 additions & 0 deletions 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

function getHighestRatedInEachGenre(books) {
// TODO
let arr=[];
for(let i=0;i<BOOKS.length;i++){
arr.push(Math.max(BOOKS[i].rating));
for(let j=0;j<arr.length;j++){
delete arr[j].rating
return
}
return arr;
}


Expand Down
29 changes: 18 additions & 11 deletions 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,31 @@
*/

function generateFibonacciSequence(n) {
// TODO
if (n<3){
return 1;
}else{
const sum =[0,1]
for(let x= 2;x < n; x++){
sum[x]=sum[x-1]+sum[x-2]
sum.push[sum[x]]
}
return sum;
}
}

/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the first 10 numbers in the Fibonacci Sequence", () => {
expect(generateFibonacciSequence(10)).toEqual(
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
);
expect(generateFibonacciSequence(10)).toEqual([
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
]);
});

test("should return the first 5 numbers in the Fibonacci Sequence", () => {
expect(generateFibonacciSequence(5)).toEqual(
[0, 1, 1, 2, 3]
);
expect(generateFibonacciSequence(5)).toEqual([0, 1, 1, 2, 3]);
});

test("should return the first 15 numbers in the Fibonacci Sequence", () => {
expect(generateFibonacciSequence(15)).toEqual(
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
);
});
expect(generateFibonacciSequence(15)).toEqual([
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,
]);
});