2

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));
Andreas
  • 21,535
  • 7
  • 47
  • 56
thereal90
  • 105
  • 1
  • 8
  • 3
    This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Aug 22 '22 at 11:20
  • 5
    A recursive function that returns different data types (`return 1`, `return str.join("")`) is not a good idea. – Andreas Aug 22 '22 at 11:21
  • What do you suggest i do? – thereal90 Aug 22 '22 at 11:24
  • @thereal90: At a high level I would suggest splitting these two operations into their own functions and performing them separately. For identifying more specific problem, I suggest stepping through the code in your browser's debugger and identifying the specific operation which isn't doing what you expect. – David Aug 22 '22 at 11:26
  • If you're planning to learn JS for real, have a look at Javascript generators, this would be a perfect use case for them. – user1514042 Aug 22 '22 at 12:35

2 Answers2

3

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));
Mina
  • 14,386
  • 3
  • 13
  • 26
2

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));
scloudes
  • 46
  • 3