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

Long Answers

Uploaded by

Aayush Chavanke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Long Answers

Uploaded by

Aayush Chavanke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

2) Write a program. to perform addition, subtraction, multiplication and division.

(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:

1) substring() 2) split() 3) charCodeAt() 4) fromCharCode()

Method Use Example


1) substring() substring() can be used to extract <html>
part of the String. The substring() <head>
method extracts characters, <script type =“text/javascript”>
between to indices (positions), let string “Deepak”;
from a string, and returns the let result = string.substr(2);
substring. document.write(result);
</html>
</head>
</script>

//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: ["Hello", "world!"]


3) charCodeAt() In JavaScript, you can find the <html>
Unicode value of a character <head>
using the charCodeAt method, <script type=“text/javascript”>
which returns the Unicode value var str="A"
of the character at the specified var
index. a=str.charCodeAt(0);
document.write(a);
</script>
</body>
</html>

//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>

8) Write a JavaScript code to display entered number is prime or not

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>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy