-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment.js
More file actions
238 lines (151 loc) · 4.05 KB
/
Copy pathassignment.js
File metadata and controls
238 lines (151 loc) · 4.05 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* Q1. write a javascript program to find the area of a rectangle using a function. where users can enter the value run time and see the result in the browser */
//A1:-
//declare variables
/*
let width = 10; //prompt concept
let length;
//create a function
function Area(width = 4, length = 6){ //default assignment
return (width * length);
}
//call
console.log("area of rectangle is" + " " + Area(width, length) );
*/
/* Q2. write a program to print the below pattern of stars.
*
**
***
****
***** */
//A2:-
/*
for (var i=1; i<=5; i++){
console.log("*".repeat(i));
}
*/
//using repeat() method to construct and return specified copies, concatenated together.
//A2:-
/*
let i,j;
for(i=1; i<6; i++){
for(j=1; j<=i; j++){
}
console.log("* ".repeat(i));
}
*/
/* Q3. write a program to print the pyramid of stars. */
//A3.
function pyramid(n){
for(let i=1; i<=n; i++){
let str =" ".repeat(n-i);
let str2 ="*".repeat(i*2 -1)
console.log(str + str2 + str);
}
}
pyramid(5);
/* Q4. write a program named displayname() which will print your name. but this function will pass as a callback to another function named main() pass your name from main() and argument and print in the console via displayname(). */
//A4.
/*
function main(firstname, lastname){
console.log("hello, my name is " + firstname + " " + lastname);
}
//callback function
function displayname(){
main("vinay", "choudhary");
}
displayname();
*/
/* Q5. what is the output of below code and why? */
//A5.
/*
let obj1 = { name: "Monday" };
let obj2 = { name: "Monday" };
console.log(obj1 == obj2);
*/
//Output = False
//1. obj1 and obj2 are in global frame.
/* Q6. What is the output of below code and why? */
//A6.
/*
function display(MyName)
{
let userName = MyName || "CK";
console.log(userName);
return true
}
display('Yuvraj')
*/
//Output = yuvraj
//1. display() points console.log(username)(101)
//2. myname is assign to "yuvraj" and username is also "yuvraj"(97,98)
/* Q7. Explain the output of the below code. */
//A7.
/*
for (let i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
console.log(i);
for (let i = 0; i <= 3; i++) {
if (i == 2) {
break;
}
console.log(`i:${i}`);
}
}
*/
/*
Output = 0
i:0
i:1
1
i:0
i:1
3
i:0
i:1
4
i:0
i:1
*/
//1. it will print zero from the first for loop, console.log(i).
//2. it will print i:0, i:1 from the second for loop, console.log(`i:${i}`) and continue till if condition(i==2)(SECOND IF CONDITION) and breaks the loop and will move to the first loop again.
//3. same process for each until it reaches the (i==2)(FIRST IF CONDITION) condition and will CONTINUE THE LOOP AGAIN FROM START and after the execution of continue statement the loop will print same for 3 and 4 and the execution will be terminated as i<5.
/* Q8. - Explain the output of the below code. */
//A8.
/*
let value = 0;
if (-2) {
var value = 10;
}
console.log(value);
*/
//Output = cannot execute(SyntaxError)
//1. Identifier 'value'(157) has already beed declared at(155).
//2. same identifier clash between global and block scope.
/* Q9. Explain the output of the below code. */
//A9.
/*
console.log(add);
var add = function (a, b) {
const results = a + b;
return results;
};
*/
//Output = Undefined
//1. at console.log add is undefined.
/* Q10. Explain the result of the below codes? */
//A10.
/*
let results = ((4 + 5) / 3) ** 2
console.log(results)
let result = NaN && null || '0' && 0
console.log(result)
*/
//console.log(0 === 0);
//Output = 9,0
//1. Operator Precedence...
//2. ** is is exponentiation
//3. the output 9 is from the first two lines(187,188) i.e (4+5) is 9 and 9/3 is 3 and 3**2 is 9.
//4. compare all one by one(189) i.e NaN == NaN is false and NaN === NaN is false, null == null is true and null === null is true, '0' == '0' is true and '0' === '0' is true, 0 == 0 is true and 0 === 0 is true.
//5. the &&(AND) operator returns is left side value and the ||(OR) operator returns the right side value.