0% found this document useful (0 votes)
7 views6 pages

Js QB

The document provides a series of JavaScript programming exercises covering basic concepts such as loops, conditionals, objects, and functions. It includes example code snippets for tasks like printing even numbers, checking for prime numbers, and calculating factorials. Additionally, it explains key features of JavaScript and demonstrates the use of various operators and expressions.

Uploaded by

virajgadhe07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Js QB

The document provides a series of JavaScript programming exercises covering basic concepts such as loops, conditionals, objects, and functions. It includes example code snippets for tasks like printing even numbers, checking for prime numbers, and calculating factorials. Additionally, it explains key features of JavaScript and demonstrates the use of various operators and expressions.

Uploaded by

virajgadhe07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT I – Basics of JavaScript Programming

4 Marks Questions

Q1. Write a JavaScript program to print even numbers from 1 to 10 using a loop.
<!DOCTYPE html>
<html>
<body>
<h3>Even Numbers from 1 to 10</h3>
<script>
for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) {
document.write(i + "<br>");
}
}
</script>
</body>
</html>

Output :-
2
4
6
8
10

Q2. Differentiate between if, if...else, and switch statements with syntax.
Statement Description Syntax Example
if Executes code if condition is true if(condition){...}
if...else Executes one block if true, another if false if(condition){...} else {...}
switch Selects one case based on a value switch(value){ case ... }
Example:
// if
if (x > 0) console.log("Positive");

// if...else
if (x % 2 === 0) console.log("Even");
else console.log("Odd");

// switch
switch(day) {
case 1: console.log("Monday"); break;
case 2: console.log("Tuesday"); break;
default: console.log("Other Day");
}

Output :-
Positive
Even
Tuesday

Q3. Key features of JavaScript (with examples):


1. Lightweight – Runs quickly in browsers.
2. Cross-platform – Works on all OS.
3. Object-oriented – Uses objects and methods.
4. Event-driven – Responds to user actions.
5. Functional – Functions are first-class citizens.
Example:
document.getElementById("btn").onclick = function() {
alert("Button Clicked!");
};
Output :-
(When you click the button, an alert pops up saying "Button Clicked!")

Q4. Create car object, update a property, delete a property.


<script>
let car = {
brand: "Toyota",
model: "Corolla",
year: 2020,
display: function() {
document.write(this.brand + " " + this.model);
}
};
car.year = 2022; // update
delete car.model; // delete
car.display();
</script>
Output :-
Honda undefined

Q5. Largest of three numbers using if...else if...else:


<script>
let a = 10, b = 20, c = 15;
if(a > b && a > c) document.write(a + " is largest");
else if(b > a && b > c) document.write(b + " is largest");
else document.write(c + " is largest");
</script>
Output :-
20 is largest

Q6. Even or Odd using if...else:


<script>
let num = 5;
if(num % 2 === 0) document.write("Even");
else document.write("Odd");
</script>
Output :- Odd

Q7. Syntax of switch with example:


switch(expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
Example:
let day = 2;
switch(day) {
case 1: console.log("Monday"); break;
case 2: console.log("Tuesday"); break;
default: console.log("Other day");
}
Output :- Tuesday

Q8. Largest of three numbers using nested if...else:


<script>
let a = 5, b = 8, c = 3;
if(a > b) {
if(a > c) document.write(a);
else document.write(c);
} else {
if(b > c) document.write(b);
else document.write(c);
}
</script>
Output :- 8

Q9. Declare and access object properties using dot notation:


let person = { name: "John", age: 30 };
console.log(person.name);
console.log(person.age);
Output :-
John
30

Q10. for...in loop to iterate through object properties:


<script>
let student = { name: "Alice", age: 20, grade: "A" };
for (let key in student) {
document.write(key + ": " + student[key] + "<br>");
}
</script>
Output :-
name: Alice
age: 20
grade: A

6 Marks Questions

Q1. Write a JavaScript program to input a number and check whether it is prime using loops.
<!DOCTYPE html>
<html>
<body>
<script>
let num = parseInt(prompt("Enter a number:"));
let isPrime = true;

if (num <= 1) {
isPrime = false;
} else {
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
isPrime = false;
break;
}
}
}
document.write(isPrime ? "Prime" : "Not Prime");
</script>
</body>
</html>
Output :- Prime

Q2. Factorial using for, while, and do...while:


<script>
// For loop
let n = 5, fact = 1;
for(let i=1; i<=n; i++) fact *= i;
document.write("Factorial (for): " + fact + "<br>");

// While loop
fact = 1; let i = 1;
while(i <= n) { fact *= i; i++; }
document.write("Factorial (while): " + fact + "<br>");

// Do...while loop
fact = 1; i = 1;
do { fact *= i; i++; } while(i <= n);
document.write("Factorial (do...while): " + fact);
</script>
Output :-
Factorial (for): 120
Factorial (while): 120
Factorial (do...while): 120

Q3. Multiplication table of N (1 to 10):


<script>
let N = 7;
for(let i = 1; i <= 10; i++) {
document.write(N + " x " + i + " = " + (N*i) + "<br>");
}
</script>
Output :-
7 x 1 = 7
7 x 2 = 14
...
7 x 10 = 70

Q4. Arithmetic, relational, and logical operators (theory + example):


 Arithmetic: +, -, *, /, %, **
Example: let sum = a + b;
 Relational: >, <, >=, <=, ==, !=, ===, !==
Example: if(a > b) {...}
 Logical: &&, ||, !
Example: if(a > 0 && b > 0) {...}
let a = 10, b = 5;
console.log(a + b); // arithmetic
console.log(a > b); // relational
console.log(a > 0 && b > 0); // logical
Output :-
15
true
true

Q5. Types of expressions in JavaScript:


1. Arithmetic Expression → x + y
2. String Expression → "Hello" + "World"
3. Logical Expression → a && b
4. Assignment Expression → x = 5
5. Function Expression → let add = function(a,b){return a+b;}
Output :-
Honda Corolla

Q6. car object with getter and setter:


<script>
let car = {
brand: "Toyota",
model: "Corolla",
get fullName() {
return this.brand + " " + this.model;
},
set updateBrand(newBrand) {
this.brand = newBrand;
}
};
car.updateBrand = "Honda";
document.write(car.fullName);
</script>
Output :-
Honda Corolla

Q7. Sum of first 10 natural numbers using a loop:


<script>
let sum = 0;
for(let i = 1; i <= 10; i++) sum += i;
document.write("Sum: " + sum);
</script>
Output :-
Sum: 55
Q8. Factorial using loop (simpler version):
<script>
let n = 5, fact = 1;
for(let i=1; i<=n; i++) fact *= i;
document.write("Factorial: " + fact);
</script>
Output :-
Factorial: 120

You might also like