|
| 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 | +// } |
0 commit comments