Microsoft
98-382 · Question #37
You are using JavaScript to create a calculator. You create the following HTML. Line numbers are included for reference only. 01 <form id="calculator"> 02 <input type="text" id="a" /> 03 <input…
The completed JavaScript function add() should be: function add() { var a = eval(document.getElementById("a").value); var b = eval(document.getElementById("b").value); document.getElementById("result").value = a + b; }
Working with the Document Object Model (DOM)
Question
You are using JavaScript to create a calculator. You create the following HTML. Line numbers are included for reference only.
01 <form id="calculator">
02 <input type="text" id="a" />
03 <input type="text" id="b" />
04 <input type="text" id="result" />
05 <input type="button" onclick="add()" value="+" />
06 </form>
You must create a function named add() that adds the values in the a and b input elements and displays the result in the result input element. You define the following function in JavaScript:
function add() {
}
You need to complete the body of the function. Which three code segments should you use to develop the solution? To answer, move the appropriate code segments from the list of code segments to the answer area and arrange them in the correct order.
Explanation
The completed JavaScript function add() should be:
function add() { var a = eval(document.getElementById("a").value); var b = eval(document.getElementById("b").value); document.getElementById("result").value = a + b; }
Topics
#getElementById#input values#type conversion#DOM manipulation
Community Discussion
No community discussion yet for this question.