Tomando cada número de cuatro dígitos de una matriz por turno, devuelva el número en el que se encuentra cuando se han descubierto todos los dígitos del 0 al 9. Si no se pueden encontrar todos los dígitos, devuelva "¡Dígitos faltantes!"
Intenté recorrer y luego establecer un condicional if (i != i+1) empujar a una nueva matriz, esto solo me dio la matriz, es evidente que mi lógica es incorrecta. Podría alguien ayudarme
Por ejemplo, llamando a esta función con
arr = findAllDigits([5175, 4538, 2926, 5057, 6401, 4376, 2280, 6137, 8798, 9083])el código debería devolver 5057.
mientras llama
arr = findAllDigits([4883, 3876, 7769, 9846, 9546, 9634, 9696, 2832, 6822, 6868])debe devolver "números que faltan"
function findAllDigits(arr) { newArr = []; for (let i = 0; i < arr.length; i++) { if (arr[i] != arr[i + 1]) newArr.push(arr[i]); console.log(newArr); } }¿Necesito dividir porque está tomando todo antes de la coma como un número y luego iterar?
Puedes usar Establecer aquí
Recorra la array y luego cree un set . Debe devolver el número actual si el tamaño del set se convierte en 10 porque necesita verificar 0-9
function findAllDigits(arr) { const set = new Set(); for (let n of arr) { String(n) .split("") .forEach((c) => set.add(c)); if (set.size === 10) return n; } return "Missing digits!"; } const arr1 = [5175, 4538, 2926, 5057, 6401, 4376, 2280, 6137, 8798, 9083]; const arr2 = [4883, 3876, 7769, 9846, 9546, 9634, 9696, 2832, 6822, 6868]; console.log(findAllDigits(arr1)); console.log(findAllDigits(arr2));Su ciclo for solo está verificando si la entrada de la matriz es igual a la siguiente. Debe dividir los dígitos dentro de cada entrada y almacenarlos individualmente:
function findAllDigits(arr) { newArr = []; for (let i = 0; i < arr.length; i++) { // now iterate the individual digits const entryAsString = arr[i].toString(); for (let j = 0; j < entryAsString.length; j++) { // if we haven't seen the digit before, add it to the array if(!newArr.includes(j) { newArr.push(j); } } // we know we have all digits when newArr is 10 entries long if (newArr.length) { console.log(arr[i]); // you can also return this value here } } }Una solución más:
const arr1 = [5175, 4538, 2926, 5057, 6401, 4376, 2280, 6137, 8798, 9083]; const arr2 = [4883, 3876, 7769, 9846, 9546, 9634, 9696, 2832, 6822, 6868]; const findAllDigits = (arr) => { // Declare new Set: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set const digits = new Set(); // return the first item from array that fits the condition, // find() method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find return arr.find((curr) => ( // String(5175) -> '5175' : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String // [...'5175'] -> ['5','1','7','5'] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax // .forEach(digits.add, digits) - forEach with callback function and context : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach // comma operator lets get rid of return : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator [...String(curr)].forEach(digits.add, digits), // condition - is find() method need to return an item (digits.size === 10) // if returned value is not undefined or null return finded number oterwise error string // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator )) ?? "Missing digits!"; }; console.log(findAllDigits(arr1)); //5057 console.log(findAllDigits(arr2)); //Missing digits!