[英]how do I use values from an array inside a function in another function?
I created a function in Javascript that convert decimal numbers to binary, and I used an array to store the binary numbers, and made the function returns the array, what I want is, how do I get the values of that array and use it outside the function or in another function? 我用Javascript创建了一个将十进制数转换为二进制数的函数,并使用一个数组存储二进制数,并使该函数返回该数组,我想要的是,如何获取该数组的值并在外部使用它该功能或其他功能?
thats the function I was talking about, and that array I want is called numArray. 多数民众赞成在我正在谈论的功能,而我想要的数组称为numArray。
function decimalToBinary(Num) { let bits = ''; let rem = Num; for(let i = 7; i >= 0; i--) { let divisor = Math.pow(2, i); let bitValue = Math.floor(rem / divisor); bits = bits + bitValue; rem = rem % divisor; } let numArray = []; for(let i = 0; i < bits.length; i++){ let bit = bits.charAt(i); let binaryNums = parseInt(bit); numArray.push(binaryNums); } return numArray; } /* what I want to do is to use a specific value from inside that array and use inside a second function, and then use if-statement to get the result I want */ function second() { //if-statement if(numArray[2] === 1){ //do something }else{ //do something else } }
var binary = [] ;
function decimalToBinary(Num) {
// do some logic push the output in binary
return binary;
}
function second(argument){
//Here you can access the binary
}
PS:- This is anti pattern if you update your question i can tell some more way to do it. PS:-这是反模式,如果您更新您的问题,我可以告诉您更多方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.