Create a program to read the string and print string length using various length properties in javascript. Let’s create a simple web page to design and perform read string and print string length using HTML, CSS, JS.
Problem statement
In this, we have to read the string length and print length using the javascript function. For that create an HTML file and use the JS function that displays the output.
Solution
- First, create an HTML file
- Give the heading as Read the length of string using <h2></h2> tag
- Display string in <p></p> tag Find the length of the string- “Self-belief and hard work will always earn you success.”
- Add bold <b></b>tag for the length os string sentense
- Create id=”demo” in <p></p> tag
- Declare variable text using the let text = “Self-belief and hard work will always earn you success.”;
- Use length property to return length of string let length = text.length;
- Display output by id use document.getElementById(“demo”).innerHTML = length;
JS code to read the string and print string length
<!DOCTYPE html> <html> <title>JavaScript Read n Strings</title> <body style="text-align:center;"> <h2 style="color: blue;">Read the length of string</h2> <p style="font-weight: bold; color: orangered;">Find the length of string- "Self-belief and hard work will always earn you success."</p> <b>The length of string is :</b> <p id="demo"></p> <script> let text = "Self-belief and hard work will always earn you success."; let length = text.length; document.getElementById("demo").innerHTML = length; </script> </body> </html>
Output
Program to return the length of an array in JavaScript
Problem statement
Create a program to return the length of the string using the javascript function. For that create an HTML file and use js functions that display output.
Solution
- Create HTML file
- Enter heading as Return Length of Array in <h2></h2> tag
- Initialize array using var array = new Array( 99 , 98 , 97, 96 , 95)
- Print the given array using document.write() on output screen document.write(“</br>”+”Array – “+ array+”</br>”);
- For length of array use array.length() and print output using document.write(“</br>”+”Length of array is : ” + array.length);
JS code to print an array of length
<html> <head> <title>JavaScript Return Array length</title> </head> <h2 style="font-style: initial; color: darkred; font-size: x-large;">Return Array length </h2> <body style="text-align: center;"> <script type = "text/javascript"> var array = new Array( 99 , 98 , 97, 96 , 95); document.write("</br>"+"Array - "+ array+"</br>"); document.write("</br>"+"Length of array is : " + array.length); </script> </body> </html>
Output
Program to find the Length of the largest substring
Problem statement
In this task create a program to find the length of the largest substring which has a character with a frequency greater than or equal to half of the substring. For that create an HTML file and use the JS function to display output.
Solution
- Create HTML file
- Enter heading as Length of largest string without reapiting characters in <h2></h2><b></b> tags
- Initialize the string using var str = “aashcash”; and print it document.write(“Original string-“+ str);
- Initialize array var arr = {}; , count var count = 0; , set maximum count to 0 maxCount = 0; Initialize count var count = 0;
- Use for loop for (var i = 0; i < str.length; i++) in that if array of string [i] count is greater than maximum count if (arr[str[i]]) {
if (count > maxCount) {
maxCount = count
and maximum count is equal to count then count is equal to 0 count = 0;
arr = [];
then count increase and array of string value is true count++;
arr[str[i]] = true; - Print output using document.write(“</br>”+”Largest string length-“+maxCount);
JS code to find the length of the largest substring
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Find length of largest string</title> <h2><b>Length of largest string without repeating characters </b></h2> </head> <body> <script> var str = "aashcash"; document.write("Original string-"+ str); var arr = {}; maxCount = 0; var count = 0; for (var i = 0; i < str.length; i++) { if (arr[str[i]]) { if (count > maxCount) { maxCount = count } count = 0; arr = []; } count++; arr[str[i]] = true; } document.write("</br>"+"Largest string length-"+maxCount); </script> </body> </html>
Output
In this way, we will learn how to read the string print strings length using javascript.