JavaScript and JSON/XML Exercises Solutions
1. Hello World using <script> and document.write:
<html>
<body>
<script>
document.write('Hello World');
</script>
</body>
</html>
2. Get user input and print details:
<script>
const fullName = prompt('Enter full name:');
const address = prompt('Enter address:');
const city = prompt('Enter city:');
const email = prompt('Enter email:');
const mobile = prompt('Enter mobile no:');
document.write(`Full Name: ${fullName}<br>
Address: ${address}<br>
City: ${city}<br>
Email: ${email}<br>
Mobile: ${mobile}`);
</script>
3. Max/Min of three numbers with function on click:
<script>
function findMaxMin(a, b, c) {
const max = Math.max(a, b, c);
const min = Math.min(a, b, c);
alert(`Max: ${max}, Min: ${min}`);
}
</script>
<button onclick="
const a=+prompt('First:'), b=+prompt('Second:'), c=+prompt('Third:');
findMaxMin(a,b,c);
">Check Max/Min</button>
4. Factorial using do-while:
<script>
let num = +prompt('Enter a number:');
let fact = 1, i = 1;
do {
fact *= i;
i++;
} while (i <= num);
alert(`Factorial of ${num} is ${fact}`);
</script>
5. Print array in reverse using while:
<script>
const arr = [1,2,3,4,5];
let idx = arr.length - 1;
while(idx >= 0) {
console.log(arr[idx]);
idx--;
}
</script>
6. Count occurrences in array using for:
<script>
const arr2 = ['a','b','a','c','a'];
const target = prompt('Element to count:');
let count=0;
for(let el of arr2) if(el===target) count++;
alert(`'${target}' found ${count} times.`);
</script>
7. Print current date/time:
<script>
const d = new Date();
document.write(`Date: ${d.getMonth()+1}/${d.getDate()}/${d.getFullYear()} ` +
`Time: ${d.toLocaleTimeString()}`);
</script>
8. Convert text to uppercase on keyup with validation:
<input id='txt' onkeyup="
const v=this.value;
if(v.trim()==='') return;
this.value=v.toUpperCase();
">
9. Validate student registration form (example fields):
<form onsubmit="return validateForm()">
<input id='name' required>
<input id='email' type='email' required>
<button type='submit'>Submit</button>
</form>
<script>
function validateForm(){
// additional checks here
return true;
}
</script>
10. Login page validation:
<form onsubmit="return login()">
<input id='user' required>
<input id='pass' type='password' required>
<button type='submit'>Login</button>
</form>
<script>
function login(){
const u=user.value, p=pass.value;
if(!u||!p) { alert('Required'); return false; }
if(p.length<6||p.length>12){ alert('Password length invalid'); return false; }
return true;
}
</script>
11. Verify two text boxes content:
<input id='t1'><input id='t2'>
<button onclick="
alert(t1.value===t2.value ? 'Same' : 'Not same');
">Check</button>
12. Validate price range:
<input id='min' type='number' min='1'>
<input id='max' type='number' min='1'>
<button onclick="
const mn=+min.value, mx=+max.value;
if(mx<mn){alert('Max < Min');} else alert('OK');
">Validate</button>
13. Check DOB not before today:
<input id='dob' type='date' onchange="
const d=new Date(this.value);
if(d>new Date()) alert('Invalid DOB');
">
14. jQuery selectors ID, class, tag:
<script>
$('#myId').hide();
$('.myClass').show();
$('p').css('color','red');
</script>
15. jQuery first, last, eq:
<script>
$('li').first().css('font-weight','bold');
$('li').last().css('font-style','italic');
$('li').eq(2).css('text-decoration','underline');
</script>
16. Table row coloring and hover:
<script>
$('tr:odd').css('background','blue');
$('tr:even').css('background','pink');
$('tr').hover(function(){ $(this).css('background','green'); });
</script>
17. Show/hide paragraphs:
<script>
$('h3').click(function(){
const idx=$(this).index('h3');
$('p').hide().eq(idx).show();
});
</script>
18. Multiplication table via jQuery:
<input id='num'><button id='go'>Go</button>
<script>
$('#go').click(function(){
const n=+$('#num').val();
if(n<1||isNaN(n)) return;
for(let i=1;i<=10;i++) console.log(`${n}x${i}=${n*i}`);
});
</script>
19. Change background via dropdown:
<select onchange="document.body.style.background=this.value;">
<option value='white'>White</option>
<option value='lightblue'>Blue</option>
</select>
20. jQuery fade in/out images:
<button id='fi'>Fade In</button>
<button id='fo'>Fade Out</button>
<img id='img' src='pic.jpg'>
<script>
$('#fi').click(()=>$('#img').fadeIn());
$('#fo').click(()=>$('#img').fadeOut());
</script>
---- JSON and XML Exercises ----
1. Print object via JavaScript:
<script>
const obj={ fullname:'Miten Bhavsar', Address:'Baroda', Mobile:1234567890 };
console.log(obj);
</script>
2. Multiple objects:
<script>
const data={ Employee:{ Fullname:'Suhit Patel', Address:'Maninagar', City:'Ahmedabad', Mobile:
Experience:{ Expertise:'JAVA Developer', DOJ:'12/01/2019', WorkExp:4.6, Salary:56
console.log(data);
</script>
3. Simple JSON array:
<script>
const arr3=['a', 'b', 'c'];
console.log(arr3);
</script>
4. Array of objects:
<script>
const books=[ { Title:'Jquery Step by Step', Author:'Maulik Patel', Page:214, Publisher:'ITSol
{ Title:'DBMS Step by Step', Author:'Bhavik Pandya', Page:300, Publisher:'ITSo
console.log(books);
</script>
5. Array in object:
<script>
const faculty={ Faculty:[ { Fullname:'Kedar Patel', Age:39, City:'Ahmedabad', DOB:'24/09/1984'
console.log(faculty);
</script>
6. Parse string to JSON:
<script>
const str='{"productid":101,"ProductName":"Phone","Price":500}';
const prod=JSON.parse(str);
console.log(prod);
</script>
7. Car array in object:
<script>
const carData={ CAR:[ { Company:'MARUTI', Model:'Swift', Variant:'Petrol', MFD:'24/03/2023', P
console.log(carData);
</script>
8. XML for Mobile Detail:
<?xml version="1.0"?>
<Mobile>
<Company>Apple</Company>
<Model>iPhone 14</Model>
<Price>999</Price>
</Mobile>
9. XML for student result:
<?xml version="1.0"?>
<Result>
<Student RollNo="101">
<Name>John Doe</Name>
<Subject name="Math">90</Subject>
<Subject name="Science">85</Subject>
<Subject name="English">88</Subject>
<Total>263</Total>
<Percentage>87.67</Percentage>
<Outcome>Pass</Outcome>
</Student>
</Result>
10. DTD for Bank Employee:
<!DOCTYPE Bank [
<!ELEMENT Bank (Employee+)>
<!ELEMENT Employee (Name, Birthdate, skills)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Birthdate (Date, Month, Year)>
<!ELEMENT Date (#PCDATA)>
<!ELEMENT Month (#PCDATA)>
<!ELEMENT Year (#PCDATA)>
<!ELEMENT skills (#PCDATA)>
]>
11. XML for University:
<?xml version="1.0"?>
<University>
<Course CourseID="CS101" CourseName="Computer Science">
<Semester id="1" name="Fall"/>
<Semester id="2" name="Spring"/>
</Course>
</University>
12. XML Agency, Owner, Property:
<?xml version="1.0"?>
<Root>
<Agency>
<Name>XYZ</Name><Email>xyz@abc.com</Email><Phone>1234567890</Phone>
</Agency>
<Owner><Name>John</Name></Owner>
<Property>
<Name>Villa</Name><Description>Luxurious</Description><Area>2000</Area><City>LA</City>
</Property>
</Root>
13. XML for Bookstore:
<?xml version="1.0"?>
<Bookstore>
<Book category="Fiction">
<Title language="EN">The Alchemist</Title>
<Author>Paulo Coelho</Author><Year>1988</Year><Price>15.99</Price>
</Book>
</Bookstore>
14. XML for BCA syllabus:
<?xml version="1.0"?>
<BCA>
<SEM>
<Subject>Maths</Subject><Subject>SQL</Subject><Subject>JS</Subject><Subject>HTML</Subject>
</SEM>
</BCA>
15. XML for Project Detail:
<?xml version="1.0"?>
<ProjectDetail>
<ProjectID>P001</ProjectID>
<ProjectName>ChatGPT App</ProjectName>
<ProjectDate>2025-04-25</ProjectDate>
<TotalMember>5</TotalMember>
</ProjectDetail>