Exchanging elements in arrays with a function. How to do?

Asked

Viewed 1,016 times

3

I apologize in advance for the beginner’s doubt, but I’m stuck in the next problem. The exercise asks us to declare a function called "replace" that has two arrays (arrayA, arrayB) and then remove the last element of the first (arrayA) and add it to the second (arrayB).

Being:

var arrayA = [1, 2, 3];
var arrayB = [4, 5];

Now, using pop and push, my code is like this:

var arrayA = [1, 2, 3];
var arrayB = [4, 5];
arrayA.pop ();
arrayB.push (3);

The point is I need to put within of a function or with a function (I really don’t know how the exercise wants us to do it) and I have no idea how to fit it into a function. So far I’ve tried this, and it only makes a mistake:

var arrayA = [1, 2, 3];
var arrayB = [4, 5];
arrayA.pop ();
arrayB.push (3);


function substituir (){
var resultado = (arrayA,arrayB);
return resultado;
}

Any idea how to solve this problem?

4 answers

4


Your question is a little vague, so this is just a way to make this substitution using a function:

// recebe dois arrays por parametro e troca a última possição
// do primeiro para o segundo
function substituir(array_a, array_b){
  array_b.push(array_a.pop())    
}

var arrayA = [1, 2, 3];
var arrayB = [4, 5];

substituir(arrayA, arrayB)

console.log("Array A", arrayA)
console.log("Array B", arrayB)

  • Luigi you are a superhero! The question of the problem is really vague and left me extremely confused, but this solution worked 100 percent!! Thank you so much!!

1

Some remarks:

The following excerpt from your code arrayB.push (3); works only when the last element of the arrayA is number 3. In any other situation, the code produces a logical error.

On this part var resultado = (arrayA,arrayB);, i don’t know what your intention is with it, or if you know you’re using comma operator, but it just assigns the value of the arrayB to the result variable.

As for the solution of the problem, you can try something simple:

function substituir(arrA, arrB){
  let x = arrA.pop();
  arrB.push(x);
}

Or a little more lean:

function substituir(a, b){ b.push(a.pop());}

Or you can test the size of the first array, if you don’t want the second array to fill with undefined:

function substituir(arrA, arrB){
  if(arrA.length > 0){
    let x = arrA.pop();
    arrB.push(x);
  }
}

0

Only use the pop with the push:

array_b.push(array_a.pop());

0

I’m a beginner too, but here’s my solution to the problem:

// Used on ES6 or above;

const myArrayA = ['A', 'B', 'C', 'D']; // Array de uma dimensão "String"
const myArrayB = [1, 2, 3, 4]; // Array de uma dimensão "Number"

// Função que retorna um novo Array
const criarNovo = (arg1, arg2) => {
    const [one, ...outros] = arg2; // Usando destructuring para pegar argumentos, ignorando o primeiro.
    for (let i in outros) { //Percorrendo o Array gerado 
        arg1.push(outros[i]); // Adiciona dentro do arg1 que representa o Array o primeiro Array ou segundo, cada item do segundo argumento;
    }
    return arg1; // Retorna um novo Array, caso não retorne nada a função retornara undefined
}

const myArrayC = criarNovo(myArrayA, myArrayB); // Aqui você escolhe quem recebe quem;
console.log(myArrayC); // Log 

Browser other questions tagged

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