1

I used to be a C programmer & new in JavaScript and use pointers to swap elements of an array using

void swap(int *a,int *b){
     int temp=*a;
     *a=*b;
    *b=temp;

 }

What will be the equivalent function of this in JavaScript?

  • 1
    This may nasser you question: https://stackoverflow.com/questions/872310/javascript-swap-array-elements – Thallius Jun 13 '21 at 13:39
  • 1
    You cannot do that in JavaScript, at least not in a way that's a general as how it can be done in C or C++. JavaScript doesn't have a pointer type that can be used as flexibly as that. – Pointy Jun 13 '21 at 13:41
  • 1
    Js has no low-level memory management - data is either primitive or reference type (https://www.javascripttutorial.net/javascript-primitive-vs-reference-values/) So, if it is int (number in js), it'll be copied by value. Objects'll be copied by reference. – A Ralkov Jun 13 '21 at 13:48

5 Answers5

2

You can transform your array from [1,2,3] to [{val: 1}, {val:2}, {val: 3}] and use such approach:

const swap = (a, b) => {
  const val = a.val;
  a.val = b.val;
  b.val = val;
}

Array contains the same references with new vals.

A Ralkov
  • 1,046
  • 10
  • 19
1

You can implement some of pointer logic using ArrayBuffer and its views like Uint8Array with helpers like TextEncoder.

var memory = new TextEncoder().encode("HelloWorld");

// create pointers in memory
var H = new Uint8Array(memory.buffer, 0, 1);
var W = new Uint8Array(memory.buffer, 5, 1);

// create string from byte array
const toString = buf => String.fromCharCode.apply(null, buf);

// swap two bytes in memory
function swap(a, b) {
    [a[0],b[0]] = [b[0],a[0]];
}

swap(H, W);
console.log(toString(memory)); // WelloHorld
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
0

In JavaScript you can pass the references of variables by grouping them in an object. So let's say you have two variables x and y... They have to be enclosed in an object.

const values = {
    x : 1,
    y : 3
}

We can pass them as one object to the swap function:

swap(values)

And inside the function we can swap them like so:

function swap (obj) {
    let temp = obj.x
    obj.x = obj.y
    obj.y = temp
}

You can read more about it here

this method has a flaw though as you have to pass the values named x and y to the swap function. We can tweak the function to be more dynamic as follows:

function swap (obj) {
    let temp = obj[Object.keys(obj)[0]]
    obj[Object.keys(obj)[0]] = obj[Object.keys(obj)[1]]
    obj[Object.keys(obj)[1]] = temp
}
0

This code snippet works as an equivalent :

function swap(arr, from, to) {
  arr.splice(from, 1, arr.splice(to, 1, arr[from])[0]);
}

Ref: https://stackoverflow.com/a/52225064/14859542

  • 1
    This is functionally equivalent, but walks through large portion of the array, which does matter in long arrays. Beeing C coder like you, I provided my answer to swap two locations in memory. – Jan Turoň Jun 13 '21 at 14:50
  • I am going through your solution. I will revert to you if I have any confusion. – Satyajit Ghosh Jun 13 '21 at 14:54
-1
let a = 1;
let b = 2;

console.log("Before Swap");
console.log("value a: " + a);
console.log("value b: " + b);

[a, b] = [b, a];

console.log("After swap");
console.log("value a: " + a);
console.log("value b: " + b);

You can use Destructuring Assignment in order to swap a value. I also just learned it. Please correct me if im wrong, im also new to JavaScript