forked from Asabeneh/JavaScript-for-Everyone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
20-functions.js
67 lines (57 loc) · 1.69 KB
/
20-functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// function without parameters
function addTwoNumbers() {
var numOne = 10;
var numTwo = 20;
var sum = numOne + numTwo;
console.log(sum);
}
addTwoNumbers(); // function has to be called to be executed
// Function without parater doesn' take input, so lets make a parameter with parameter
function sumTwoNumbers(numOne, numTwo) {
var sum = numOne + numTwo;
console.log(sum);
}
sumTwoNumbers(10, 20); // calling functions
// If a function doesn't return it doesn't store data, so it should return
function sumTwoNumbersAndReturn(numOne, numTwo) {
var sum = numOne + numTwo;
return sum;
}
console.log(sumTwoNumbersAndReturn(10, 20));
function printFullName(firstName, lastName) {
return `${firstName} ${lastName}`;
}
console.log(printFullName("Asabeneh", "Yetayeh"));
console.log(printFullName("Dean", "Phan"));
function square(number) {
return number * number;
}
console.log(square(10));
// this function takes array as a parameter and sum up the numbers in the array
function sumArrayValues(arr) {
var sum = 0;
for (var i = 0; i < arr.length; i++) {
sum = sum + numbers[i];
}
return sum;
}
const numbers = [1, 2, 3, 4, 5];
console.log(sumArrayValues(numbers));
//
function sumOfEvensAndOdds() {
var sumOfEvens = 0;
var sumOfOdds = 0;
for (var i = 0; i <= 100; i++) {
if (i % 2 === 0) {
sumOfEvens = sumOfEvens + i;
console.log('I am proudly even', i);
} else {
sumOfOdds = sumOfOdds + i;
console.log('I am a proud odd number', i);
}
}
return { evens: sumOfEvens, odds: sumOfOdds };
//return [sumOfEvens, sumOfOdds];
//return `The sum of all evens is ${sumOfEvens}. And the sum of all odds is ${sumOfOdds}`;
}
console.log(sumOfEvensAndOdds());