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
9 changes: 6 additions & 3 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,28 @@
// Example 1
let a;
console.log(a);

//a has not defined.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

a is declared but not assigned to the value so a is undefined


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

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

//sayhello() function will not return anything.

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

sayHelloToUser();

//no argument in envoked function

// Example 4

let arr = [1,2,3];
console.log(arr[3]);
//no 4th element
12 changes: 11 additions & 1 deletion 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@
*/

function evenNumbers(n) {
// TODO
let number = 0;
let output = [];
while (n > 0){
output.push(number);
number += 2;
n--;
}
output = output.join(',');
console.log(output);

}


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
20 changes: 20 additions & 0 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,28 @@ const BIRTHDAYS = [
"November 15th"
];

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

// while(i < birthdays.length){
// if(birthdays[i].includes("july")) {
// return birthdays[i];
// }
// i++;
// }

// }


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

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 number = 0;
let sum = 0;
do {
sum += number;
number += 2;
n--;
} while (n > 0);
return sum;
}

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


// 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++;
}

// The output shouldn't change.
// let i = 0;
// while(i < 26) {
// console.log(String.fromCharCode(97 + i));
// i++;
// }

for (i = 0; i < 26; i++) {
console.log(String.fromCharCode(97 + i));
}
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 @@ -27,7 +27,9 @@ const AGES = [
];

// TODO - Write for loop code here

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

Expand Down
10 changes: 8 additions & 2 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ let tubeStations = [
"Baker Street",
"Picadilly Circus",
"Oxford Street",
"Tottenham Court Road"
"Tottenham Court Road",
];

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

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

for (let char of str) {
console.log(char.toUpperCase())
}
5 changes: 5 additions & 0 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

function getTemperatureReport(cities) {
// TODO
let result = [];
for (let i = 0; i < cities.length; i++) {
result.push(`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`);
}
return result;
}


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

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


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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
47 changes: 47 additions & 0 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
// let newArr = [];
// for(let i of allArticleTitles) {
// if(allArticleTitles.length <= 65){
// newArr.push(allArticleTitles[i]);
// }
// }
// return newArr;
// }


return allArticleTitles.filter(items => items.length < 65);
}

/*
Expand All @@ -15,6 +26,11 @@ function potentialHeadlines(allArticleTitles) {
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let arr = [];
for (let i = 0; i < allArticleTitles.length; i++) {
arr.push(allArticleTitles[i].split(" ").length);
}
return allArticleTitles[arr.indexOf(Math.min(...arr))];
}

/*
Expand All @@ -24,6 +40,32 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
// let newArr = []
// for (let i of ARTICLE_TITLES) {
// for (let el of i){
// if (el.includes(Number)) {
// return newArr.push(el.includes(Number));
// }
// }
// return newArr;
// }

// second solution

let NumberArray = []
const regex = /[0-9]/g;
for (let i = 0; i < allArticleTitles.length; i++) {
if (allArticleTitles[i].search(regex) >= 0) { NumberArray.push(allArticleTitles[i]) }
}
return NumberArray



// third solution

// return allArticleTitles.filter((element) =>
// [...element].find((number) => number >= "0" && number <= "9")
// );
}

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


Expand Down
38 changes: 38 additions & 0 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,27 @@ 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 calculateAverage(arr) {
let total = 0;
for (let price of arr) {
total += price;
}
const average = total / arr.length;
return average;
}


function getAveragePrices(closingPricesForAllStocks) {
// TODO
const myArr = [];
for (let stock of closingPricesForAllStocks) {
const average = calculateAverage(stock);
const formattedAverage = Number(average.toFixed(2));
myArr.push(formattedAverage);
}
return myArr;


}

/*
Expand All @@ -49,6 +68,14 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let myArra = [];
for (let price of closingPricesForAllStocks) {
let firstDay = price[0];
let lastDay = price[price.length - 1];
let difference = lastDay - firstDay;
myArra.push(Number(difference.toFixed(2)));
}
return myArra;
}

/*
Expand All @@ -65,6 +92,17 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let arr = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
arr.push(
`The highest price of ${stocks[
i
].toUpperCase()} in the last 5 days was ${Math.max(
...closingPricesForAllStocks[i]
).toFixed(2)}`
);
}
return arr;
}


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 sum = 1
for (i = 1; i <= input; i++){
sum *= i
}
return sum
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
16 changes: 16 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,22 @@

function getHighestRatedInEachGenre(books) {
// TODO
const result = books.reduce((acc, cur) => {
const groupByGenre = cur.genre;
if (!acc[groupByGenre]) {
acc[groupByGenre] = [];
}
acc[groupByGenre].push(cur);
return acc;
}, {});
const genreName = Object.keys(result);
const arr = [];
for (let i = 0; i < genreName.length; i++) {
arr.push(
result[genreName[i]].sort((a, b) => b.rating - a.rating)[0].title
);
}
return arr;
}


Expand Down
5 changes: 5 additions & 0 deletions 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

function generateFibonacciSequence(n) {
// TODO
let fibo = [0, 1];
for (let i = 0; i < n - 2; i++) {
fibo.push(fibo[i] + fibo[i + 1]);
}
return fibo;
}

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