samedi 13 janvier 2018

Check that the lottery result is done by a function called checkNumbers() - JavaScript excercise

Following on from my previous question, this is the next step in the exercise.

Now update your program so that the work of checking the lottery result is done by a function called checkNumbers(). This function should take the customer number and the array of winning numbers as arguments. The customer number should be returned from a function called getCustomerNumber(). The array of winning numbers should be returned from a function called getWinningNumbers(). The display of the results should be done by a function called displayResult(). The whole process should be kicked off by a function called init().

This is what I have so far. Does this fulfill the exercise requirements or is there anything I have missed?

function getCustomerNumber() {
        var customerNumber = 12;
        return customerNumber; 
}

function getWinningNumbers() {
        var winningNumbers = [];
        // Adds the winning numbers to winningNumbers
        winningNumbers.push(12, 17, 24, 37, 38, 43);
        return winningNumbers;
}

// Searches the array to check if the customer number is a winner
function checkNumbers(customerNum, winningNums) {
        for (var i = 0; i < winningNums.length && !match; i++) {
                if (customerNum == winningNums[i]) {
                        match = true;
                        return "We have a match and a winner!";
                } else {
                        return "Sorry, you are not a winner this week.";
                }
        }
}

function displayResult() {
        // Messages that will be shown
        var winningMessage = "This Week's Winning Numbers are:\n\n" + getWinningNumbers() + "\n\n";
        var customerMessage = "The Customer's Number is:\n\n" + getCustomerNumber() + "\n\n";
        var resultMessage = checkNumbers(getCustomerNumber(), getWinningNumbers());
        // Result
        alert(winningMessage + customerMessage + resultMessage);        
}

var match = false;

function init() {
        displayResult();
}

window.onload = init;

Aucun commentaire:

Enregistrer un commentaire