0

When I run this, it claims x is undefined on the on the line with the for loop.

Full code:

function getCustomerNumbers() {
    var customerNumbers = [];
    customerNumbers.push(12, 17, 24, 37, 38, 43);
    return customerNumbers;
}
function getWinningNumbers() {
    var winningNumbers = [];
    winningNumbers.push(12, 17, 24, 37, 38, 43);    
    return winningNumbers;
}
function checkNumbers(x, y) {    
    var matches = 0;
    for (i=0; i<x.length; i++) {
        for (j=0; j<y.length; j++) {
            if (x[i] == y[j]) {
                matches++;
            }
        }
    }
    return matches;
}
function displayResult() {
    checkNumbers(getWinningNumbers(), getCustomerNumbers())
    alert("This Week's Winning Numbers are:\n\n" + getWinningNumbers().toString() + 
    "\n\nThe Customer's Number is:\n\n" + getCustomerNumbers().toString() + "\n\nNumber of Matches:" + checkNumbers());
}
function init() {
    displayResult();
}
window.onload = init;

Later it runs, with arrays going into values x and y. It ran fine when it was just x as an array and one for loop.

Anyone know what is wrong here?

14
  • 1
    What are you putting in for x when it doesn't run? ".length" only works on arrays or certain collections. Commented Feb 12, 2015 at 16:24
  • 1
    Then how are you calling it? We can't see your code. Commented Feb 12, 2015 at 16:26
  • 2
    function checkNumbers(var x, var y) -- why do you have var listed in the parameters? It should be function checkNumbers( x, y) Commented Feb 12, 2015 at 16:30
  • 1
    @ps2goat You've just found the missing vars for i and j? Commented Feb 12, 2015 at 16:32
  • 1
    Check my answer. you're not passing anything in for x and y on the second call to checkNumbers Commented Feb 12, 2015 at 16:40

2 Answers 2

2

Your posted code is still missing something, as the first function is missing.

The next thing I found was the second call to checkNumbers doesn't pass anything in.

function displayResult() {
    checkNumbers(getWinningNumbers(), getCustomerNumbers())
    alert("This Week's Winning Numbers are:\n\n" + getWinningNumbers().toString() + 
    "\n\nThe Customer's Number is:\n\n" + getCustomerNumbers().toString() + "\n\nNumber of Matches:" + checkNumbers()); //<-- where are the parameters?
}

Working code:

function getCustomerNumbers(){
var customerNumbers = [];
    customerNumbers.push(12, 17, 24, 37, 38, 43);
    return customerNumbers;
}
function getWinningNumbers() {
    var winningNumbers = [];
    winningNumbers.push(12, 17, 24, 37, 38, 43);    
    return winningNumbers;
}
function checkNumbers(x, y) {    
    var matches = 0;
    for (var i=0; i<x.length; i++) {
        for (var j=0; j<y.length; j++) {
            if (x[i] == y[j]) {
                matches++;
            }
        }
    }
    return matches;
}

function displayResult() {    
    alert("This Week's Winning Numbers are:\n\n" + getWinningNumbers().toString() + 
    "\n\nThe Customer's Number is:\n\n" + getCustomerNumbers().toString() + "\n\nNumber of Matches:" + checkNumbers(getWinningNumbers(), getCustomerNumbers())
         );
}

displayResult();

Sign up to request clarification or add additional context in comments.

2 Comments

apologies, left out the top line where I called the function
Then the code you posted is different from what you're using. My snippet works.
0

You're calling checkNumbers twice - the first time passing in valid values for x & y and the second time passing in nothing

function displayResult() {
    // Good, but ignores result
    checkNumbers(getWinningNumbers(), getCustomerNumbers()) 
    //Nothing passed in
    alert("This Week's Winning Numbers are:\n\n" + getWinningNumbers().toString() + 
    "\n\nThe Customer's Number is:\n\n" + getCustomerNumbers().toString() + "\n\nNumber of Matches:" + checkNumbers()); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.