Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Experiment No: 5

Aim Develop Java Script to implement Strings.

Theory: -
The JavaScript string is an object that represents a sequence of characters.
There are 2 ways to create string in JavaScript
1. By string literal
2. By string object (using new keyword)
1) By string literal
The string literal is created using double quotes. The syntax of creating string
using string literal is given below:
var stringname="string value";

2) By string object (using new keyword)


The syntax of creating string object using new keyword is given below:
var stringname=new String("string literal");

Practical related questions:


1. How to remove the white spaces between the strings?
We use Trim () function to remove the white spaces between the strings.

2. How to convert text in lowercase into title case?


We can convert text in lower into title case by
a. By using replace () function
b. By using for loop to title case a string

Exercise:
1. Write a program to check whether the given staring is palindrome or not?

Code

<html>
<head> <title> Palindrome </title>
</head>
<body>

192020058
<script>
function validatePalin(str) {
// get the total length of the words
const len = string.length;
// Use for loop to divide the words into 2 half
for (let i = 0; i < len / 2; i++) {
// validate the first and last characters are same
if (string[i] !== string[len - 1 - i]) {
alert( 'It is not a palindrome');
}
}
alert( 'It is a palindrome');
}
// accept the string or number from the prompt
const string = prompt('Enter a string or number: ');
const value = validatePalin(string);
console.log(value);
</script>
</body>
</html>

Output: -

192020058
2. Write a program to count the number of vowels into the string?

Code: -
<html>
<head>
<title>Count Vowels</title>
</head>
<body>
<script>
function getVowels(string) {
var Vowels = 'aAeEiIoOuU';
var vowelsCount = 0;
for(var i = 0; i < string.length ; i++) {

192020058
if (Vowels.indexOf(string[i]) !== -1) {
vowelsCount += 1;
}
}
return vowelsCount;
}
document.write("The Number of vowels in -"+
" MY name is INDU: "
+ getVowels("I am INDU"));
</script>
</body>
</html>

Output: -

Conclusion:
We have learnt about how to print message and how to perform vowels operation
using alert and prompt.

Marks Obtained Dated Signature of Teacher


Process Related Product Total
(15) Related (50)
(35)

192020058

You might also like