0% found this document useful (0 votes)
4 views

JavaScript QA (1)

The document provides various JavaScript examples and explanations, including the usage of the prompt method for user input, properties of the window object, and a function to check for palindromes. It also differentiates between prompt() and alert() methods, discusses getters and setters, and includes examples of displaying even numbers and calculating their sum. Overall, it serves as a tutorial on fundamental JavaScript concepts and methods.

Uploaded by

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

JavaScript QA (1)

The document provides various JavaScript examples and explanations, including the usage of the prompt method for user input, properties of the window object, and a function to check for palindromes. It also differentiates between prompt() and alert() methods, discusses getters and setters, and includes examples of displaying even numbers and calculating their sum. Overall, it serves as a tutorial on fundamental JavaScript concepts and methods.

Uploaded by

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

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);

You might also like