Hola chicos estoy luchando con este ejercicio. Estoy tratando de verificar si la entrada contiene todos los números del 0 al 9. Por ejemplo, si la entrada es "0a1b2345asd6s7e89", debería devolver verdadero, "123789" debería ser falso. Probé el código a continuación, pero tengo la sensación de que me dirijo en la dirección equivocada. ¿Quizás expresiones regulares? Por favor ayuda.
const test = (input) => { const arrayOfNumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] const inputArray = [...input] inputArray.every(el => { arrayOfNumbers.forEach(elNumber => { elNumber === el }) }) }const test = (a, b) => a.every(el => b.includes(el))Entonces puedes usarlo como
const ax = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] var ab = "0a1b2345asd6s7e8" test(ax,ab) // -> false ab = "0a1b2345asd6s7e89" test(ax,ab) // -> trueen esta solución utilicé include() e indexOf()
incluir: para verificar si la prueba (cadena de entrada) contiene un número del 0 al 9
indexoOf: para verificar si los números son del 0 al 9 (orden ascendente)
Solución:
function Test(){ let ContAllNumber = 0; //0 = false, 1 = true var test = "0a1b2345asd6s7e89"; const arrayNum = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; for(let i = 0; i < test.length; i++){ if(test.includes(i)){ arrayNum[i] = test.indexOf(i); } else{ break; } } for(let i = 0; i < 9; i++){ if(arrayNum[i] < arrayNum[i+1]){ ContAllNumber = 1; } else{ ContAllNumber = 0; break; } } console.log(ContAllNumber); //condition : contains number from 0 - 9 //example respecting condition: 0123456789, 0a1b2345asd6s7e89 //example NOT respecting condition: 7193456082 , a0s93as0j4}
Depuración:
(Te recomiendo que lo pruebes para entender mejor el código)
function Test(){ let ContAllNumber = 0; //0 = false, 1 = true var test = "0123456789"; const arrayNum = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; for(let i = 0; i < test.length; i++) { if(test.includes(i)) { arrayNum[i] = test.indexOf(i); console.log("number: " + i); console.log("position: " + arrayNum[i]); } } for(let i = 0; i < 9; i++) { if(arrayNum[i] < arrayNum[i+1]) { ContAllNumber = 1; console.log("position of number " + i + " is " + arrayNum[i] + " and position of the next number " + (i + 1) + " is " + arrayNum[i+1] + "so it's respecting condition (ok)"); console.log("so you can return 1 (true)"); } else { ContAllNumber = 0; console.log("position of number " + i + " is " + arrayNum[i] + " and position of the next number " + (i + 1) + " is " + arrayNum[i+1] + "it is NOT respecting condition (NOT OK)"); console.log("so you can return 0 (false)"); break; } } console.log(ContAllNumber); //condition = contains number from 0 - 9 //example respecting condition: 0123456789, 0a1b2345asd6s7e89 //example NOT respecting condition: 7193456082 , a0s93as0j4 }Prueba every e includes :
const test = input => [...Array(10).keys()].every(digit => input.includes(digit)); console.log("Expected output - true:", test("0a1b2345asd6s7e89")); console.log("Expected output - false:", test("123789")); [...Array(10).keys()] es una matriz de 0 a 9. La devolución de llamada a .every() se asegura de que la entrada contenga cada dígito al menos una vez.