I write a bubble sort function to arrange numbers in the array, but when I put array like [1,3] ,vscode and chrome give me the
Identifier expected.javascript OR Uncaught SyntaxError: Invalid destructuring assignment target
here is the full code
function bubbleg(arr) {
var len = arr.length;
for (var i = 0; i < len - 1; i ) {
for (var j = 0; j < len - 1 - i; j ) {
if (arr[j] > arr[j 1]) {
var temp = arr[j];
arr[j] = arr[j 1];
arr[j 1] = temp;
}
}
}
return arr;
}
function bubbleg([1, 3]);
After make a change, like
num= [1,3] function bubbleg(num);
Everything is good, but why we can not put the something like bubbleg([1,3])? Is there any books so when I look it up with some bugs list in it ?
CodePudding user response:
remove function keyword in calling your function. function
is keyword used to declare function, not to call it.
function bubbleg(arr) {
var len = arr.length;
for (var i = 0; i < len - 1; i ) {
for (var j = 0; j < len - 1 - i; j ) {
if (arr[j] > arr[j 1]) {
var temp = arr[j];
arr[j] = arr[j 1];
arr[j 1] = temp;
}
}
}
return arr;
}
bubbleg([1, 3]);
CodePudding user response:
that is because you're not calling the function bubbleg correctly
function bubbleg([1,2])
should be
bubbleg([1,2])
CodePudding user response:
It is not about the [1, 3]
argument you pass. But how you call the function.
Here is the correct code:
function bubbleg(arr) {
var len = arr.length;
for (var i = 0; i < len - 1; i ) {
for (var j = 0; j < len - i - 1; j ) {
if (arr[j] > arr[j 1]) {
var temp = arr[j];
arr[j] = arr[j 1];
arr[j 1] = temp;
}
}
}
return arr;
}
console.log(bubbleg( [3,1] ));