1. What is web technology? Explain different data types used in JavaScript.
Ans -> Web technology refers to the collection of tools, software, protocols, and standards that are used for building and maintaining websites and web applications.
Javascript provides different datatypes to hold different types of values. There are two types of datatypes in Javascript.
Primitive datatype,
Non-Primitive (reference) datatype
Primitive Data Types
Primitive datatypes can hold only one value at a time. There are five types of primitive datatypes in Javascript. They are as follow:
Data type | Description |
String | Represents a sequence of characters e.g., “hello”. |
Number | Represents numeric values e.g. 100 |
Boolean | Represents boolean values either true or false. |
Undefined | Represents an undefined value |
Null | Represents null i.e, no value at all |
Non-Primitive Data Types
Non-primitive datatypes can hold collections of values and complex entities. The non -primitive data types are as follows:
Data Type | Description |
Object | Represents an instance through which we can access members |
Array | Represents a group of similar value |
RegExp | Represents a regular expressions |
- Number:
> represents both integer and floating – point number (Decimal value)
>Example : ‘let num = 404; ‘ or var num = “404”;
- String :
> represents the sequence of characters.
>Example: let txt = “Hello World !”;
- Boolean :
> represent logical values ‘True’ or ‘false’
> Example : let value = true;
- Null :
> represent the absence of any object values,
> Example : let value = null;
- Object :
> represents a collections of key-value pairs (Properties & methods)
- Array:
> represents a group of similar values,
> Example : let name = [ ‘Ram’ , ‘Shyam’ , ‘Hari’] ;
2. Differentiate between Client-side scripting & server-side scripting.
Ans -> Client-side scripting and server-side scripting are two distinct approaches to handling the logic and functionality of web applications. We can be differentiated as under explained below:
Client-side scripting involves running scripts on the user’s web browser. It is responsible for enhancing the user interface, providing dynamic interactivity, and reducing server load by handling tasks locally. Commonly implemented in JavaScript, client-side scripting allows for instant user feedback and is visible to users, potentially posing security risks. Beside client – side, Server-side scripting runs on the web server, and only the results are sent to the client. The server-side code is not visible to users, ensuring better security. It handles tasks that require server resources, such as processing form submissions, querying databases, and generating dynamic content. Server-side scripting often uses languages like PHP, Python, Ruby, or Node.js.
3. What is event handling in Javascript? Explain with example.
Ans -> Javascript’s interactions with HTML is handled through events that occurs when the user or the browser manipulates a page. When the page loads, it is called an event. When the users clicks a button. That clicks is an event. Others examples include events like pressing any key, closing a window , resizing a window.
Developers can use these events to executes javascript coded responses, which causes button to close window, messages to be displayed ot users, data to validated, and virtually any other types of responses imagination.
It can be explained better with the help of programming :
<html>
<head>
<title> Event Handling </title>
</head>
<body>
<form>
<input type=”button” value=”Click” onclick = “sample()”>
</form>
<script>
function sample(){
document.write(“Hello World !”);
}
</script>
</body>
</html>
4. . Create a Page with a button with the value “Computer” on clicking the button your page should be” Computer Science”.
Ans -></html>
<head><title>Computer science </title></head>
<body>
<form>
<input type=”button” value=”Computer” onclick=”sample()”>
</form>
<script>
function sample(){
document.write(“Computer science”);
}
</script>
</body>
</html>
5. Write a JavaScript program to calculate the factorial of a given number.
Ans:
<html>
<head>
<title> Factorial of a number </title>
</head>
<body>
<script>
var num = prompt(“Enter any number: “);
var i, res=1;
for(i=1;i<=num;i++)
{
res = res*i;
}
document.write(“Factorial is “+res);
</script>
</body>
</html>
6. What is Javascript ? Write a code to display the sum of two numbers.
Ans : Javascript is a light-weight object-oriented programming language, which is used in the websites for scripting the webpages.
Program:
<html>
<head>
<title> Sum </title>
</head>
<body>
<script>
var num1 = prompt(“Enter first number: ”);
var num2= prompt(“Enter second number: “);
var sum = parseInt(num1) + parseInt(num2);
document.write(“The sum is “+sum);
</script>
</body>
</html>
7. Write a javascript program to input three numbers and display the largest number between them.
Ans:
<html>
<head>
<title> Greatest number </title>
</head>
<body>
<script>
var num1 = prompt(“Enter first number: “);
var num2 =prompt(“Enter second number: “);
var num3= prompt(“Enter third number: “);
if(num1>num2 && num1>num3)
document.write(“The greatest number is “+num1);
else if(num2>num1 && num2>num3)
document.write(“The greatest number is “+num2);
else if(num3>num1 && num3>num1)
document.write(“The greatest number is “ +num3);
else
document.write(“All are equal”);
</script>
</body>
</html>
8. Write a JavaScript program to display “Welcome Class-12” using the onload event.
Ans:
<html>
<head>
<title> Loading </title>
</head>
<script>
function display(){
document.write(“Welcome Class-12”);
}
</script>
<body onload=”display()”>
</body>
<html>
9. Define form validation. Write a code to create a login form of users including username, password, gender, & address.
Ans -> Form is the collection of form fields that accepts the input from the user & send it to the server for further processing .
Similarly, Form Validation is a process of validating the form after user input such as checking whether the form is empty, required attributes are filled , or any other client side events before sending it to the server.
Program:
<html>
<head>
<title> Form Validation </title>
</head>
<form>
Username: <input type=”text” placeholder=”Username” > <br>
Password: <input type=”password” ><br>
Gender: <input type=”radio”>Male
<input type=”radio”>Female
<br>
Address: <select>
<option> Nepal </option>
<option> India </option>
<option> China </option>
</select>
<input type=”submit” value=”Login” >
</form>
</body>
</html>
10. Write a J.S code to create a marksheet using if structure & find the grades based on the following:
Percentage(%) | Grade |
>=90 | A+ |
>=80 | A |
>=70 | B+ |
>=60 | B |
>=50 | C+ |
<50 | C |
Program:
<html>
<head>
<title> Marksheet </title>
</head>
<body>
<form>
Marks obtained in Math: <input type=”number” autofocus id=”math”> <br>
Marks obtained in Computer: <input type=”number” autofocus id=”comp”><br>
Marks obtained in Account: <input type=”number” autofocus id=”acc”> <br>
<input type=”submit” value=”Find” onclick=”check()”>
</form>
<script>
function check(){
var math = document.getElementById(“math”).value;
var comp = document.getElementById(“comp”).value;
var acc = document.getElementById(“acc”).value;
var total = parseInt(math) + parseInt(comp) + parseInt(acc);
var per = parseFloat(total)/3;
if(per >=90)
document.write(“You Scored A+”):
else if(per>=80 && per<=90)
document.write(“You scored A”);
else if(per>=70 && per<=80)
document.write(“You scored B+”);
else if(per>=60 && per<=70)
document.write(“You scored B”);
else if(per>=50 && per<=60)
document.write(“You scored C+”);
else if(per>=40 && per<=50)
document.write(“You scored C”);
else
document.write(“You failed !!”);
</script>
</body>
</html>
11. Define loop . Write a J.S code to display the number from 1 to 10.
-> A loop in programming is a control flow structures that allows a set of instructions to be repeated multiples time. In Javascript, there are several types of loop , such as ‘for’ loop , ‘while’ loop & ‘do while’ loop.
Program:
<html>
<head>
<title> Looping </title>
</head>
<script>
Var i;
for(i=1;i<=10;i++)
{
document.write(i);
document.write(“<br>”);
}
</script>
</body>
</html>
12. Write a Javascript code whether the number is palindrome or not using function.
Ans:
<html>
<head>
<title> Palindrome </title>
</head>
<body>
Enter number: <input type=”number” required id=”num”><br>
<input type=”submit value=”Check”onclick=”palindrome()”>
</body>
<script>
function palindrome(){
var num = document.getElementById(“num”).value;
var r,c,s=0;
c=num;
while(num>0)
{
r=num%10;
s=r+(s*10);
num=num/10;
}
if(num==s)
document.write(“Palindrome”);
else
document.write(“Not Palindrome”);
</script>
</html>
13. WAP to display 1 3 5 7 …….. 10th term.
Program:
<html>
<head>
<title> Odd number </title>
</head>
<body>
<script>
var num;
for(num=1;num<=20;num=num+2)
{
document.write(num+” ”);
}
</script>
</body>
<html>
14. Write a J.S code to input the no. between (1-7) & display the name of running week.
->
<html>
<head>
<title> Switch Case </title>
</head>
<body>
<script>
var num = prompt(“Enter the number (1-7) !”);
var n = parseInt(num);
switch(n){
case 1:
document.write(“Sunday”);
break;
case 2:
document.write(“Monday”);
break;
case 3:
document.write(“Tuesday”);
break;
case 4:
document.write(“Wednesday”);
break;
case 5:
document.write(“Thursday”);
break;
case 6:
document.write(“Friday”);
break;
case 7:
document.write(“Saturday”);
break;
default:
document.write(“Invalid input ! Please Try again “);
}
</script>
</body>
</html>
15. Define CSS. List it’s advantages and explain with example.
Ans -> CSS stands for cascading style sheet which consists of various styles that defines how to display HTML elements. It is used to make the design of the website dynamic & attractive. Style are normally stored in style sheet. Since, every tags cannot design the website in very fascinating way we use CSS to solve that problems.
Advantages or features of CSS:
- Web pages will load faster.
- It helps to maintain design consistency across all the pages of web site.
- CSS allows for customizing elements such as font, font size, font color, & many more.
- It makes web page compatible with all the browsers.
In web designing , there are several methods of C.S.S :
- Inline CSS:
-> It is the practice of applying CSS styles directly within the HTML document , so called in the tags. It can be expressed with the example:
<body bgcolor=”fff”>
<h2 style=”text-align: center;> Hello Class 12 </h2>
</body>
- External CSS:
-> It refers to the practice of storing style information in a separate file with a .css extension, which is then linked into an HTML document.
It can be expressed with the programming:
HTML : <head>
<title> External CSS </title>
<link rel=”stylesheet” href=”style.css”>
</head>
<body>
<p> Hello world ! </p>
</body>
CSS : body{
text-align:center;
color : white;
}
- Internal CSS
-> It refers to the practice of placing definition directly within the HTML document typically with in the <style> tag in the document in the <head> section.
<head>
<title> Internal CSS </title>
<style>
body{
back-ground color: “3b5998”;
Margin:0;
}
</style>
</head>
<body>
<h1> Welcome </h1>
</body>
16. Write a Php program to display three largest number among three.
->
<?php
$num1 = 10;
$num2 = 20;
$num3 = 30;
if($num1 > $num2 && $num1 > $num3){
$greatestnumber = $num1;
}
else if($num2 > $num1 && $num2> $num3){
$greatestnumber = $num2;
}
else {
$greatestnumber = $num3;
}
echo “The greatest number among $num1, $num2,$num3 is”.$greatestnumber;
?>
17. Explain Php and its uses & features in web development with it’s syntax.
-> Php(Hyper-text preprocessor) is a server-side scripting language, & powerful tool for making dynamic & interactive web pages. Php is widely -used ,free & efficient alternative to competitions such as Microsoft ASP , Php scripts are executed on the server & is free to download & use.It runs on various platforms (Window, linux, unix). Php files contain text,html, css, J.S, & php code. Php codes are executed on the server & the result is returned to the browser as plain HTML. Php files have extension ‘.php’.
What can Php do?
- Php can generate dynamic page content .
- Php can create , open , read,write, delete & close files on the servers.
- Php can collect form data.
- Php can send & receive cookies.
A php script can be placed anywhere in the document . A php script starts with <?php & ends with ?>
Syntax:
<? Php
// Php code here
?>
In php, a variable start with the $ sign, followed by the name of the variable .
Example:
$txt = “Hello world”;
$num = 40;
Rules for php variables:
- A variable starts with $ sign , followed by the name of the variable.
- A variable name must start with a letter or the underscore characters.
- A variable name cannot start with a number.
- Variable names are case-sensitive.
Php example to print “Hello World “ .
<html>
<body>
<h3> Home Page </h3>
<?php
echo “Hello World”;
?>
</body>
</html>
18. Develop a program in J.S to exchange/swap the values of any two variables.
Program:
<html>
<head>
<title> Swapping </title>
</head>
<body>
<script>
// Variable value declaration
var num1 = 40;
var num2 = 30;
document.write(“Value before swapping : “);
document.write(“num1: ” +num1);
document.write(“num2: ” +num2);
//Now swapping values declaring temp variable
var temp = num1;
num1 = num2;
num2 = temp;
document.write(“Value after swapping : “):
document.write(“num1: ”+num1);
document.write(“num2: ”+num2);
</script>
</body>
</html>
19. How can you connect a database with Php? Demonstrate with an example.
-> To connect a database with php, ,we use the mysqli_connect(), which takes four parameter, servername, username , password and database name. Here is an example code.
<?php
$servername = “localhost;
$username = “root”;
$password = “ “;
$dbname = “dbform”;
//Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
//Check connection
if(!=$conn)
{
die (“Connection failed: “.mysqli_connect_error());
}
else
echo “Connected successfully!”;
?>
Now Save the Php file and run it in a web browser. If the connection is successful , you can see the “Connected successfully” displayed in the browser.
20. Write a J.S code to print the largest & smallest number among 10 elements of an array.
->
<html>
<head>
<title> Array </title>
</head>
<body>
<script>
var num = [10,20,30,40,50,60,70,80,90,100];
var i;
//Initialize the variable to store the largest and smallest number
var largest = num[0];
var smallest = num[0];
//Looping the array to find the largest & smallest number
for(i=1;i<=num.length;i++)
{
if(num[i] > largest){
largest = num[i];
}
if(num[i] < smallest){
smallest = num[i];
}
//Displaying the largest and smallest number
document.write(“Largest number is : “ +largest +”<br>”);
document.write(“Smallest number is: “ +smallest +”<br>”);
</script>
</body>
</html>
21. Write a J.S code to display the multiplication table using function.
Program:
<html>
<head>
<title> Multiplication table </title>
</head>
<body>
<script>
function table()
{
var a=prompt(“Enter any number”);
var b;
for(b=1;b<=10;b++)
{
document.write(a+”*”+b+”=”+a*b+”<br>”);
}
}
</script>
<input type=”button” value=”table” onclick=”table()”>
</body>
</html>
22. Write a server -side scripting code to insert data into the table student having fields (first name, last name, mark, and email). Assume that servername=”localhost” , username=”root”, password=” “, database name=”student.db”.
Program:
<?php
//DB connection parameters
$servername =”localhost”;
$username = “root”;
$password =” “;
$database =”student”;
//Create connection
$conn = mysqli_connect($servername,$username,$password,$dbname);
//Check connection
if(!=$conn){
die (“Connection failed: “.mysqli_connect_error());
}
else
echo “Connected successfully”;
// Sample data to be inserted in the form
$firstname = “Ram”;
$lastname =”Shah”;
$mark = 99;
$email =”ramshah@sample.com”;
//SQL query to insert data into student table
$sql = INSERT into student (first_name,last_name,mark,email) VALUES (‘$firstname’, ‘$lastname’ , ‘$mark’, ‘$email’);
if(mysqli_query($conn,$sql)){
echo “Data is stored in database successfully”;
else
echo “Error: “.$sql.mysqli_error($conn);
}
//Close connection
mysqli_close($conn);
?>