Esta es la solución al desafio "Where do I belong" de FreeCodeCamp:
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
var aArr = arr.sort(function(a, b) {//Se utiliza la funcion para organizar los número en orden ascedente.
return a - b; // ya que si organiza en unicode no será el orden númerico deseado.
});
var position = 0;
var cont = 0;
for(i=0; i<arr.length;i++){
if(arr[i]>=num){
position = i;
return position;
}else if(i+1===arr.length){ //esta condición es en caso que el número a evaluar sea mayor que todos los del arreglo
return arr.length; //sí i+1 es igual a la longitud del arreglo quiere decir que no encontro un numero
} //mayor que el buscado por lo que se acomoda en la última posición.
}
return aArr;
}
getIndexToIns([5, 3, 20, 3], 5);
martes, 5 de julio de 2016
sábado, 2 de julio de 2016
Seek And Destroy
Seek and Destroy:
You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these argument
The solution:
function destroyer(arr) { args = Array.from(arguments); var nArray=[]; for(j=0;j<arr.length;j++){ var x=0; for(i=1;i<args.length;i++){ if (arr[j] !== args[i]){ x= x+1;
if (x===args.length-1){ nArray.push(arr[j]); } } } } return nArray; } destroyer([1, 2, 3, 1, 2, 3], 2, 3);
You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these argument
The solution:
function destroyer(arr) { args = Array.from(arguments); var nArray=[]; for(j=0;j<arr.length;j++){ var x=0; for(i=1;i<args.length;i++){ if (arr[j] !== args[i]){ x= x+1;
if (x===args.length-1){ nArray.push(arr[j]); } } } } return nArray; } destroyer([1, 2, 3, 1, 2, 3], 2, 3);
Falsy Bouncer
This is challenge Falsy Bouncer:
Remove all falsy values from an array.
Falsy values in JavaScript are
false, null, 0, "",undefined, and NaN.
Remember to use Read-Search-Ask if you get stuck. Write your own code.
Here are some helpful links:
And here the solution:
function check(value) {
return Boolean(value);
}
function bouncer(val){
var filtered = val.filter(check);
return filtered;
}
Suscribirse a:
Comentarios (Atom)