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

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] [codebyte] [/codebyte] } 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); }

@mega9537936112

is there a constraint you have with the problem statement you’re doing currently?

If no, you can consider changing the variable type to global instead, (I.e., var loc) so the variables can be accessed outside the defined function environment.

I can’t use var type parameters

Is this your personal project? Or a Codecademy’s coding challenge?

It’d be best if you can direct me to the link.

Thanks.

is a project

Why not destructure the results from the first function with array destructuring (since your function is returning an array):

const [loc, forn, ref] = onSame();

Then call the 2nd function with those parameters (loc, forn, ref). Or you could also use let/const in the top scope which would be accessible in both functions.

this?

const lista = document.getElementById('lista'); const original = document.getElementById('clone'); let i = 0; function duplicar() { const clone = original.cloneNode(true); clone.id = "clone" + i++; const loc = document.getElementsByClassName('copyLoc')[0].value; const forn = document.getElementsByClassName('copyRefForn')[0].value; const ref = document.getElementsByClassName('nossaRef')[0].value; const [loc, forn, ref] = onSame(); } function onSame(loc, forn, ref) { clone.getElementsByClassName('clonedLoc')[0].innerText = loc; clone.getElementsByClassName('clonedForn')[0].innerText = forn; clone.getElementsByClassName('clonedRef')[0].innerText = ref; lista.appendChild(clone); }

No. You would destructure the array returned from onSame at the top level of your program. Right now in that change onSame is no longer returning anything - in first example it was returning an array. Then you have these variables in the top scope and can call the other function with them. So I would go back to first example and: Do the array desctructuring from onSame() outside of both functions. Then call duplicar with the 3 parameters. Then change duplicar to use these 3 parameters to make the clone.

2 Likes

@mike7127143742 meant,

// Line 23 - just return the array of variables
return [loc, forn, ref]

// Line 27 - remove the parameters and stay with the first example you shared before
function onSame()

/** 
 * Some where above line 7 onward, declare the relevant variables and call via destructuring method.
 *
 * Later on, within the specific function, just call the specific variable(s) accordingly towards your components.
 */
const [ var1, var2, var3 ] = target_function();

Cheers to Mike!

2 Likes

@mike7127143742 and @estforesta Thank you so much for all your support, I’m still taking my first steps in JS.

2 Likes