Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2 (15 pts.). Write an HTML document prob2.html that loads a JavaScript file prob

ID: 3873856 • Letter: 2

Question

2 (15 pts.). Write an HTML document prob2.html that loads a JavaScript file prob2.js. The JavaScript program should prompt the user for a string that contains three values separated by any amount of whitespace. There may also be any amount of whitespace (including, in this case, none) before the first value and after the last. The first value on a line should be in a floating point format (explained in detail below), the second value should be a signed or unsigned decimal integer (see below), and the third value should be one of the single letters x, 'y, or z You write a regular expression intended to match an entire line (so it begins with 'A and ends with 'S'). Keep running sums of the floating-point numbers represented by the first values and of the integers represented by the second values in a line. Also, maintain a string onto which the third values are concatenated. If one or more of the values in a line are improperly formatted, output that line preceded by the string "No match with "; in this case, the entire line is ignored (making no contribution to the accumulated values) The format for the integer value is simple: either (1) an optional sign followed by a string of digits beginning with a digit other than '0 or (2) the string consisting only of 0 The format for floating-point values is very versatile. There is an optional initial sign. Then, before the decimal point (if there is one), there is either the character 0 or a string of digits beginning with a digit other than '0". There are two possibilities for the remainder. One possibility is a decimal point followed by one or more digits. The other possibility is an optional decimal point and one or more digits, then or e, and then a string of one or more digits optionally preceded by a sign. The following are examples of valid floating-point representations 12.34 32.2E2 4.23e-10 62E+2 0.0 The following are examples of representations that fail to meet this format. 12.E2 (There is no digit after the decimal point.) 062.42 (The initial '0 is invalid.) The following is the strings that you can use to test your program, which you can download from the assignment site 12.34 -79 x -3.5 23 y 32.2E2 2 z 4.23e-10 +45 x 62E+2 -4 Y 0.0 0 z 12.E2-125 x 062.42 12987 y 3.45 12.5 z -45.2 06 x

Explanation / Answer

1. prob2.html

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<script src="prob2.js"></script>

<body>

<h1>Enter String(s) to validate</h1>

<textarea id="Text1" cols="100" rows="10"></textarea>

<button>Cancel</button>

<h1>Result</h1>

<textarea id="Result" cols="100" rows="10" disabled="disabled"></textarea>

</body>

</html>

2. prob2.js

/**

*

*/

function validateString(){

var lines = document.getElementById('Text1').value.split(' ');

var result="";

for(var i = 0;i < lines.length;i++){

//code here using lines[i] which will give you each line

var re = new RegExp("^\s*([-+]?([1-9]+[0-9]*||[0-9]{1})\.?[0-9]+([eE][-+]?[0-9]+)?)\s*([-+]?([1-9]+[0-9]*)||[0-9]{1})\s*[xyz]\s*$");

if (re.test(lines[i])) {

result=result+"Matching with "+lines[i]+" ";

} else {

result=result+"No match with "+lines[i]+" ";

}

}

document.getElementById("Result").value=result;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote