Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
Open
16 changes: 8 additions & 8 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@
For each example, can you explain why we are seeing undefined?
*/

// Example 1
// Example 1 = Variable a has not assigned any value.
let a;
console.log(a);


// Example 2
// Example 2 = Function sayHello()does not return any information or results.
function sayHello() {
let message = "Hello";
}
// return message + " Hi JScript!";
}
let hello = sayHello();
console.log(hello);

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

// Example 3 = When calling a function,function arg-t does not get a value.

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

sayHelloToUser();


// Example 4
// Example 4 = There is no element with index 3 in this array arr.
let arr = [1,2,3];
console.log(arr[3]);
10 changes: 8 additions & 2 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
*/

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

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
13 changes: 10 additions & 3 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,14 @@ const BIRTHDAYS = [
];

function findFirstJulyBDay(birthdays) {
// TODO

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Very good. You can also write it in fewer lines with find()

}

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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

too much indentation

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

whitespace missing before i < n

return sum;
}
console.log("The sum of first three even numbers equal ", evenNumbersSum(3)); // should output 6
console.log("The sum of first zero even numbers equal ", evenNumbersSum(0)); // should output 0
console.log("The sum of first ten even numbers equal ", evenNumbersSum(10)); // should output 90

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


// Change the below code to use a for loop instead of a while loop.
// 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.

7 changes: 5 additions & 2 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ const AGES = [
];

// TODO - Write for loop code here

/*
let i = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you don't need to declare i here, since it's declared in the line below, in the for statement.
This could lead to future errors because now you have 2 different i variables, one outside of your for loop (which you don't need) and the one inside the for loop (which you need)

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

Virginia Woolf is 59 years old
Expand Down
13 changes: 9 additions & 4 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
let tubeStations = [
"Aldgate",
"Baker Street",
"Picadilly Circus",
"Oxford Street",
"Tottenham Court Road"
"Canada Water",
"Forest Hill",
" "
];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why did you change the list?


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

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

/*
Imagine we're making a weather app!

We have a list of cities that the user wants to track.
We also already have a temperatureService function which will take a city as a parameter and return a temparature.

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"
- Hint: you can call the temperatureService function from your function
*/

function getTemperatureReport(cities) {
// TODO
// TO DO
}


Expand Down
3 changes: 2 additions & 1 deletion 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ function generateRandomNumber() {
return Math.round(Math.random() * 100);
}


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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
1 change: 1 addition & 0 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
return allArticleTitles.filter((items) => items.length < 65);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This works perfectly, well done.
Hint: the choice of variable and parameter names is important to make your code readable. Here, items doesn't really describe what it contains. For instance, it could have the value "This is the title", which isn't really an items. Instead it's just a title of an article, so it would make more sense to call it just title for example.

}

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


}

/*
Expand Down
28 changes: 27 additions & 1 deletion 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,36 @@
Using a loop, complete the function below so it returns the factorial of the number being passed in.
*/

function factorial(input) {
function factorial(n) {
// TODO
let result = 1;
while (n) {
result *= n--;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This works, but some teams prefer not to use -- or ++ because they make expressions hard to understand, specifically because expressions that change the values of variables (also called expressions with side effects) are confusing. So try to rewrite this expression to move the change of n on a separate line.

return result;
}
console.log("Factorial number 2 = ", factorial(2));
console.log("factorial number 3 = ", factorial(3));
console.log("factorial number 5 = ", factorial(5));
console.log("factorial number 10 = ", factorial(10));


/* sol 2
function factorial(n) {
if (n === 1) {
return 1;
}

return n * factorial(n - 1);
}

alert(factorial(1) === 1);
alert(factorial(2) === 2);
alert(factorial(3) === 6);
alert(factorial(4) === 24);
alert(factorial(5) === 120);
*/

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

test("3! should be 6", () => {
Expand Down
16 changes: 14 additions & 2 deletions 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,20 @@
*/

function generateFibonacciSequence(n) {
// TODO
}
// TODO
let a = 1;
let b = 1;
for (let i = 3; i <= n; i++) {
let c = a + b;
a = b;
b = c;
}
return b;
}
console.log(generateFibonacciSequence(3)); // 2
console.log(generateFibonacciSequence(5)); // 5
console.log(generateFibonacciSequence(10)); //55
console.log(generateFibonacciSequence(55)); // 139583862445

/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the first 10 numbers in the Fibonacci Sequence", () => {
Expand Down