Syntax error in Javascript

Asked

Viewed 188 times

3

I am testing an excerpt of code here in javascript and is giving syntax error on line 12. It says that is missing a ; somewhere, but I’ve already reviewed that code and I can’t find the error.

This code was originally written in C and I’m trying to adapt to a specific problem using javascript. I changed values defined as integers to var, I’ve already changed to type char since javascript is weakly typed and the == means that "circumvents" the type differences of the variables.

    var arr = ["1", "2", "3", "4", "5"];
    var r = "3";
    var n = (arr.length)/(arr[0].length);
    var data[r];

    function combinationUtil(var arr[], var data[], var start, var end, var index, var r)
    {
      // Current combination is ready to be printed, print it
      if (index == r)
      {
        for (var j=0; j<r; j++){
          console.log(data[j]);
          //document.getElementById("demo").innerHTML = data[j];

        }

      }

      // replace index with all possible elements. The condition
      // "end-i+1 >= r-index" makes sure that including one element
      // at index will make a combination with remaining elements
      // at remaining positions
      for (var i=start; i<=end && end-i+1 >= r-index; i++)
      {
        data[index] = arr[i];
        combinationUtil(arr, data, i+1, end, index+1, r);

      }
    }

    combinationUtil(arr, data, 0, n-1, 0, r);
    <p id="demo"></p>

  • What would that line 12 be?

  • Sorry for the delay in responding. It is the line that indicated below that contains var data[r];

2 answers

4

Removes the var and the [] function parameters combinationUtil, because the way it is it makes a syntax error.

function combinationUtil(arr, data, start, end, index, r)
{

    if (index == r)
    {
        for (var j = 0; j < r; j++) {
            console.log(data[j]);
        }

    }

    for (var i = start; i <= end && end-i+1 >= r-index; i++)
    {
        data[index] = arr[i];
        combinationUtil(arr, data, i+1, end, index+1, r);

    }
}
  • Thank you for the reply.

3


in addition to the syntax error pointed out by @Yurepereira, we still have Line 12 Error:

var data[r];

You are declaring this array wrong, in this case you have two options:

Without specifying the size of the array, let Javascript define this in Runtime.

var data = [];

Create an Array object with size r.

var data = new Array(r);

Below is your working script.:

var data = [];
var arr = [1, 2, 3, 4, 5];
var r = 3;
var n = arr.length;

var demo = document.getElementById("demo");

function combinationUtil(arr, data, start, end, index, r)
{
  // Current combination is ready to be printed, print it
  if (index == r)
  {
    var demo = document.createElement("div");
    demo.innerHTML = JSON.stringify(data.slice(0, r));
    document.body.appendChild(demo);
  }

  // replace index with all possible elements. The condition
  // "end-i+1 >= r-index" makes sure that including one element
  // at index will make a combination with remaining elements
  // at remaining positions
  for (var i=start; i<=end && end-i+1 >= r-index; i++)
  {
    data[index] = arr[i];
    combinationUtil(arr, data, i+1, end, index+1, r);
  }
}

combinationUtil(arr, data, 0, n-1, 0, r);

  • Thank you so much for the most complete reply with the code running.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.