Professional Documents
Culture Documents
191202C0058 Exp5
191202C0058 Exp5
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";
Exercise:
1. Write a program to check whether the given staring is palindrome or not?
192020058
Code
<html>
<head> <title> Palindrome </title>
</head>
<body>
<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>
192020058
<title>Count Vowels</title>
</head>
<body>
<script>
function getVowels(string) {
var Vowels = 'aAeEiIoOuU';
var vowelsCount = 0;
for(var i = 0; i < string.length ; i++) {
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.
192020058
192020058