Tanto la primera como la segunda función (n1 y n2) generan un número aleatorio de sus respectivas matrices en la consola. Surgen problemas cuando trato de sumar estos dos valores generados y el resultado también se muestra en la consola. Intenté crear una variable global para agregar los resultados pero no funcionó.
Si n1() genera 3 y n2() genera 1, me gustaría que la consola genere 4.
Aquí está mi código:
var add = (n1() + n2()); function n1() { var cpuSelection = [1, 2, 3]; var randomSelection = cpuSelection[Math.floor(Math.random() * cpuSelection.length)]; console.log(randomSelection) } n1(); function n2() { var cpuSelection = [1, 2, 3]; var randomSelection = cpuSelection[Math.floor(Math.random() * cpuSelection.length)]; console.log(randomSelection) } n2(); console.log(add)console.log( (n1() + n2())); function n1() { var cpuSelection = [1, 2, 3]; var randomSelection = cpuSelection[Math.floor(Math.random() * cpuSelection.length)]; console.log(randomSelection) return randomSelection; } function n2() { var cpuSelection = [1, 2, 3]; var randomSelection = cpuSelection[Math.floor(Math.random() * cpuSelection.length)]; console.log(randomSelection) return randomSelection; }Como dice DM, debe devolver esos valores aleatorios.
function n1() { var cpuSelection = [1, 2, 3]; var randomSelection = cpuSelection[Math.floor(Math.random() * cpuSelection.length)]; console.log(randomSelection) return randomSelection; } n1(); function n2() { var cpuSelection = [1, 2, 3]; var randomSelection = cpuSelection[Math.floor(Math.random() * cpuSelection.length)]; console.log(randomSelection) return randomSelection; }