skip to Main Content

I should create a function that given an array of numbers and a specific value, returns another array filled with those numbers in which the number of digits matches the specified value.

function filtrarPorQuantidadeDeDigitos (array, nDigitos) {
  let solucao = array.filter(elemento => {
    elemento.toString().length === nDigitos
    console.log(elemento.toString().length == nDigitos)
  })
  return solucao
}

console.log(filtrarPorQuantidadeDeDigitos([123, 11, 158, 0], 3))

Thus I created an arrow function associated with the .filter method that converts each number in a string (.toString()) and then, counts its length to check if it’s the same as what we are expecting. Naturally, if the condition was fulfilled, that element would be copied to the resulting array. However, even checking with a console.log that the boolean values for each loop are correct, I still get an empty array as output.

Why isn’t it working? What should I do to solve this problem? Is it possible to do it in any similar way?

2

Answers


  1. When the filter function is simple, i.e. can be given as an expression, not requiring multiple steps, you can write it like this:

    const result = original.filter( item => item.startsWith('The') );
    

    If it is more complex, you can use several statements grouped with { }, but you must then give a return statement at the end.

    const result = original.filter( item => {
       const word = "T"+"h"+"e";
       return item.startsWith(word); 
    });
    

    Your code just needs that fixed:

    function filtrarPorQuantidadeDeDigitos (array, nDigitos) {
      const solucao = array.filter(elemento => 
        elemento.toString().length === nDigitos
    )
      return solucao
    }
    
    console.log(filtrarPorQuantidadeDeDigitos([123, 11, 158, 0], 3))
    Login or Signup to reply.
  2. You need to return the value if the result is true. Like this:

    function filtrarPorQuantidadeDeDigitos (array, nDigitos) {
      let sulution = array.filter(elem => elem.toString().length === nDigitos ? true : false)
      return sulution
    }
    
    console.log(filtrarPorQuantidadeDeDigitos([123, 11, 158, 0], 3))

    In the example above I check if the length is equal to nDigits, if it is the arrow function in the filter returns true and the element is added to the new filtered array, otherwise it returns false and the element is not added.
    Since arrow functions accept the implicit return, the one that can just pass an expression and it returns automatically, so you can just do it like this:

    function filtrarPorQuantidadeDeDigitos (array, nDigitos) {
      let sulution = array.filter(elem => elem.toString().length === nDigitos)
      return sulution
    }
    
    console.log(filtrarPorQuantidadeDeDigitos([123, 11, 158, 0], 3))

    If you want to add a console to display the data, then you can do like this:

    function filtrarPorQuantidadeDeDigitos (array, nDigitos) {
      let sulution = array.filter(elem => {
        const result = elem.toString().length === nDigitos
        console.log(result)
        return result
      })
      return sulution
    }
    
    console.log(filtrarPorQuantidadeDeDigitos([123, 11, 158, 0], 3))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search