Q13. Write syntax of and explain prompt method in JavaScript with the help of suitable example.
Answer:
Syntax:
prompt(text, defaultText)
It displays a dialog box prompting the user for input.
Example:
let userName = prompt("Enter your name:", "Guest");
alert("Hello " + userName);
---
Q14. Write a JavaScript that displays all properties of window object. Explain the code.
Answer:
for (let prop in window) {
console.log(prop);
This loops through all properties of the window object and displays them.
The window object represents the browser window and includes properties like document, navigator,
location, etc.
---
Q15. Write a JavaScript function that checks whether a passed string is palindrome or not.
Answer:
function isPalindrome(str) {
let reversed = str.split('').reverse().join('');
return str === reversed;
let input = prompt("Enter a string:");
if (isPalindrome(input)) {
alert("It is a palindrome.");
} else {
alert("It is not a palindrome.");
---
Q16. Describe all the tokens of the following statements: document.bgColor and document.write()
Answer:
document: The object representing the web page.
bgColor: Property to set background color of the document.
write(): Method to write text into the HTML document.
---
Q17. Differentiate between prompt() and alert() methods.
Answer:
| prompt() | alert() |
|:--------------------------------------|:-----------------------------------|
| Asks the user to input some information. | Only displays a message to the user. |
| Returns the entered value. | Does not return anything. |
| Example: prompt("Enter name:") | Example: alert("Welcome!") |
---
Q18. State use of getters and setters.
Answer:
Getters are used to get the property value.
Setters are used to set a property value.
Example:
let person = {
firstName: "John",
lastName: "Doe",
get fullName() {
return this.firstName + " " + this.lastName;
},
set fullName(name) {
[this.firstName, this.lastName] = name.split(" ");
};
console.log(person.fullName); // John Doe
person.fullName = "Jane Smith";
console.log(person.firstName); // Jane
---
Q19. Write a JavaScript that displays first 20 even numbers on the document window.
Answer:
for (let i = 2; i <= 40; i += 2) {
document.write(i + "<br>");
---
Q20. Write a program to print sum of even numbers between 1 to 100 using for loop.
Answer:
let sum = 0;
for (let i = 2; i <= 100; i += 2) {
sum += i;
console.log("Sum of even numbers between 1 and 100:" + sum);