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/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,37 @@
// Example 1
let a;
console.log(a);

/*Answer
The code in example 1 will output undefined beasue a value has not been assigned to variable a,
So, when the program exacutes the code in line 14 will not get any output value.

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! All your answers here are fairly clear and detailed, they show a good understanding level

*/

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

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

/*
The sayHello() function in example 2, is not return any value, therefore the program will print out undefined.
*/

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

sayHelloToUser();

/* Answer for example 3
The funciton is called with not value, to replace the 'user' parameter
*/

// Example 4
let arr = [1,2,3];
let arr = [1, 2, 3];
console.log(arr[3]);
/*
Index 3 is not defined means there is no value in index 3. Therefore, the program will output undefined message.
*/
10 changes: 8 additions & 2 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
/*
while loops can be useful when you want to execute some code as long as some condition is true.

Using a while loop, complete the function below so it logs (using console.log) the first n even numbers as a comma-seperated string.
Using a while loop, complete the function below so it logs (using console.log)
the first n even numbers as a comma-seperated string.
The list of numbers should start with 0. n is being passed in as a parameter.
*/

function evenNumbers(n) {
// TODO
// TODO
let i = 0;
while (i % 2 === 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.

So, this looks good from a distance but it doesn't quite work as intended. The loop will only run once whatever number you pass to the function. Can you see why?

console.log();
i++;
}
}

evenNumbers(3); // should output 0,2,4
Expand Down
42 changes: 39 additions & 3 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/*
Loops can be useful when working with arrays.
In the below example, imagine we've defined an array holding the birthdays of your closest friends.
Use a while loop to search through the array until you find the first birthday in July, then return that birthday from the function.
In the below example,
imagine we've defined an array holding the birthdays of your closest friends.
Use a while loop to search through the array until you find the first birthday in July,
then return that birthday from the function.
*/

const BIRTHDAYS = [
Expand All @@ -16,8 +18,42 @@ const BIRTHDAYS = [
"November 15th"
];

console.log();

function findFirstJulyBDay(birthdays) {
// TODO
// TODO
// lets sort the array first a-z

birthdays.sort();
let i = 0;
while (i < birthdays.length) {
if (birthdays[i].includes('July')) {
return birthdays[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.

Works, good work, bit much commnented out code here I would say See: https://syllabus.codeyourfuture.io/guides/code-style-guide#dont-leave-lots-of-commented-out-code

}
i++
}
/*
using for loop


BIRTHDAYS.sort();
for (let i = 0; i < birthdays.length; i++){
if (birthdays[i].includes('July')) {
return birthdays[i]
}

}
*/
}

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

/*
let i = 0;
while (birthdays[i] === 'July 11th') {
console.log(birthdays[i])
i++;
}

return birthdays[i] = birthdays[i];
*/
41 changes: 35 additions & 6 deletions 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
/*
Sometimes when using loops, we'll want to execute the body of the loop at least once. We can make sure this happens by using a do-while loop.
Sometimes when using loops, we'll want to execute the body of the loop at least once.
We can make sure this happens by using a do-while loop.
- If the condition in a while loop is initially false, the body of the loop will never execute
- But in a do-while loop, because the condition is checked after the body, we know that it will always execute at least once
- But in a do-while loop, because the condition is checked after the body,
we know that it will always execute at least once

Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0)
1 Using a do-while loop,
2 write a function which returns
*the sum of
*the first n even numbers (starting from 0)
*/

function evenNumbersSum(n) {
// TODO

let sum = 0;
let arr = [];
for (let i = 0; i < n; i++){
if (i >0 && i % 2 === 0) {
arr.push(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.

This is decent code but it doesn't "return the sum of the first n even numbers (starting from 0)"

for (let j = 0; j < arr; j++) {
console.log(arr[j]);
}
// console.log(sum)
}
// return sum;
}
evenNumbersSum(10);

console.log(evenNumbersSum(3)); // should output 6
console.log(evenNumbersSum(0)); // should output 0
console.log(evenNumbersSum(10)); // should output 90
/*
if (n[i] % 2 === 0) {
let arr = [];
arr.push(n[i]);
}
*/
//evenNumbersSum(10);
// console.log(n)
/*
console.log(evenNumbersSum(3)); should output 6
console.log(evenNumbersSum(0)); should output 0
console.log(evenNumbersSum(10)); should output 90
*/
13 changes: 8 additions & 5 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
Change the while loop below into a for 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++;
// let i = 0;
// while(i < 26) {

// i++;
// }

for (let i = 0; i < 26; i++) {
console.log(String.fromCharCode(97 + 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.

Good work

}
// 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 @@ -27,7 +27,9 @@ const AGES = [
];

// TODO - Write for loop code here

for (let i = 0; i < WRITERS.length; i++){
console.log(`${WRITERS[i]} ${AGES[i]} years old`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good work

}
/*
The output should look something like this:

Expand Down
17 changes: 12 additions & 5 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@

// TODO Use a for-of loop to output each of the tube stations below.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All good here!

let tubeStations = [
"Aldgate",
"Baker Street",
"Picadilly Circus",
"Oxford Street",
"Tottenham Court Road"
"Aldgate",
"Baker Street",
"Picadilly Circus",
"Oxford Street",
"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 i of str) {
console.log(i.toUpperCase());
}
23 changes: 22 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,29 @@
*/

function getTemperatureReport(cities) {
// TODO
// TODO
return cities.map(city => `The temperature in ${city} is ${temperatureService(city)} degrees`)

}

// console.log(getTemperatureReport(["London", "Paris", "São Paulo"]));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bit too much commented out code here but the tests pass and the non commented out code looks good, so good work


/*
for (let city of cities) {
return `The temperature in ${temperatureService(city)} degrees`
}
cities.forEach((element, i) => {
console.log(element, i)
});
*/
/*
getTemperatureReport([
"London",
"Paris",
"São Paulo"
])
*/


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

Expand All @@ -31,6 +51,7 @@ function temperatureService(city) {

return temparatureMap.get(city);
}
// getTemperatureReport(cities);

test("should return a temperature report for the user's cities", () => {
let usersCities = [
Expand Down
11 changes: 10 additions & 1 deletion 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@
// This function shouldn't be changed
function generateRandomNumber() {
console.log("Generating number...");
return Math.round(Math.random() * 100);
return Math.round(Math.random() * 100) ;
}

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop

let i = 0;
do {
i = generateRandomNumber();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Tests pass, good work

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Tests pass, good work

} while (i < 50);

return i;
}


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

test("Returned value should always be greater than 50", () => {
Expand Down
Loading