I need to export (for using variables in other scripts and other .js files) the actual value of a variable inside a function, how i can do that?
Global variables with zero values are set, they change their values inside functions, I need to export these values only after the function is executed.
the variables that need to be exported are commented out inside the function.
let numberOfTracks = null;
let tracks = '';
let musicid = null;
let dude = '';
let currentTrack = null;
let pathForTrack = '';
const generateTracksBox = function () {
for (let i = 1; i <= numberOfTracks; i++) {
$(`<div class="container card chartsCard" data-musicid="${i}">
<img class="card-img-top" src="/AppMusic/assets/chartLogo/${i}.svg" alt="Card image cap"></img>
<div class="card-body">
<h5 class="card-title">${tracks[i-1].split('-')[0]}</h5>
<p class="card-text">${tracks[i-1].split('-')[1]}</p>
</div>
</div`).appendTo('.charts');
}
const dude = $('.chartsCard');
for (let i = 0; i < dude.length; i++) {
dude[i].onclick = function () {
currentTrack = $(dude[i]).data('musicid'); //need to export
location.href = "music/player.html";
}
}
}
const getTracksArray = function () {
let xhr = new XMLHttpRequest();
xhr.open('GET', `/AppMusic/assets/tracks`);
xhr.responseType = 'text';
xhr.onload = function () {
const pathsForTracks = new RegExp('(?<=href="\)(.+?)(?=")', 'g');
const nameOf = new RegExp('(?<=mp3">)(.+?)(?=.mp3)', 'g');
numberOfTracks = xhr.response.match(nameOf).length; // need to export
tracks = xhr.response.match(nameOf)
generateTracksBox()
pathForTrack = xhr.response.match(pathsForTracks) // need to export
}
xhr.send();
}