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

css ut1

Uploaded by

Deep gaichor
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

css ut1

Uploaded by

Deep gaichor
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Q State the ways to display the output in JavaScript.

-->
i] Using console.log():
Displays output to the browser's console.
Ex:
console.log("Hello World!");

ii] Using alert():


Displays an alert dialog box with specified message.
Ex:
alert("Hello Worl");

iii] Using document.write():


Writes directly into Html document.
Ex:
document.write("Hello Wor");

Q List the logical operator in javascript with description.


-->

i] &&
Returns true if both operands are true, otherwise false.
ii] ||
Returns true if at least one of operand is true, otherwise false.
iii] !
Returns true if operand is false and vice versa.

Q Write JavaScript to create a object “student” with properties roll number, name,
branch, year. Delete branch property and display remaining properties of student
object.
-->
<!Doctype html>
<html>
<body>
<script type="text/javascript">
let student={
rollno:14, name:'Deep', branch:'IF', year:'Ty'
};
delete student.branch;
console.log(student);
</script>
</body>
</html>

Q Write a JavaScript that initializes an array called Colors with the names of 3
Colors and display the array elements.
-->
<!DOCTYPE html>
<html>
<body>
<script>
Colors=["Red","Green","Blue"];
for(let i in Colors){
document.write(Colors[i]);
}
</script>
</body>
</html>
Q Write a JavaScript for loop that will iterate from 1 to 15. For each iteration,
it will check if the current number is odd or even and display a message to the
screen.
Sample Output :
“1 is odd”
“2 is even”
-->
<!DOCTYPE html>
<html>
<head>
<title>Looping </title>
</head>
<body>
<script>
for(let i=1;i<16;i++){
if(i%2==0){
document.writeln(i+" is even<br>");
}
else{
document.write("\n"+i+" is odd<br>")
}
}
</script>
</body>
</html>

Q Differentiate between push() and join() method of array object with respect to
use, syntax, return value and example.
-->
feature push() join()

Use Adds one or more elements to end of an array. Joins all the elements
in the array into single string.

Syntax array.push(element1,element2,..,elementN);
array.join(separator);

Return value Returns the new length of the array. Returns a string
with all array elements joined.

Example let a=[2,3,4]; let a=[2,3,4],b;


a.push(9); b=a.join("==");
document.write("<br>"+a); document.write("<br>"+b);

Q Write JavaScript code to perform following operations on string. (Use split()


method)
Input String : “Sudha Narayana Murthy”
Display output as
First Name : Sudha
Middle Name : Narayana
Last Name : Murthy
-->

<!DOCTYPE html>
<html>
<head>
<title>Looping </title>
</head>
<body>
<script>
let a='Sudha Narayana Murthy';
let s=a.split(' ');
document.write("First Name: "+s[0]+"<br>");
document.write("Middle Name: "+s[1]+"<br>");
document.write("Last Name: "+s[2]);
</script>
</body>
</html>

Q Explain splice() method of array object with syntax and example.


-->
The splice() method is used to add, remove or replace elements in array.
It returns array containing removed elements.
It modifies original array.

Syntax:
array.splice(start, deleteCount,item1,item2,...,itemN);
Ex:
<!DOCTYPE html>
<html>
<body>
<script>
let numbers = [1, 2, 3, 4, 5],r;
r=numbers.splice(2, 2, 6, 7);
document.write(numbers);
document.write("<br>Removed elements: "+r);
</script>
</body>
</html>

Q Write HTML code to design a form that displays two textboxes for accepting two
numbers, one textbox for accepting result and two buttons as ADDITION and
SUBTRACTION. Write proper JavaScript such that when the user clicks on any one of
the button, respective operation will be performed on two numbers and result will
be displayed in result textbox.
-->
<!DOCTYPE html>
<html>
<head>
<title>Simple Calculator</title>
<script>
function calculate(operation) {
let n1 = parseFloat(document.getElementById("num1").value);
let n2 = parseFloat(document.getElementById("num2").value);
let result = (operation === 'add') ? n1 + n2 : n1 - n2;
document.getElementById("result").value = result;
}
</script>
</head>
<body>
<label>Number 1: <input type="number" id="num1"></label><br><br>
<label>Number 2: <input type="number" id="num2"></label><br><br>
<label>Result: <input type="text" id="result" readonly></label><br><br>
<button type="button" onclick="calculate('add')">Addition</button>
<button type="button" onclick="calculate('sub')">Subtraction</button>
</body>
</html>
Explain how to evaluate Radiobutton in JavaScript with suitable example.
-->
In JavaScript, evaluating a radio button involves determining which radio button in
a group is selected. This is commonly done when a user needs to select one option
from a set of choices.

