-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbasicprogram.js
More file actions
366 lines (253 loc) · 7.72 KB
/
Copy pathbasicprogram.js
File metadata and controls
366 lines (253 loc) · 7.72 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// 1. Using console.log()
// the hello world program
console.log('Hello World');
// 2. Using alert()
// the hello world program
alert("Hello, World!");
// 3. Using document.write()
// the hello world program
document.write('Hello, World!');
// Example 1: Add Two Numbers
const num1 = 5;
const num2 = 3;
// add two numbers
const sum = num1 + num2;
// display the sum
console.log('The sum of ' + num1 + ' and ' + num2 + ' is: ' + sum);
// Example 2: Add Two Numbers Entered by the User
// store input numbers
const num3 = parseInt(prompt('Enter the first number '));
const num4 = parseInt(prompt('Enter the second number '));
// display the sum
console.log(`The sum of ${num1} and ${num2} is ${sum}`);
// Example 1: subtract Two Numbers
const numb1 = 5;
const numb2 = 3;
// subtract two numbers
const sub = numb1 - numb2;
// Example: Square Root of a Number
// take the input from the user
const numberr = prompt('Enter the number: ');
const result = Math.sqrt(numberr);
console.log(`The square root of ${numberr} is ${result}`);
// Example 2: Square Root of Different Data Types
const number1 = 2.25;
const number2 = -4;
const number3 = 'hello';
const result1 = Math.sqrt(number1);
const result2 = Math.sqrt(number2);
const result3 = Math.sqrt(number3);
console.log(`The square root of ${number1} is ${result1}`);
console.log(`The square root of ${number2} is ${result2}`);
console.log(`The square root of ${number3} is ${result3}`);
// Example 1: Area When Base and Height is Known
const baseValue = prompt('Enter the base of a triangle: ');
const heightValue = prompt('Enter the height of a triangle: ');
// calculate the area
const areasValue = (baseValue * heightValue) / 2;
console.log(
`The area of the triangle is ${areasValue}`
);
// Example 2: Area When All Sides are Known
// JavaScript program to find the area of a triangle
const side1 = parseInt(prompt('Enter side1: '));
const side2 = parseInt(prompt('Enter side2: '));
const side3 = parseInt(prompt('Enter side3: '));
// calculate the semi-perimeter
const s = (side1 + side2 + side3) / 2;
//calculate the area
const areaValue = Math.sqrt(
s * (s - side1) * (s - side2) * (s - side3)
);
console.log(
`The area of the triangle is ${areaValue}`
);
// Example 1: Using a Temporary Variable
//JavaScript program to swap two variables
//take input from the users
let aa = prompt('Enter the first variable: ');
let bb = prompt('Enter the second variable: ');
//create a temporary variable
let temp;
//swap variables
temp = aa;
aa = bb;
bb = temp;
console.log(`The value of a after swapping: ${aa}`);
console.log(`The value of b after swapping: ${bb}`);
// Example: Roots of a Quadratic Equation
// program to solve quadratic equation
let root1, root2;
// take input from the user
let a = prompt("Enter the first number: ");
let b = prompt("Enter the second number: ");
let c = prompt("Enter the third number: ");
// calculate discriminant
let discriminant = b * b - 4 * a * c;
// condition for real and different roots
if (discriminant > 0) {
root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
// result
console.log(`The roots of quadratic equation are ${root1} and ${root2}`);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
// result
console.log(`The roots of quadratic equation are ${root1} and ${root2}`);
}
// if roots are not real
else {
let realPart = (-b / (2 * a)).toFixed(2);
let imagPart = (Math.sqrt(-discriminant) / (2 * a)).toFixed(2);
// result
console.log(
`The roots of quadratic equation are ${realPart} + ${imagPart}i and ${realPart} - ${imagPart}i`
);
}
//Example 1: Kilometers to Miles
// taking kilometers input from the user
const kilometers = prompt("Enter value in kilometers: ")
// conversion factor
const factor = 0.621371
// calculate miles
const miles = kilometers * factor
console.log(`${kilometers} kilometers is equal to ${miles} miles.`);
//Example: Celsius to Fahrenheit
// program to convert celsius to fahrenheit
// ask the celsius value to the user
const celsius = prompt("Enter a celsius value: ");
// calculate fahrenheit
const fahrenheit = (celsius * 1.8) + 32
// display the result
console.log(`${celsius} degree celsius is equal to ${fahrenheit} degree fahrenheit.`);
// Example 1: Using if...else
// program to check if the number is even or odd
// take input from the user
let num = prompt("Enter a number: ");
//check if the number is even
if(num % 2 == 0) {
console.log("The number is even.");
}
// Example: Check Prime Number
// if the number is odd
else {
console.log("The number is odd.");
}
// Example 2: Using Ternary Operator
// program to check if the number is even or odd
// take input from the user
const numm = prompt("Enter a number: ");
// ternary operator
const res = (numm % 2 == 0) ? "even" : "odd";
// display the result
console.log(`The number is ${res}.`);
// Example: Check Prime Number
// program to check if a number is prime or not
// take input from the user
const number = parseInt(prompt("Enter a positive number: "));
let isPrime = true;
// check if number is equal to 1
if (number === 1) {
console.log("1 is neither prime nor composite number.");
}
// check if number is greater than 1
else if (number > 1) {
// looping through 2 to number-1
for (let i = 2; i < number; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
console.log(`${number} is a prime number`);
} else {
console.log(`${number} is a not prime number`);
}
}
// check if number is less than 1
else {
console.log("The number is not a prime number.");
}
// Example: Print Prime Numbers
// program to print prime numbers between the two numbers
// take input from the user
const lowerNumber = parseInt(prompt('Enter lower number: '));
const higherNumber = parseInt(prompt('Enter higher number: '));
console.log(`The prime numbers between ${lowerNumber} and ${higherNumber} are:`);
// looping from lowerNumber to higherNumber
for (let i = lowerNumber; i <= higherNumber; i++) {
let flag = 0;
// looping through 2 to user input number
for (let j = 2; j < i; j++) {
if (i % j == 0) {
flag = 1;
break;
}
}
// if number greater than 1 and not divisible by other numbers
if (i > 1 && flag == 0) {
console.log(i);
}
}
//Example: Convert Date to Number
// program to convert date to number
// create date
const d1 = new Date();
console.log(d1);
// converting to number
const results = d1.getTime();
console.log(results);
// program to remove item from an array
function removeItemFromArray(array, n) {
const newArray = [];
for ( let i = 0; i < array.length; i++) {
if(array[i] !== n) {
newArray.push(array[i]);
}
}
return newArray;
}
const result = removeItemFromArray([1, 2, 3 , 4 , 5], 2);
console.log(result);
// program to loop through an object using for...in loop
const student = {
name: 'John',
age: 20,
hobbies: ['reading', 'games', 'coding'],
};
// using for...in
for (let key in student) {
let value;
// get the value
value = student[key];
console.log(key + " - " + value);
}
// program to merge property of two objects
// object 1
const person = {
name: 'Jack',
age:26
}
// object 2
const student = {
gender: 'male'
}
// merge two objects
const newObj = Object.assign(person, student);
console.log(newObj);
// program to merge property of two objects
// object 1
const person = {
name: 'Jack',
age:26
}
// object 2
const student = {
gender: 'male'
}
// merge two objects
const newObj = {...person, ...student};
console.log(newObj);