Transfer data from one function to another?

I have two functions duplicar and onSame, I need to use the data from onSame function in the duplicar function, but I don’t know how to do someone could give a clue please?

Thanks

const lista = document.getElementById('lista');
const original = document.getElementById('clone');
let i = 0;

function duplicar()
{
  const clone = original.cloneNode(true); 
  clone.id = "clone" + i++;

  clone.getElementsByClassName('clonedLoc')[0].innerText = loc;
  clone.getElementsByClassName('clonedForn')[0].innerText = forn;
  clone.getElementsByClassName('clonedRef')[0].innerText = ref;

  lista.appendChild(clone);

}
function onSame() {
  const loc = document.getElementsByClassName('copyLoc')[0].value;
  const forn = document.getElementsByClassName('copyRefForn')[0].value;
  const ref = document.getElementsByClassName('nossaRef')[0].value;
}
  1. Return values from function onSame
  2. function duplicar should take as parameters those results.

I’ll just show a hint:

function duplicar(els){
//do stuff
};
function onSame() {
//do stuff
return [loc,forn,ref]
}

There are some other things to improve, but that’s a first step.

Or also:

function duplicar(loc,forn,ref){
//do stuff
};

function onSame() {
//do stuff
return [loc,forn,ref]
}
//execute as
duplicar(...onSame())

Very thanks, I tried this solution but have “undefined” results

function duplicar(loc,forn,ref)
{
  const clone = original.cloneNode(true); 
  clone.id = "clone" + i++;

  clone.getElementsByClassName('clonedLoc')[0].innerText = loc;
  clone.getElementsByClassName('clonedForn')[0].innerText = forn;
  clone.getElementsByClassName('clonedRef')[0].innerText = ref;

  lista.appendChild(clone);
}


function onSame() {

  return[loc, forn, ref]
  const loc = document.getElementsByClassName('copyLoc')[0].value;
  const forn = document.getElementsByClassName('copyRefForn')[0].value;
  const ref = document.getElementsByClassName('nossaRef')[0].value;
}

duplicar(onSame())

Hi.
We read from the 1st line at the top of the page going down, line by line.
A computer also processes Code in the same order.
(I’m not being funny, just making a point).
Coding is a set of instructions for the computer to follow, from the top down.

Let me give an example from every day life:-
If you go to the shop to buy fresh milk, 18 eggs and brown bread, what do you do?

  1. Go to the shop
  2. Buy the fresh milk, 18 eggs and brown bread.
  3. Return home (with the fresh milk, 18 eggs and brown bread).

What you are telling your function to do is:-

  1. Go to the shop - function onSame()
  2. Return home (with milk, eggs and bread) - return[loc, forn, ref] … but what milk, eggs and bread . . . you did not buy anything yet!!!
  3. Buy fresh milk, 18 eggs and brown bread - const loc, const forn and const ref (I’m not going to copy all that stuff…)

When you return home, you are NO LONGER IN THE SHOP, therefore you cannot buy anything after you have RETURNED HOME. In the same way, once a Function RETURNS a value, nothing else is executed in that function. It’s like leaving the building. It cannot do any of the lines that follow the RETURN statement.

Your values are assigned in the following lines, but they do not happen, because they are After the Return statement.

  const loc = document.getElementsByClassName('copyLoc')[0].value;
  const forn = document.getElementsByClassName('copyRefForn')[0].value;
  const ref = document.getElementsByClassName('nossaRef')[0].value;

Hope it helps.

Thank you very much for your help and your time, I am very grateful, I am taking my first steps in JS.

I tried like you said, putting pattern at the end of the function but the result I get is undefined. I don’t know what I could be doing wrong.

function duplicar(loc, forn, ref)
{
  const clone = original.cloneNode(true); 
  clone.id = "clone" + i++;

  clone.getElementsByClassName('clonedLoc')[0].innerText = loc;
  clone.getElementsByClassName('clonedForn')[0].innerText = forn;
  clone.getElementsByClassName('clonedRef')[0].innerText = ref;

  lista.appendChild(clone);
}


function onSame() 
{

  const loc = document.getElementsByClassName('copyLoc')[0].value;
  const forn = document.getElementsByClassName('copyRefForn')[0].value;
  const ref = document.getElementsByClassName('nossaRef')[0].value;

  

  return[loc, forn, ref]
  
}

Did you come right. Sorry I have not been on the forum for a while.

Can you send the code that calls these 2 functions pls.
Is the ‘onSame’ function called before the ‘duplicar’ function?
Also, the loc, forn & ref variables are not passed to the ‘onSame’ function. So it would modify the global variables of the same name, or those variables in the function that calls ‘onSame’.

Do you understand the SCOPE of variables?
If a program is like a filing cabinet( let’s name it MainCabinet), then:-
Global variables are visible to all drawers and hanging files in MainCabinet.
A function (let’s name it FnDrawer), called from MainCabinet is like a drawer in the filing cabinet.
A function’s variables are visible to all hanging files in the drawer (FnDrawer) in which it resides.
A function (let’s name it SubFunction), called from FnDrawer is visible to itself and any functions it calls. You could think of envelopes inside the hanging files, for the functions called by SubFunction.
A hanging file in the top drawer cannot access or modify variables in the 2nd drawer.
A hanging file in a drawer can only show it’s variables to the drawer by RETURNING them (which you have done in ‘onSame’). But, which variables are being changed by ‘onSame’? No parameters are being passed to it, so it is modifying the variables from it’s calling function (or global, if you made those 3 variables global).

Check the scope of your variables in your functions.
Tip: you can change the loc to loc1 in a certain function, just to clarify in your mind, which loc you are referring to (same with the other 2 variables).

Eg.
Main:
loc1 = firstvalue; forn1 etc
onSame:
loc2 = all that good stuff (don’t change the good stuff, okay)
duplicar:
loc = all that other good stuff (as required in your challenge)

Note:
In ‘duplicar’, you passed the 3 variables to it as loc, forn and ref. So no change needed there.
In ‘onSame’, nothing is passed. Just saying. I think I’m repeating myself now. I’ll let you have a go at it . . . .