Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
Factorial function returning NAN

I am new to javascript, I am trying to write a function that calculate the factorial of a given number and also replace the fourth element. I expect when the code above is run, it should produce 6GeXmanX but instead i am getting NaNXermXny

function Recursion(num) {
  
  if (num=== 0 || num===1){
    return 1;
  }
  result= Recursion(num-1)*num;
  
  results = result + 'Germany'
  const str = results.split('')
  const nth = 4
  var replaceWith = 'X'
  for (var i = nth-1; i < str.length-1; i+=nth){
    str[i]= replaceWith;
    
    
  }
  //y = (results.join(""))
  return (str.join(""));
  
  }
  


  // code goes here  
   
// keep this function call here 
console.log(Recursion(3));
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

First, you need to split the factorial function into a separate function.

Second, in the for condition, the i should be i < str.length not i < str.length - 1 as it will not iterate over the last letter.

function factorial(num) {
  
  if (num=== 0 || num===1){
    return 1;
  }
  return  factorial(num-1)*num;
  
}
  
  
function func(num) {
  let results = factorial(num) + 'Germany'
  const str = results.split('')
  const nth = 4
  var replaceWith = 'X'
  for (var i = nth-1; i < str.length; i+=nth){
    str[i]= replaceWith;
  }
  //y = (results.join(""))
  return (str.join(""));
  
  }
  


  // code goes here  
   
// keep this function call here 
console.log(func(3));

about 4 years ago · Juan Pablo Isaza Relatório

0

This is your code:

function Recursion(num) {
  //When this condition don't comply, the return of Recursion is str.join("")
  //You can add a console.log to see for yourself
  //So you need to split the functions
  if (num === 0 || num === 1) {
    return 1;
  }
  result = Recursion(num - 1) * num;
  console.log("recursive")
  results = result + 'Germany'
  const str = results.split('')
  const nth = 4
  var replaceWith = 'X'
  //It should be only str.length 
  for (var i = nth - 1; i < str.length - 1; i += nth) {
    str[i] = replaceWith;
  }
  return (str.join(""));
}

console.log(Recursion(5))

So you need to do this modifications:

function Factorial(num){
  return num < 2 ? num : num * Factorial(num-1) 
}

function Replace(num){
  const newString = (Factorial(num)+"Germany").split('')
  const replaceWith = 'X'
  let replaceEvery = 4
  for (var i = replaceEvery - 1; i < newString.length; i += replaceEvery) {
    newString[i] = replaceWith;
  }
  return (newString.join(""));
}

console.log(Replace(6));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda