Javascript Hands on TCS Xplore Question Solution

Javascript Hands on 1


JavaScript Hands-on 1
index.html
-
---------------------------------------------------------------------
<!DOCTYPE html>
<htmt>
<head>
<script src="index.js">
</script>
</head>
<body><input type="button" value="show" onclick="callMe()">
</body>
</html>

----------------------------------------------------------------------------
index.js
--------------------------------------------------------------
----------------------------------------------------------------------------
function callMe() {
var Passenger=new Object();
Passenger.name="Arun";
Passenger.age=28;
Passenger.reservedStatus='true';
document.write('<div id="name">'+Passenger.name+'</div>');
document.write('<div id="age">'+Passenger.age+'</div>');
document.write('<div id="reservedStatus">'+Passenger.reservedStatus+'</div>');
}

Javascript Hands on 2

JavaScript Hands-on 2
index.html
----------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Array</title>
<script></script>
</head>
<body>
<div id="app">Please Choose a Car to get its Details:
<select id="SelectCar" onchange="displayCarDetails()">
<option value="Innova">Innova</option>
<option value="Dzire">Dzire</option>
<option value="i20">i20</option>
<option value="i10">i10</option>
</select>
<p id="CarDetail"></p>
</div>
</body>
</html>
----------------------------
index.js
-----------------------------------------
------------------------------------------------

let carDetails = [{carName:"Innova" ,onRoadPrice:900000,yearOfModel:2016},{carName:"Dzire" ,onRoadPrice:700000, yearOfModel:2017},{carName:"i20" ,onRoadPrice:500000, yearOfModel:2013},{carName:"i10" ,onRoadPrice:400000, yearOfModel:2016}];
function displayCarDetails(){
var x = document.getElementById("SelectCar").value;
if(x=="Innova"){
document.getElementById("CarDetail").innerHTML= carDetails[0].carName+"-"+carDetails[0].onRoadPrice+"-"+carDetails[0].yearOfModel;}
else if(x=="Dzire"){
document.getElementById("CarDetail").innerHTML=carDetails[1].carName+"-"+carDetails[1].onRoadPrice+"-"+carDetails[1].yearOfModel;}
else if(x=="i20"){
document.getElementById("CarDetail").innerHTML=carDetails[2].carName+"-"+carDetails[2].onRoadPrice+"-"+carDetails[2].yearOfModel;}
else{
document.getElementById("CarDetail").innerHTML =carDetails[3].carName+"-"+carDetails[3].onRoadPrice+"-"+carDetails[3].yearOfModel;}
}

Javascript Hands on 3

JavaScript Hands-on 3
index.html
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="app">
<form onsubmit="return getAge()">
<label>Enter your DOB:</label>
<input type="date" id="dob" name="dob">
<input type="submit" value="submit">
<p id="showresults"></p>
</form>
</div>
<script src="index.js" type="text/javascript"> </script>
</body>
</html>
--------------------------------------------------
--------------------------------------------------
index.js
--------------------------------------------------
---------------------------------------------------
function getAge() {
var age=document.getElementById("dob").value;
var l=age.toString();
var kk= l.substring(0,4);
var year=parseInt(kk);
var d= new Date();
var n=d.getFullYear();
var k=n.toString();
var m=parseInt(k);
if(year>=m){
document.getElementById("showresults").innerText="Wrong date!!";
}
else{
document.getElementById("showresults").innerText="You are "+(m-year)+" year(s) old!!";
}
}

Javascript Hands on 4


JavaScript Hands-on 4
index.html
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Loop</title>
<script src="index.js"></script>
</head>
<body>
<input type="button" id="generate" value="Generate Series"
onclick="SumOfSeries()">
<p id="result"></p>
</body>
</html>
----------------------------------------
--------------------------------------
index.js
----------------------------------------
----------------------------------------

function SumOfSeries(){
var a=0;
var b=1;
var sum=0;
var tmp;
while((a<100)&&(b<100)){
sum=sum+b;
tmp=a;
a=b;
b=b+tmp;
}
document.getElementById("result").innerText=sum;

}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top