Page 227 - Web Applications (803) Class 12
P. 227
}
Output: Even Number
11. Write a script using JavaScript which accepts 2 strings from the user and passes them to a function. The function
checks whether they are anagram of each other or not. [Hint: Two strings are called anagram if they contain the
same characters but in different order e.g. “listen” and “silent”.
Ans. <html>
<body>
<script>
function check(str1, str2) {
return cleanstr(str1) === cleanstr(str2);
}
function cleanstr(str) {
return str.replace(/\s/g, '').toLowerCase().split('').sort().join('');
}
var s1 = prompt("Enter the first string:");
var s2 = prompt("Enter the second string:");
if (check(s1, s2))
document.write(s1+" and "+s2+" are anagrams");
else
document.write(s1+" and "+s2+" are not anagrams");
/* The function cleanstr removes spaces and converts the strings to lowercase for case-insensitive comparison. It
then checks if the sorted characters of both strings are the same. */
</script>
</body></html>
Output:
12. Differentiate between “var” and “const”. Also give an example.
Ans.
var const
1. Variables declared using var can be reassigned 1. Used to declare constants. Once a value is
i.e. the values of the variables can be changed. assigned, it cannot be changed.
2. var variables can be accessed anywhere within 2. const variables can only be accessed within the
the function in which they are declared. block of code in which they are declared.
Example: Example:
var ab= “Tanisha” const p=2.4
ab = “Sumit” //This is allowed p = 31 //This is not allowed
Web Scripting—JavaScript 225

