Skip to content

Commit 0ac1892

Browse files
JavaScript Function and Object
0 parents commit 0ac1892

File tree

9 files changed

+282
-0
lines changed

9 files changed

+282
-0
lines changed

function-examples.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// 5(Function Examples)
2+
3+
/*
4+
function functionName (parameters){
5+
function body
6+
return
7+
}
8+
var returnedValue = functionName(parameters value)
9+
*/
10+
11+
// Advance add operation by function
12+
13+
// function getAverage (assignment1, assignment2, assignment3){
14+
// const total = assignment1 + assignment2 + assignment3;
15+
// const average = total / 3;
16+
// return average;
17+
// }
18+
19+
// const assignment1Marks = 60;
20+
// const assignment2Marks = 58;
21+
// const assignment3Marks = 59;
22+
23+
// var myAverage = getAverage(assignment1Marks, assignment2Marks, assignment3Marks);
24+
// console.log('my average so far: ', myAverage);
25+
26+
function add(num1, num2){
27+
const sum = num1 + num2;
28+
return sum;
29+
}
30+
31+
const result1 = add(12, 13);
32+
const result2 = add(35, 7);
33+
const finalResult = add(result1, result2);
34+
console.log(finalResult);

function.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// 1(Introduction and review of previous)
2+
// 2(function declaration)
3+
4+
function startFan(){
5+
console.log('Stand up');
6+
console.log('walk towards the switch');
7+
console.log('Press the switch');
8+
}
9+
10+
// calling the function
11+
startFan();
12+
console.log('Waking up in the morning')
13+
startFan();
14+
console.log('Eating lunch');
15+
console.log('Watching JS tutorial')
16+
startFan();

object-looping.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// 8(Object Looping)
2+
3+
var shoppingCart = {
4+
books: 3,
5+
sunglass: 1,
6+
keyboard: 5,
7+
mouse: 1,
8+
pen: 25,
9+
shoes: 2
10+
}
11+
12+
const keys = Object.keys(shoppingCart);
13+
// console.log(keys);
14+
15+
// for(var i = 0; i < keys.length; i++){
16+
// var propertyName = keys[i];
17+
// var propertyValue = shoppingCart[propertyName];
18+
// console.log(propertyName, propertyValue);
19+
// }
20+
21+
// for in loop
22+
for(var propertyName in shoppingCart){
23+
const value = shoppingCart[propertyName];
24+
console.log(propertyName, value);
25+
}

object.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var student = {
2+
id: 115,
3+
name: 'Solamin Khan',
4+
class: 9,
5+
marks: 98
6+
}
7+
8+
var mobile = {
9+
brand: 'Samsung',
10+
price: 19000,
11+
storage: '64gb',
12+
camera: '7MP'
13+
}
14+
15+
16+
var myComputer = {
17+
brand: 'lonovo',
18+
price: 39000,
19+
color: 'silver',
20+
processor: 'i7'
21+
}
22+
23+
console.log(myComputer.processor);
24+
myComputer.processor = 'i79';
25+
console.log(myComputer)

parameter.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// 3(Function Parameter)
2+
3+
// function bringSingara(money){
4+
// console.log('taka disen: ', money);
5+
// console.log('ai nen singara');
6+
// }
7+
8+
// var taka = 300;
9+
// bringSingara(taka);
10+
11+
// function add(num1, num2){
12+
// console.log('going to add:', num1, num2);
13+
// }
14+
15+
// add(125, 96);
16+
17+
function sum(a, b, c, d, e){
18+
console.log(a, b, c, d, e);
19+
var sum = a + b + c + d + e;
20+
console.log(sum);
21+
}
22+
23+
sum(15, 98, 56, 5, 9);

