Skip to content

Instantly share code, notes, and snippets.

@alphanetEX
Created May 2, 2022 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alphanetEX/dfba231e50e6d0442e0b291fe17f9ae3 to your computer and use it in GitHub Desktop.
Save alphanetEX/dfba231e50e6d0442e0b291fe17f9ae3 to your computer and use it in GitHub Desktop.
array probability of type of number
// Necesitamos una función masMenos que reciba un array y devuelva otro con los siguientes tres números:
// En la primera posición, la fracción de números que son positivos
// En la segunda posición, la fracción de números que son cero
// En la última posición, la fracción de números que son negativos
// Por ejemplo, masMenos([1, 2, 0, -1]) debería devolver [0.5, 0.25, 0.25],
// dado que hay 50% de positivos, 25% de ceros, y 25% de negativos.
/*
La función masMenos([0,0,0,0]) debe retornar [0,1,0]
La función masMenos([1,0]) debe retornar [0.5,0.5,0]
*/
function masMenos(ard){
let total = ard.length;
let pro_x = [0,0,0];
for(let i=0; i < total; i++){
if (ard[i] > 0 ){
pro_x[0] +=+1;
}else if (ard[i] < 0 ){
pro_x[1] +=+1;
}else{
pro_x[2] +=+1;
}
}
return [ pro_x[0]/total, pro_x[2]/total, pro_x[1]/total ];
}
console.log(masMenos([1,2,0,-1]));
console.log(masMenos([0,0,0,0]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment