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
116 changes: 116 additions & 0 deletions 1-exercises/A-undefined/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
function double(n) {
return n * 2;
}
console.log(double(3));
console.log(double(5));


function doubleEachElement(arr) {
let newArr = [];
for(let element of arr) {
let newValue = double(element);
newArr.push(newValue);
}
return newArr;
}


function triple(n) {
return n * 3;
}
console.log(double(3));
console.log(double(5));

function tripleEachElement(arr) {
let newArr = [];
for(let element of arr) {
let newValue = triple(element);
newArr.push(newValue);
}
return newArr;
}


function update(arr, func) {
let newArr = [];
for(let element of arr) {
let newValue = func(element);
newArr.push(newValue);
}
return newArr;
}
let arr =[2, 3, 4];
let doublesValues = update(arr, double);
let tripleValues = update(arr, triple);
console.log(doublesValues);
console.log(tripleValues);

function double(n) {
return n * 2;
}
update(arr, double);

function hello(teamMember) {
console.log("Hello " + teamMember);
}
function notifyPeople(team) {
for(teamMember of team) {
hello(teamMember);
}
}

notifyPeople(arr, hello)


function goodBye(teamMember) {
console.log('Goodbye ' + teamMember);
}
function notifyPeople(team, func ){
for(teamMember of team) {
func(teamMember);

}
}

notifyPeople(arr, goodBye);


let shoppingList = ['bananas', 'milk', 'bread'];
shoppingList.forEach(value => {
console.log(`We need to buy ${value}`);
});

let data = [1, 2, 3, 4, 5];
let newArr = data.map(value => value * 2);


console.log(newArr);
console.log(data);


let data1 = [1, 2, 3, 4, 5];
let evenNumbers = data.filter(value => {
return value % 2 === 0});
console.log(evenNumbers);
console.log(data1);

let data2 = [1, 2, 3, 4, 5];
let firstEvenNumber = data.find(value => value % 2 === 0);
console.log(firstEvenNumber);

let data3 = [1, 2, 3, 4, 5];
data
.filter(value => value % 2 === 0)
.map(value => value * 3)
.forEach(value => console.log(value));



let channels = ["bbc1", "BBC2", "ITV", "channel4",
"Channel5", "bbc3", "bbc4", "itv2", "ITV3", "itv4"];
console.log(channels);

channels.map(channel => channel.toUpperCase)
let newChannel = channels.map(item => item.toUpperCase());
let itvChannel = newChannel.filter(item => item.includes('ITV'))
newChannel.forEach(item => console.log(item));
202 changes: 179 additions & 23 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,192 @@
/*
By now, you would have already seen "undefined", either in an error message or being output from your program.
But what does it mean? undefined represents the absence of a value.
// /*
// By now, you would have already seen "undefined", either in an error message or being output from your program.
// But what does it mean? undefined represents the absence of a value.

In some cases, undefined will be used by a programmer intentionally, and they will write code to handle it.
But usually, when you see undefined - it means something has gone wrong!
// In some cases, undefined will be used by a programmer intentionally, and they will write code to handle it.
// But usually, when you see undefined - it means something has gone wrong!

Below are 4 typical examples of when you would see undefined.
For each example, can you explain why we are seeing undefined?
*/
// Below are 4 typical examples of when you would see undefined.
// For each example, can you explain why we are seeing undefined?
// */

// Example 1
let a;
console.log(a);
// // Example 1
// let a;
// console.log(a);
// //does not have assigned value//


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

// let hello = sayHello();
// console.log(hello);
// //a value was not returned//

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

// sayHelloToUser();
// //there is no any parameter when sayHelloToUser() is called//

// // Example 4
// let arr = [1,2,3];
// console.log(arr[3]);
// //there is no value in arr[3]//



// let i = 0;
// while (i < 3) {
// console.log(i);
// i++
// }