practice.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Practice Tasks
2+
3+
// Task-1: Write a function called foo() which prints "foo" and a function called bar() which prints "bar". Call function bar() in the foo() function after printing. What will be the output? Now call the foo() to see the output.
4+
// function foo() {
5+
// console.log("foo");
6+
// bar();
7+
// }
8+
// foo();
9+
10+
// function bar() {
11+
// console.log("bar");
12+
// }
13+
// bar();
14+
15+
16+
// Task-2: Write function called make_avg() which will take an three integers and return the average of those value.
17+
// function make_avg(int1, int2, int3) {
18+
// const total = int1 + int2 + int3;
19+
// const average = total / 3;
20+
// return average;
21+
// }
22+
// const makeAverage = make_avg(60, 60, 60);
23+
// console.log(makeAverage);
24+
25+
// Task-3: Write a function called make_avg which will take an array of integers and the size of that array and return the average of that value.
26+
// const array = [51, 52, 53, 54, 55, 56, 57];
27+
// function make_avg(nums) {
28+
// let sum = 0;
29+
// for (let i = 0; i < array.length; i++) {
30+
// const element = array[i];
31+
// sum += element;
32+
// }
33+
// const average = sum / array.length;
34+
// return average;
35+
// }
36+
// var averageFinder = make_avg(array);
37+
// console.log(averageFinder);
38+
39+
// Task-4: Write a function called odd_even() which takes an integer value and tells whether this value is even or odd. Condition: "Has return + Has parameter" and "No return + Has parameter"
40+
// function odd_even(number) {
41+
// if (number % 2 === 0){
42+
// return "Even number";
43+
// }
44+
// return "Odd number";
45+
// }
46+
// var isEvenOrOdd = odd_even(37);
47+
// console.log(isEvenOrOdd);
48+
49+
// Without return
50+
// function odd_even(number) {
51+
// if (number % 2 == 0){
52+
// console.log("Even number");
53+
// }
54+
// else if (number % 2 == 1){
55+
// console.log("Odd number")
56+
// }
57+
// }
58+
// odd_even(36);
59+
60+
// Task-5: Write a JS switch on traffic lights signal direction
61+
// const signal = 'green';
62+
// switch (signal){
63+
// case 'red':
64+
// console.log('Danger');
65+
// break;
66+
// case 'yellow':
67+
// console.log('Stop');
68+
// break;
69+
// case 'green':
70+
// console.log('Go');
71+
// break;
72+
// default:
73+
// console.log('N/A');
74+
// }

properties.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 7(Object Properties/Keys and Values)
2+
3+
var shoppingCart = {
4+
books: 3,
5+
sunglass: 1,
6+
keyboard: 5,
7+
mouse: 1,
8+
pen: 25
9+
}
10+
11+
// 3 way to get object property value
12+
// var penCount = shoppingCart.pen;
13+
// console.log(penCount);
14+
15+
// var penCount2 = shoppingCart['pen'];
16+
// console.log(penCount2);
17+
18+
var propertyName = 'mouse';
19+
// var mouseValue = shoppingCart[propertyName];
20+
// console.log(propertyName, mouseValue);
21+
22+
// Get object property and value by using JS reserve keywords
23+
// var properties = Object.keys(shoppingCart);
24+
// console.log(properties);
25+
26+
// var propertyValues = Object.values(shoppingCart);
27+
// console.log(propertyValues);
28+
29+
// 3 way to change/set property values
30+
shoppingCart.mouse = 15;
31+
console.log(shoppingCart);
32+
33+
shoppingCart['mouse'] = 29;
34+
console.log(shoppingCart);
35+
36+
shoppingCart[propertyName] = 89;
37+
console.log(shoppingCart)

return.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// 4(Function Return)
2+
3+
function add(number1, number2){
4+
console.log(number1, number2);
5+
var sum = number1 + number2;
6+
// console.log(sum);
7+
return sum;
8+
console.log('I need more code');
9+
return 15;
10+
return 'hello done';
11+
return 'I am hungry';
12+
}
13+
14+
// add(45, 15);
15+
16+
var total = add(80, 20);
17+
// console.log('total', total);
18+
19+
function bringSingara(money){
20+
var singaraPrice = 10;
21+
var quantity = money / singaraPrice;
22+
return quantity;
23+
}
24+
25+
var myTaka = 150
26+
var singaras = bringSingara(myTaka);
27+
console.log('Eating singaras: ', singaras);

switch.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// 9(JavaScript switch)
2+
3+
switch(color){
4+
case 'green':
5+
console.log('You are a green friend');
6+
break;
7+
case 'blue':
8+
console.log('You are a BLUE friend');
9+
break;
10+
case 'white':
11+
console.log('You are a white friend');
12+
break;
13+
case 'red':
14+
console.log('You are a red friend');
15+
break;
16+
case 'yellow':
17+
console.log('You are a himu friend');
18+
break;
19+
default:
20+
console.log('You are a kala kala friend');
21+
}

0 commit comments

Comments
 (0)