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
24 changes: 19 additions & 5 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,41 @@
*/

// Example 1
//We are declaring a variable here, but never assigning it to a value -
//therefore the variable is undefined.
let a;
console.log(a);
console.log(a); // There's no value assigned to variable a


// Example 2
//Explanation: Here, we are assigning the value returned from the function
// sayHello to the variable hello.
// But sayHello doesn't actually return anything, so hello is undefined.
function sayHello() {
let message = "Hello";
let message = "Hello"; // There's no output from this function.
}

let hello = sayHello();
console.log(hello);
console.log(hello); //log 'hello', but due to sayHello()function is undefined, so it's logged undefined as well.


// Example 3
// Explanation: Here, we have a function which takes a parameter called user.
// But when we call the function, we're not passing in any arguments - there's nothing in the brackets!
// Because of this, the value of the user parameter inside the function is undefined.

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

sayHelloToUser();
sayHelloToUser(); // There's no argument assigned when call sayHelloToUser()function.


// Example 4
// Explanation: Here, we are trying to retrieve an element from the array at index 3.
// But in this case, the array doesn't have any value at index 3 -
// it only has values at indexes 0, 1 and 2.
// Therefore, retrieving an element from index 3 will give us undefined.

let arr = [1,2,3];
console.log(arr[3]);
console.log(arr[3]); // There's no 4th element in array, so it logs 'undefined'.
21 changes: 17 additions & 4 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
/*
while loops can be useful when you want to execute some code as long as some condition is true.
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.
The list of numbers should start with 0. n is being passed in as a parameter.
Using a while loop, complete the function below so it logs (using
console.log) the first n even numbers as a comma-separated string.
The list of numbers should start with 0.
n is being passed in as a parameter.
*/

function evenNumbers(n) {
// TODO
// TODO
// initializing an array to hold even numbers:
let result = [];
let i = 0;
while (i < n) {
result.push(i * 2);
i++;
}
// joining the elements of the array together as a string
// Read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
console.log(result.join())
}

evenNumbers(3); // should output 0,2,4
Expand Down
21 changes: 19 additions & 2 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 @@ -18,6 +20,21 @@ const BIRTHDAYS = [

function findFirstJulyBDay(birthdays) {
// TODO
let i=0;
while(i<birthdays.length){

// using includes()methods
// let newBday = birthdays.find(x => x.includes('July'));
// return newBday;
// i++;
//

//or using startsWith()method:
if(BIRTHDAYS[i].startsWith('July')){
return BIRTHDAYS[i];
}
i++
}
}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
54 changes: 47 additions & 7 deletions 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,55 @@
/*
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
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

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

function evenNumbersSum(n) {
// TODO
}
// TODO

//1.own solution: initialize first even number is 0, sum is 0, counter from 0;
// let currEvenNumber = 0;
// let sum = 0;
// let i = 0;
// do {
// //sum of first n even number(0):
// sum += currEvenNumber;
// //next even number:
// currEvenNumber += 2;
// //next counter:
// i++;
// } while (i < n); //condition
// return sum; //return final output when condition is false.

//2.solution from google classroom review:
let sum = 0;
let i=0;
do {
sum += (i * 2);
i++;
}while(i < n);
return sum;

}
// for loop: solution
// let currEvenNumber = 0,
// sum = 0;
// // sum of first n even numbers
// for (let i = 0; i < n; i++) {
// sum += currEvenNumber;
// // next even number
// currEvenNumber += 2;
// }
// // required sum
// return sum;
// }

console.log(evenNumbersSum(3)); // should output 6
console.log(evenNumbersSum(0)); // should output 0
console.log(evenNumbersSum(10)); // should output 90
console.log(evenNumbersSum(10)); //should output 90
13 changes: 9 additions & 4 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++;
//let i = 0;
//while(i < 26) {
// console.log(String.fromCharCode(97 + i));
// i++;
//}

for (let i=0; i< 26; 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.

Keep up the good work

console.log((String.fromCharCode(97 + i)).toUpperCase());
}

// The output shouldn't change.
25 changes: 22 additions & 3 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/*
for loops can be useful when we already know exactly how many times we want to loop.
for loops can be useful when we already know exactly how many times
we want to loop.

Below we have 2 arrays which have exactly the same number of values.
- The first array is a list of writers
- The second array is a list of ages
The writers in the first array correspond with the ages in the second array.
The writers in the first array correspond with the ages in the second
array.
For example, Virginia Woolf is 59 years old.

Using a for loop, output to the console a line about the age of each writer.
Using a for loop, output to the console a line about the age of each
writer.
*/

const WRITERS = [
Expand All @@ -27,7 +30,23 @@ const AGES = [
];

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

//2. solution from google classroom review:
for (let i=0; i<WRITERS.length;i++){
// Because we want to retrieve the element at the same index for
// both arrays, we can re-use the same variable i
console.log(`${WRITERS[i]} is ${AGES[i]} years old`)
}

//for (let j=0; j< b.length; j++){
// return AGES[j];
//}

//console.log(writersAges(WRITERS,AGES))
/*
The output should look something like this:

Expand Down
9 changes: 8 additions & 1 deletion 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ let tubeStations = [
"Oxford Street",
"Tottenham Court Road"
];
for ( let tube of tubeStations){
console.log(tube)
}


// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
// 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());
}
28 changes: 23 additions & 5 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,36 @@
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.
We also already have a temperatureService function which will take a city
as a parameter and return a temperature.

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"
- 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) {
function getTemperatureReport(cities) {
// TODO
}

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

//for ... of: (from google classroom review)
let report = [];
for (let city of cities){
let temperature = temperatureService(city);
report.push(`The temperature in ${city} is ${temperature} degrees`);
}
return report;

}


/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
10 changes: 8 additions & 2 deletions 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
In the below example, we want to keep calling generateRandomNumber until we get a value that is > 50.
In the below example, we want to keep calling generateRandomNumber until
we get a value that is > 50.
Implement this using a do-while loop.
*/

Expand All @@ -10,7 +11,12 @@ function generateRandomNumber() {
}

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
// TODO - implement using a do-while loop
let x;
do {
x = generateRandomNumber();
} while (x <= 50); //keep trying until we get a number that is > 50
return x;
}

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