Steps to Evaluate a Radio Button:


Create a Group of Radio Buttons: In HTML, radio buttons are grouped by giving them
the same name attribute.
Use JavaScript to Check Which Radio Button is Selected: You can loop through the
radio buttons or use a method to determine the selected one.

Ex:
<!DOCTYPE html>
<html>
<head>
<title> Radiobutton Ex:</title>
</head>
<body>
<form>
<label><input type="radio" id="c1" name="color" value="Red">
Red</label><br><br>
<label><input type="radio" id="c2" name="color" value="Green">
Green</label><br><br>
<label><input type="radio" id="c3" name="color" value="Blue">
Blue</label><br><br>
<button type="button" onclick="getSelectedB()">Submit</button>

</form>
<p id="Re"></p>
<script>
function getSelectedB(){
let c=document.getElementsByName("color");
let d='';
for(let i=0;i<c.length;i++){
if(c[i].checked){
d=c[i].value;
break;
}
}
if(d!=''){
document.getElementById('Re').textContent="Your Favourite Color
is "+d;
}else{
document.getElementById('Re').textContent="Please choose a
color.";
}
}
</script>
</body>
</html>

Q Write a JavaScript that accepts a number and displays addition of digits of that
number in a message box.
-->
<!DOCTYPE html>
<html lang="en">

<body>
<script>
let num=parseInt(prompt("Enter a number: "));
let d=0,m;
while(num>0){
m=(num%10);
d=d + m;
num=parseInt(num/10);
}
alert("addition of digits of number is "+d);
</script>
</body>
</html>

Q Write a JavaScript program that will remove the duplicate element from an array.
-->
<!DOCTYPE html>
<html lang="en">
<body>
<script>
function removeduplicates(array){
return [...new Set(array)]
}
let a=[3,4,6,7,3,5,4],rd;
rd=removeduplicates(a);
document.write(rd);
</script>
</body>
</html>

Q Write a javascript to checks whether a passed string is palindrome or not.


-->
<!DOCTYPE html>
<html lang="en">

<body>
<script>
function cheP(a){
let rv=a.split('').reverse().join('');
if(a==rv){
document.write("String is Palindrome.");
}else{
document.write("String is not Palindrome");
}
}
let a=prompt("Enter a string: ");
cheP(a);
</script>
</body>
</html>

Q Write a javascript to display square of 1 to 10 number using while loop.


-->

<!DOCTYPE html>
<html>
<body>
<script>
num=1;
while(num<11){
document.write(num*num+"<br>");
num+=1;
}
</script>
</body>
</html>

Q Write a program to generate armstrong num bers betwen 1 to 100.


-->

<!DOCTYPE html>
<html>
<body>
<script>
for(let i=1;i<101;i++){
let a=0,d=i;
while(d>0){
let c=d%10;
a=a+(c*c*c);
d=parseInt(d/10);
}
if(i==a){
document.write(a+"<br>");
}
}
</script>
</body>
</html>

Q Write a javascript program which takes n as input and print first n odd numbers.
-->

<!DOCTYPE html>
<html>
<body>
<script>
let n=parseInt(prompt("Enter a number: ")),c=0;
while(c!=n){
for(let i=1;i<n*2;i++){
if(i%2!=0){
document.write(i+"<br>");
c++;
}
}
}
</script>
</body>
</html>

Q Write a javascript program to define array elements, find length of array and
sort array.
-->

<!DOCTYPE html>
<html>
<body>
<script>
let a=new Array(1,2,6,3,7,4,9);
document.write("Length of an Array: "+a.length);
document.write("<br>Sorted Array: "+a.sort());
</script>
</body>
</html>

Q Write a javascript to convert string to number.


-->

<!DOCTYPE html>
<html>
<body>
<script>
let a=' 456 ';
b=Number(a);
c=parseInt(a);
d=+a;
document.write("<br> Using Number(): "+b);
document.write("<br> Using parseInt(): "+c);
document.write("<br> Using +unary: "+d);
</script>
</body>
</html>

Q Give syntax of and explain the use of small “with” clause.


-->
“with” clause is used to directly access the properties and method of an object.

Syntax:
with (object)
{
//code
}
Example:
<script>
var person ={ name:"Abc", age:18
}
with(person){ docment.write(name); docment.write(age);
}
</script>

You might also like