function sumTo(n) {
let sum = 0;
let i = 0;
while(i <= n) {
sum = sum + i;
i = i + 1;
}
return sum;

}
console.log(sumTo(3));


function showStocks(stocks){
if(stocks.lengths === 0) {
console.log("Empty Portfolio");
} else {
let i = 1;
while (i <= stocks.length) {
console.log(stocks[i - 1]);
i++;
}
}
}
let stocks = ["aapl","msft","amzn","googl",
"tsla"]
showStocks(stocks);


for(let i = 0; i < 3; i++) {
console.log(i);
}

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


// Example 3
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
function sumTo(n) {
let sum = 0;
for( let i = 0; i <= n; i = i + 1) {
sum = sum + i;

}
return sum;

}
function showStocks(stocks){
if(stocks.lengths === 0) {
console.log("Empty Portfolio");
} else {
for(let i = 0; i < stocks.length; i++) {
console.log(stocks[i]);

}
}
}
stocks = ["aapl","msft","amzn","googl",
"tsla"]
showStocks(stocks);


sayHelloToUser();
let colours = ["red", "green", "blue"];

for(let i = 0; i < colours.length; i++) {
let colour = colours[i];
console.log(colour);
}


let colors = ["red", "green", "blue"];
for(let color of colors) {
console.log(color);
}

// Example 4
let arr = [1,2,3];
console.log(arr[3]);
let phrase = "CodeYourFuture";
for(let letter of phrase) {
console.log(letter);
}


function calculatechanges(prices) {
let change = prices[prices.length - 1] - prices[0];
return change.toFixed(2);
}
function changeInPrices(closingPrices){
let changes = [];

for(let pricesForOneStock of closingPrices) {
let change = calculatechanges(pricesForOneStock);
changes.push(change);
}
return changes;
}

const closingPricesLast5Days = [
[179.19, 180.33, 176.28, 175.64, 172.99], // AAPL
[340.69, 342.45, 334.69, 333.20, 327.29], // MSFT
[3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN
[2951.88, 2958.13, 2938.33, 2928.30, 2869.45], // GOOGL
[1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA
];
let result = changeInPrices(closingPricesLast5Days);
console.log(result);


let fruits = ["orange", "apple", "banana"];
fruits.splice(1, 2, "pear");
console.log(fruits);
console.log(fruits.length)


let todo = ["order dog food", "do the dishes"];
todo.splice(1, 1, 'take out garbage');

console.log(fruits);

console.log(["orange", "apple", "banana"].sort());


let array = [1, 2, 3];
let firstNewArr = array.concat(4, 5, 6);
let secondNewArr = array.concat([4, 5, 6]);
console.log(firstNewArr);
console.log(secondNewArr);
console.log(array);


let arr = [1, 2, 3, 4, 5];
console.log(arr.slice(0, 3));
console.log(arr.slice(3));
console.log(arr.slice(1, -1));
console.log(arr);

let arra = [1, 3, 5];
console.log(arra.includes(2));
console.log(arra.includes(3));

let arry = ["orange", "apple", "banana"];
console.log(arry.join());
console.log(arry.join(' - '));

let names = ['khadija', 'tom'];
console.log(names.join());

function namesInArray(names) {

}
14 changes: 11 additions & 3 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
/*
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.
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
let result = [];
let i = 0;
while (i < n ) {
result.push(i * 2);
i++;
}
console.log(result.join());
}

evenNumbers(3); // should output 0,2,4
Expand Down
10 changes: 9 additions & 1 deletion 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ const BIRTHDAYS = [
];

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"
14 changes: 12 additions & 2 deletions 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@
*/

function evenNumbersSum(n) {
// TODO
}
let sum = 0;
let i = 0;
do{
sum += (i * 2);
i++;
} while (i < n );

return sum;

}



console.log(evenNumbersSum(3)); // should output 6
console.log(evenNumbersSum(0)); // should output 0
Expand Down
Loading