0% found this document useful (0 votes)
6 views3 pages

JS Assi4

This JavaScript code defines four functions (com1, com2, com3, com4) to compare two strings entered by the user in different ways: comparing the uppercase versions, using a regular expression, locale-insensitive comparison, and strict equality. It displays the results of each comparison method in separate HTML paragraphs on the page. The set_string() function calls each comparison function and passes the two strings.

Uploaded by

Aditya Mavle
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)
6 views3 pages

JS Assi4

This JavaScript code defines four functions (com1, com2, com3, com4) to compare two strings entered by the user in different ways: comparing the uppercase versions, using a regular expression, locale-insensitive comparison, and strict equality. It displays the results of each comparison method in separate HTML paragraphs on the page. The set_string() function calls each comparison function and passes the two strings.

Uploaded by

Aditya Mavle
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/ 3

JavaScript: Assignment 4

JavaScript Code
function set_string()
{
var string1 = document.getElementById("st1").value;
var string2 = document.getElementById("st2").value;
com1(string1,string2);
com2(string1,string2);
com3(string1,string2);
com4(string1,string2);

function com1(string1,string2)
{

var ot;
const result1 = string1.toUpperCase() ===
string2.toUpperCase();

if(result1) {
ot="The strings are similar ";

} else {
ot="The strings are not similar ";

document.getElementById("casing").innerHTML=ot;

function com2(string1,string2)
{

const pattern = new RegExp(string1, "gi");


var ot;
// compare the stings
const result2 = pattern.test(string2)

if(result2) {
ot="The strings are similar <br><br>";

} else {
ot="The strings are not similar <br><br>";

}
document.getElementById("regex").innerHTML=ot;
}

function com3(string1,string2)
{

// program to perform case insensitive string comparison


var ot;
const result3 = string1.localeCompare(string2, undefined, {
sensitivity: 'base' });

if(result3 == 0) {
ot="The strings are similar <br><br>";

} else {
ot="The strings are not similar <br><br>";

document.getElementById("locom").innerHTML=ot;
}

function com4(string1,string2)
{

var ot;

if( string1 === string2)


{
ot="The strings are similar <br><br>";

}
else
{
ot="The strings are not similar";

document.getElementById("equality").innerHTML=ot;
}

HTML Code
<!DOCTYPE html>
<html>
<body style="text-align:center">
<h1>String Comparison</h1>
<h1>42231 Pratyush Ingale</h5>
<br>
String 1= <input type="text" id="st1" value=" ">
<br></br>
String 2= <input type="text" id="st2" value=" ">
<br><br>
<button style="background:rgb(255, 255, 255)" id="myBtn"
onclick="set_string()">Compare Strings</button>
<br><br>Result using to uppercase() method= <p id="casing"></p>
<br><br> Result using to RegEx method: <p id="regex"></p>
<br><br> Result using localeCompare method: <p id="locom"></p>
<br><br> Result using Strict Eqaulity method: <p id="equality"></p>

<script src="assignment_4.js"></script>
</body>
</html>

You might also like