Javascript Question
Javascript Question
each one covers. These problems will help you build a foundation for web development using
JavaScript.
11. **Create an array** of numbers and print each number using a `for` loop.
17. Write a loop that **counts from 1 to 100**, but prints "Fizz" for multiples of 3, "Buzz" for
multiples of 5, and "FizzBuzz" for multiples of both.
19. Write a function that **removes the last element** from an array.
30. Write a function that **capitalizes the first letter** of each word in a sentence.
31. **Create a simple object** with properties `name` and `age`. Print the name property.
33. Create a function that **loops through an object** and logs each key and value.
35. **Access an HTML element** by its `id` and change its text content.
36. **Create a button** that changes the background color of the page when clicked.
37. Write a function to **add a new list item** to an unordered list (`<ul>`) in HTML.
39. Create an input field and a button. Write a function that **displays the input value** when the
button is clicked.
40. **Change the image source** (`src`) of an image tag using JavaScript.
41. **Add an event listener** to a button that logs "Button clicked!" when clicked.
42. Write a function that **prevents a form from submitting** when the submit button is clicked.
43. **Create a dropdown menu** that displays an alert with the selected option.
44. Write a function to **toggle the visibility** of an element when a button is clicked.
46. Create a button that, when clicked, **adds a new paragraph** to the page.
47. Write a function that **changes the text color** of all paragraphs to red when clicked.
48. **Change the content** of an `<h1>` tag when the mouse hovers over it.
49. **Disable a button** after it has been clicked once.
50. Create a form that requires a valid email address. **Show an error message** if the email is not
valid.
1. Try solving each problem on your own before checking any solution.
3. Test your code in a browser’s developer console or in an online code editor like CodePen or
JSFiddle.
This set of problems covers basic concepts in JavaScript like variables, arrays, loops, functions,
objects, and DOM manipulation, helping you get a solid start in web development.
Here’s a list of simple, practical tasks you can perform on a webpage using JavaScript, along with
their solutions. These will help you understand how to manipulate HTML elements, handle events,
and perform basic operations.
Level -01
1. **Question**: Change the text of a heading (`<h1>` tag) when the page loads.
- **Answer**:
```javascript
```
- **Answer**:
```javascript
document.body.style.backgroundColor = 'lightblue';
```
3. **Question**: Add a new paragraph to the webpage.
- **Answer**:
```javascript
let p = document.createElement('p');
document.body.appendChild(p);
```
- **Answer**:
```javascript
paragraphs[i].style.color = 'red';
```
- **Answer**:
```javascript
document.getElementById('myButton').onclick = function() {
document.getElementById('myImage').style.display = 'none';
};
```
- **Answer**:
```javascript
document.getElementById('alertButton').onclick = function() {
alert('Button clicked!');
};
```
7. **Question**: Add a list item (`<li>`) to an existing list when a button is clicked.
- **Answer**:
```javascript
document.getElementById('addButton').onclick = function() {
let li = document.createElement('li');
document.getElementById('myList').appendChild(li);
};
```
- **Answer**:
```javascript
document.getElementById('toggleButton').onclick = function() {
para.style.display = 'block';
} else {
para.style.display = 'none';
};
```
- **Answer**:
```javascript
document.getElementById('changeImage').onclick = function() {
document.getElementById('myImage').src = 'new-image.jpg';
};
```
- **Answer**:
```javascript
document.getElementById('myButton').onclick = function() {
this.disabled = true;
};
```
11. **Question**: Create a simple form that displays the user’s input in an alert box after
submission.
- **Answer**:
```javascript
document.getElementById('submitForm').onclick = function() {
};
```
12. **Question**: Change the font size of a paragraph when a button is clicked.
- **Answer**:
```javascript
document.getElementById('fontButton').onclick = function() {
document.getElementById('myParagraph').style.fontSize = '24px';
};
```
13. **Question**: Make an element change color when the mouse hovers over it.
- **Answer**:
```javascript
document.getElementById('hoverDiv').onmouseover = function() {
this.style.backgroundColor = 'yellow';
};
document.getElementById('hoverDiv').onmouseout = function() {
this.style.backgroundColor = '';
};
```
14. **Question**: Count the number of clicks on a button and display the count.
- **Answer**:
```javascript
let count = 0;
document.getElementById('clickButton').onclick = function() {
count++;
};
```
15. **Question**: Display the current date and time in a `<div>` when a button is clicked.
- **Answer**:
```javascript
document.getElementById('timeButton').onclick = function() {
document.getElementById('timeDiv').textContent = now;
};
```
- **Answer**:
```javascript
document.getElementById('changeTextButton').onclick = function() {
this.textContent = 'Clicked!';
};
```
17. **Question**: Show an alert box when a user hovers over a specific text.
- **Answer**:
```javascript
document.getElementById('hoverText').onmouseover = function() {
};
```
18. **Question**: Change the border color of an input field when it is focused.
- **Answer**:
```javascript
document.getElementById('myInput').onfocus = function() {
this.style.borderColor = 'blue';
};
document.getElementById('myInput').onblur = function() {
this.style.borderColor = '';
};
```
19. **Question**: Change the background color of the page when a checkbox is checked.
- **Answer**:
```javascript
document.getElementById('myCheckbox').onchange = function() {
if (this.checked) {
document.body.style.backgroundColor = 'lightgreen';
} else {
document.body.style.backgroundColor = '';
};
```
20. **Question**: Create a button that hides a list when clicked, and shows it again when clicked a
second time.
- **Answer**:
```javascript
document.getElementById('toggleList').onclick = function() {
list.style.display = 'block';
} else {
list.style.display = 'none';
};
```
21. **Question**: Display the value of a text input field in a paragraph when a button is clicked.
- **Answer**:
```javascript
document.getElementById('displayButton').onclick = function() {
document.getElementById('displayParagraph').textContent = inputValue;
};
```
22. **Question**: Validate if an email input field contains a valid email address.
- **Answer**:
```javascript
document.getElementById('validateButton').onclick = function() {
};
```
### These simple tasks will help you understand how to manipulate HTML elements, handle events,
and create interactive webpages using JavaScript. Let me know if you’d like more!
Level -2
Here are 30 medium-level JavaScript questions that involve more complex DOM manipulation, form
validation, events, and user interaction. These will help you take your JavaScript skills to the next
level.
1. **Question**: Create a button that, when clicked, creates a new `<div>` element with a random
background color.
- **Answer**:
```javascript
document.getElementById('createDivButton').onclick = function() {
newDiv.style.width = '100px';
newDiv.style.height = '100px';
document.body.appendChild(newDiv);
};
```
2. **Question**: Create a simple to-do list where a user can add and remove tasks.
- **Answer**:
```javascript
document.getElementById('addTaskButton').onclick = function() {
if (task) {
let li = document.createElement('li');
li.textContent = task;
li.onclick = function() {
li.remove();
};
document.getElementById('taskList').appendChild(li);
};
```
3. **Question**: Create a button that, when clicked, alternates between showing and hiding a
specific section.
- **Answer**:
```javascript
document.getElementById('toggleSectionButton').onclick = function() {
};
```
4. **Question**: Write a program that changes the color of the text based on the user’s selection
from a dropdown menu.
- **Answer**:
```javascript
document.getElementById('colorDropdown').onchange = function() {
document.getElementById('textToChange').style.color = selectedColor;
};
```
5. **Question**: Create a form that dynamically updates the content of a paragraph with the value
of an input field as the user types.
- **Answer**:
```javascript
document.getElementById('dynamicInput').oninput = function() {
document.getElementById('dynamicText').textContent = this.value;
};
```
6. **Question**: Create a button that, when clicked, moves an image to a random position on the
page.
- **Answer**:
```javascript
document.getElementById('moveImageButton').onclick = function() {
image.style.position = 'absolute';
};
```
- **Answer**:
```javascript
let currentIndex = 0;
setInterval(function() {
document.getElementById('slideshowImage').src = images[currentIndex];
}, 3000);
```
8. **Question**: Create a progress bar that fills up as a user scrolls down the page.
- **Answer**:
```javascript
window.onscroll = function() {
};
```
9. **Question**: Create a calculator that performs addition, subtraction, multiplication, and division.
- **Answer**:
```javascript
function calculate(operation) {
let result;
switch(operation) {
document.getElementById('result').textContent = result;
}
```
10. **Question**: Create a button that, when clicked, displays the current mouse coordinates.
- **Answer**:
```javascript
document.getElementById('showCoordinates').onclick = function(event) {
};
```
11. **Question**: Validate a form to check if the username is at least 5 characters long.
- **Answer**:
```javascript
document.getElementById('submitForm').onclick = function() {
if (username.length < 5) {
};
```
12. **Question**: Create a form that only submits if both the password and confirm password fields
match.
- **Answer**:
```javascript
document.getElementById('submitForm').onsubmit = function(event) {
event.preventDefault();
};
```
13. **Question**: Show a character count for a textarea input as the user types.
- **Answer**:
```javascript
document.getElementById('myTextarea').oninput = function() {
};
```
14. **Question**: Automatically capitalize the first letter of each word in an input field.
- **Answer**:
```javascript
document.getElementById('capitalizeInput').oninput = function() {
};
```
- **Answer**:
```javascript
document.getElementById('passwordInput').oninput = function() {
document.getElementById('passwordStrength').textContent = strength;
};
```
16. **Question**: Disable the submit button of a form until all required fields are filled out.
- **Answer**:
```javascript
document.querySelectorAll('input').forEach(function(input) {
input.oninput = function() {
});
document.getElementById('submitButton').disabled = !allFilled;
};
});
```
17. **Question**: Create a search bar that filters through a list of items as the user types.
- **Answer**:
```javascript
document.getElementById('searchInput').oninput = function() {
Array.from(items).forEach(function(item) {
});
};
```
18. **Question**: Automatically format a phone number input to the format (xxx) xxx-xxxx.
- **Answer**:
```javascript
document.getElementById('phoneInput').oninput = function() {
if (formatted) {
};
```
19. **Question**: Create a button that clears all input fields in a form when clicked.
- **Answer**:
```javascript
document.getElementById('clearFormButton').onclick = function() {
document.querySelectorAll('input').forEach(function(input) {
input.value = '';
});
};
```
20. **Question**: Add validation to ensure that an email address contains "@" and "." characters.
- **Answer**:
```javascript
document.getElementById('submitEmailForm').onclick = function() {
if (!email.includes('@') || !email.includes('.')) {
alert('Please);
level -3
Here are 30 more medium-level JavaScript practice questions for you, focusing on DOM
manipulation, events, form handling, and web interactions. These will help you improve your web
development skills using JavaScript.
1. **Question**: Create a button that changes the font style of a paragraph between normal and
italic when clicked.
- **Answer**:
```javascript
document.getElementById('fontButton').onclick = function() {
};
```
2. **Question**: Create an input field that changes the background color of the page as the user
types a color name.
- **Answer**:
```javascript
document.getElementById('colorInput').oninput = function() {
document.body.style.backgroundColor = this.value;
};
```
3. **Question**: Create a button that shuffles the order of items in an unordered list when clicked.
- **Answer**:
```javascript
document.getElementById('shuffleButton').onclick = function() {
list.innerHTML = '';
};
```
4. **Question**: Create a button that displays an image and hides it after 5 seconds.
- **Answer**:
```javascript
document.getElementById('showImageButton').onclick = function() {
image.style.display = 'block';
setTimeout(function() {
image.style.display = 'none';
}, 5000);
};
```
5. **Question**: Create a button that increases the font size of a paragraph each time it is clicked.
- **Answer**:
```javascript
document.getElementById('increaseFontButton').onclick = function() {
};
```
6. **Question**: Create a button that reverses the text inside a paragraph when clicked.
- **Answer**:
```javascript
document.getElementById('reverseButton').onclick = function() {
para.textContent = para.textContent.split('').reverse().join('');
};
```
7. **Question**: Create a dropdown that changes the text alignment of a paragraph based on the
user’s selection (left, center, or right).
- **Answer**:
```javascript
document.getElementById('alignmentDropdown').onchange = function() {
document.getElementById('myParagraph').style.textAlign = this.value;
};
```
8. **Question**: Create a form that only enables the submit button if all checkboxes in a group are
checked.
- **Answer**:
```javascript
checkboxes.forEach(checkbox => {
checkbox.onchange = function() {
document.getElementById('submitButton').disabled = !allChecked;
};
});
```
9. **Question**: Create a button that changes the text of all headings (`<h1>`, `<h2>`, etc.) to
uppercase.
- **Answer**:
```javascript
document.getElementById('uppercaseButton').onclick = function() {
headings.forEach(heading => {
heading.textContent = heading.textContent.toUpperCase();
});
};
```
10. **Question**: Create a button that changes the text of a specific paragraph based on the user’s
input.
- **Answer**:
```javascript
document.getElementById('changeTextButton').onclick = function() {
document.getElementById('myParagraph').textContent = newText;
};
```
11. **Question**: Create a password strength indicator that shows "Weak", "Medium", or "Strong"
based on the length of the password.
- **Answer**:
```javascript
document.getElementById('passwordInput').oninput = function() {
document.getElementById('passwordStrength').textContent = strength;
};
```
12. **Question**: Create a button that displays an alert if the user has not filled out all required
fields in a form.
- **Answer**:
```javascript
document.getElementById('submitButton').onclick = function() {
if (!allFilled) {
};
```
13. **Question**: Create a form that displays a confirmation message before submitting.
- **Answer**:
```javascript
document.getElementById('myForm').onsubmit = function(event) {
let confirmSubmit = confirm('Are you sure you want to submit the form?');
if (!confirmSubmit) {
event.preventDefault();
};
```
14. **Question**: Create a live word counter that displays the number of words in a textarea as the
user types.
- **Answer**:
```javascript
document.getElementById('textArea').oninput = function() {
};
```
15. **Question**: Create a form that shows an error message if an email input does not include a
"@" symbol.
- **Answer**:
```javascript
document.getElementById('emailInput').oninput = function() {
if (!email.includes('@')) {
} else {
errorMessage.textContent = '';
};
```
16. **Question**: Create a form that calculates and displays the total of numbers entered into
multiple input fields.
- **Answer**:
```javascript
document.getElementById('calculateButton').onclick = function() {
let total = 0;
inputs.forEach(input => {
total += parseFloat(input.value) || 0;
});
};
```
17. **Question**: Create a form that automatically formats the entered text to title case (first letter
capitalized) as the user types.
- **Answer**:
```javascript
document.getElementById('titleInput').oninput = function() {
};
```
18. **Question**: Create a form that limits the user to typing a maximum of 100 characters in a
textarea.
- **Answer**:
```javascript
document.getElementById('myTextarea').oninput = function() {
};
```
19. **Question**: Create a password toggle button that shows and hides the password in an input
field.
- **Answer**:
```javascript
document.getElementById('togglePassword').onclick = function() {
passwordField.type = 'text';
} else {
passwordField.type = 'password';
this.textContent = 'Show Password';
};
```
20. **Question**: Create a form that shows an error message if the user tries to submit with a blank
input field.
- **Answer**:
```javascript
document.getElementById('submitForm').onsubmit = function(event) {
inputs.forEach(input => {
event.preventDefault();
});
};
```
21. **Question**: Create a dropdown menu that changes the background color of a specific `<div>`
based on the selected option.
```