In: Computer Science
(Javascript) Write a function that converts the alphabetical characters of first half of each argument to uppercase and returns the result as one string. Number/s and special chars are ignored. If the length of string is not even, append an extra "-" (hyphen) to end of it to make it even.
Included the script in a html file to test
TestScript.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script>
//prompt and read a
string from the user
var str =
window.prompt("Enter a string: ");
//get the length of the
string entered
var strLength =
str.length;
//if the length is not
even add hyphen to the last
if (strLength % 2 != 0)
{
str = str.concat("-");
}
//get the result by
calling the convert_to_uppercase function
var res =
convert_to_uppercase(str)
//display the
result
window.alert(res);
//function that takes
a string as argument and returns the first half in uppercase
function
convert_to_uppercase(str) {
var res = "";
//get each character in first half of the string
for (i = 0; i < (str.length) / 2; i++) {
//convert the character into uppercase
res = res.concat(str.charAt(i).toUpperCase());
}
//comment below if you don't want to add second half to result use
below
res = res.concat(str.substring((str.length) / 2, str.length))
//return the result
return res;
}
</script>
</head>
<body>
</body>
</html>
Output generated:
Code Screenshot: