Training Guide: Programming in HTML5 with JavaScript and CSS3 Ebook

(Nora) #1

132 CHAPTER 3 Getting started with JavaScript


var expected = '10';
equal(txtResult.value, expected, 'Expected value: ' + expected +
' Actual value: ' + txtResult.value);
expected = '0';
equal(txtInput.value, expected, 'Expected value: ' + expected +
' Actual value: ' + txtInput.value);

});

Notice that in these tests, the expect function call is passed a value of two because you
now have two assertions.


  1. Press F5 to run the test and verify that the test fails because you haven’t added code to
    the plusClick and minusClick functions that will reset txtInput.

  2. At the bottom of the plusClick and minusClick functions, call the clearEntry function.
    Your code should look like the following:
    function plusClick() {
    txtResult.value = Number(txtResult.value) + Number(txtInput.value);
    clearEntry();
    }


function minusClick() {
txtResult.value = Number(txtResult.value) - Number(txtInput.value);
clearEntry();
}


  1. Press F5 to run the test, which should pass.
    When the application starts, txtInput and txtResult should be initially set to zero.

  2. Add a test to the top of tests.js that checks txtInput and txtResult.
    Your test code should look like the following:
    test("Initialize Test", function () {
    expect(2);
    txtInput.value = '';
    txtResult.value = '';
    var expected = '0';
    equal(txtInput.value, expected, 'Expected value: ' + expected +
    ' Actual value: ' + txtInput.value);
    equal(txtResult.value, expected, 'Expected value: ' + expected +
    ' Actual value: ' + txtResult.value);
    });

  3. Press F5 to run the test and verify that the test fails because you haven’t added code to
    the initialize function to initialize txtInput and txtResult.

  4. At the bottom of the initialize function, add a call to the clear function to initialize
    txtInput and txtResult.
    Your code should look like the following:
    function initialize() {
    for (var i = 0; i < 10; i++) {

Free download pdf