function isSpecialNumber(n) { let str = n.toString(); for (let i = 0; i < str.length; i++) { if (str.charAt(i) > 5) { return 'NOT!!' } else { return 'Special!!'; } } } console.log( isSpecialNumber(144525), isSpecialNumber(2), isSpecialNumber(9), isSpecialNumber(23), isSpecialNumber(39) )Cómo tiene que funcionar:
esNúmeroEspecial(2) === '¡¡Especial!!' 2 es un solo dígito en el intervalo [0:5].
esNúmeroEspecial(9) === '¡¡NO!!' 9 es un número de un dígito, pero no está en el intervalo [0:5].
esNúmeroEspecial(23) === '¡¡Especial!!' Todos los dígitos del número 23 están en el intervalo [0:5].
esNúmeroEspecial(39) === '¡¡NO!!' El segundo dígito (9) no está en el intervalo [0:5].
Lo único que debe hacer es poner el retorno ¨Especial¨ fuera del for. Como esto:
function isSpecialNumber(n) { let str = n.toString(); for (let i = 0; i < str.length; i++) { if (str.charAt(i) > 5) { return "NOT!!"; } } return "Special!!" }Porque en su solución, si uno de los números es menor que 5, regresa "Especial" y tal vez el siguiente no lo sea.
function isSpecialNumber(n) { let str = n.toString(); let found = 'special'; for (let i = 0; i < str.length; i++) { if (str.charAt(i) > 5) { found = 'NOT!!'; } } return found; } console.log( isSpecialNumber(144525), isSpecialNumber(2), isSpecialNumber(9), isSpecialNumber(23), isSpecialNumber(39) )Prueba esto, es un poco diferente pero debería hacer lo mismo:
function isSpecialNumber(n) { // n.toString() turns the number into a string // .split('') splits the string into an array // .every loops through the items in an array and // checks that all the elements return true for the function that // passed as the parameter into the every function. // .every doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every const special = n.toString() .split() .every((digit) => parseInt(digit) < 5) // You can also do this, which uses .some // .some doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some /* const special = n.toString() .split() .some((digit) => parseInt(digit) > 5) */ // if all the digits are less than 5, then it's special // and should return "Special!!" // else, return "NOT!!" return special ? "Special!!" : "NOT!!" } console.log( isSpecialNumber(144525), isSpecialNumber(2), isSpecialNumber(9), isSpecialNumber(23), isSpecialNumber(39) )