'expected a identifier instead saw '('. expected a identifier instead saw ')'
I am using bubble sort to sort a array but when I write swap function , I'm getting error when writing the line function swap(arr,j,j+1) I am getting the err" expected ')' to match from '('
function solve(N, arr){
for (var i = 0; i < N; i++){
for(var j = 0; j < (N - i - 1); j++){
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1)
}
}
}
function swap(arr,j,j+1) //getting error here "expected a identifier instead saw '('. expected a identifier instead saw ')'. "[look at the screenshot ][1]
{
var temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
Solution 1:[1]
The problem is that you have an expression j+1 as a parameter in your function.
function swap(arr, j, j + 1)
You cannot do that, you either rename j + 1 to k for example. But why even send them both in the first place if k = j + 1? You could accomplish this by only using
function swap(arr,j)
{
var temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
and calling the function just as
swap(arr,j);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Samuel Olekšák |
