Long Answers
Long Answers
(Use
prompt())
Ans :-
<html>
<body>
<script type="text/javascript">
var no1=prompt("Enter number: ","0");
var no2=prompt("Enter number: ","0");
var choice=prompt("Enter choice 1.Addition 2.Subtraction 3.Multiplication 4.Division : ","1");
switch(choice)
{
case '1':
document.write("Addition = " + (parseInt(no1)
+parseInt(no2)));
break;
case '2':
document.write("Subtraction = " + (no1-no2));
break;
case '3':
document.write("Multiplication = " + (no1*no2));
break;
case '4':
document.write("Division = " + (no1/no2));
break;
default:
document.write("Invalid Choice");
}
</script>
</body>
</html>
3) Write the use of following String methods:
//output : epak
2) split() split method, which splits a string <html>
into an array of substrings based <head>
on a specified separator. Each <script type =“text/javascript”>
substring in the resulting array let str = "Hello, world!";
corresponds to a portion of the let parts = str.split(", "); // Splitting at ', '
original string delimited by the document.write(parts);
separator. </html>
</head>
</script>
//Output : 65
4) fromCharCode we can create a character from its <html>
Unicode value using the <head>
fromCharCode method. <script type=“text/javascript”>
var a=String.fromCharCode(65);
document.write(a);
</script>
</body>
</html>
Output : A
4) Write a program to print all even and odd numbers from the range of 1 to 20.
Ans :-
<html>
<body>
<script type ="text/javascript">
var i;
document.write("<br>"+"Even numbers are :")
for(i=1;i<=20;i++)
{
if(i%2==0)
{
document.write("<br>"+i);
}
}
document.write("<br>"+"Odd numbers are :")
for(i=1;i<=20;i++)
{
if(i%2!=0)
{
document.write("<br>"+i);
}
}
</script>
</body>
</html>
6) Write a Program to initialize array of integers and display their double values
<html>
<body>
<script type ="text/javascript">
const arr = [10,20,30,40];
const doublevalues=arr.map(num => num * 2);
document.write('Original integers: ' + arr.join(', ') + '<br>');
document.write('Doubled values: ' + doublevalues.join(', ') + '<br>');
</script>
</body>
</html>
7) Write a JavaScript code to accept a string and pass it to function. Function should display
length of accepted string. Also Display message if string is less than 10 characters or not.
<html>
<body>
<script type="text/javascript">
var a =prompt("Enter a String");
function disLength(n)
{
if(n.length<10)
{
return "String is less than 10 characters";
}
else
{
return "length of the string is "+n.length;
}
}
var result = disLength(a);
document.write(result);
</script>
</body>
</html>
Ans :-
<html>
<head>
<title>Prime Number Checker</title>
</head>
<body>
<script>
var number = parseInt(prompt("Enter a number:"));
var isPrime = true;
if (number < 2)
isPrime = false;
for (var i = 2; i < number; i++) {
if (number % i === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
document.write(number + " is a prime number.");
}
else {
document.write(number + " is not a prime number.");
}
</script>
</body>
</html>