4

Hi I'm new to Javascript and I'm really confused at how to actually call functions. I'm not sure why this isn't working and would love some input.

My HTML :

<html>
<head>
    <script type="text/javascript" src="problem2.js"></script>
</head>
<body>
<INPUT TYPE="button" NAME="Answer" VALUE="Click to see Answer" onClick="MyFunction(array);">
    <div id="answer"></div>
</body>
</html>

My JavaScript :

var array = [3, 1, 1, 'a', 'a', 3, 'b', 'f', 'a', 1, 'a'];

function MyFunction(array) {
    var counter = 0,
        amount = 1,
        highest;

    for (var i=0; i<array.length; i++) {
        for(var j=i; j<array.length;j++) {
            if (array(i) ===array(j)){
                counter++;
            }
            if(counter>amount){
                amount=counter;
                highest=array[i];
            }
        }
        counter = 0;
    }
    document.getElementById("answer").innerHTML(highest + " " + amount + times");
}
1

2 Answers 2

4

You have several errors in your function, first to access to an array item you should use bracket [] and not (), e.g :

if (array[i] === array[j]){
     counter++;
}

You miss a double quote " in the following line, also to assign HTML using innerHTML you should follow it by = :

document.getElementById("answer").innerHTML(highest + " " + amount + times");

Should be :

document.getElementById("answer").innerHTML = highest + " " + amount + "times";

And you don't have to pass the array from HTML since the array already defined in JS code so just use it :

<INPUT TYPE="button" NAME="Answer" VALUE="Click to see Answer" onClick="MyFunction();">

//Define function without argument
function MyFunction() {
   //your code here
}

Hope this helps.


Working Snippet

var array = [3, 1, 1, 'a', 'a', 3, 'b', 'f', 'a', 1, 'a'];

function MyFunction() {
  var counter = 0;
  var amount = 1;
  var highest;
  var i;
  for (i=0; i<array.length; i++)
  {
    for(var j=i; j<array.length;j++){
      if (array[i] === array[j]){
        counter++;
      }

      if(counter>amount){
        amount=counter;
        highest=array[i];
      }
    }
    counter = 0;
  }

  document.getElementById("answer").innerHTML = highest + " " + amount + "times";
}
<INPUT TYPE="button" NAME="Answer" VALUE="Click to see Answer" onClick="MyFunction();">
  <div id="answer"></div>

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

Comments

0

Working copy of the code you provided & a FIDDLE for it

<body>
  <script>
    var array = [3, 1, 1, 'a', 'a', 3, 'b', 'f', 'a', 1, 'a'];

    function MyFunction(array) {
      var counter = 0;
      var amount = 1;
      var highest;
      var i;
      for (i = 0; i < array.length; i++) {
        for (var j = i; j < array.length; j++) {
          if (array[i] === array[j]) {
            counter++;
          }

          if (counter > amount) {
            amount = counter;
            highest = array[i];
          }
        }
        counter = 0;
      }

      document.getElementById("answer").innerHTML = highest + " " + amount + " times";
      }

  </script>
  <INPUT TYPE="button" NAME="Answer" VALUE="Click to see Answer" onClick="MyFunction(array)">
  <div id="answer"></div>

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.