Please, help with optimization!

Want to complete such task: Link!

Code works, but have problems with optimization. Code:

function findDigit(n){
 let pArr = [],sArr = [];
 for(i=0; i<=n+1; i++){
 pArr+= i+1;
 sArr+=(i+1)*(i+1);
 }
      let tim = Number(pArr[n+1]) + Number(sArr[n+1]);
      if(tim>=10){
      dec=1}else{dec=0}
      tim = Number(pArr[n]) + Number(sArr[n]) + dec;
      let a = tim%10;
      return(a);
}

Help please!

For starters (I have not changed how anything works here):

function findDigit(n) {
  // `pArr` and `sArr` make sense to you, but they don't
  // mean anything to someone reading the code (e.g. me)
  let integers = [];
  let squaredIntegers = [];

  // You didn't declare `i` before using it,
  // `i = 0`, needs to be declared like `let i = 0`
  for(let i = 0; i <= n + 1; i++) {
    // pArr and sArr are arrays, you can't add to them
    // because they aren't numbers, so this can't work:
    integers += i + 1;
    squaredIntegers += (i + 1) * (i + 1);
  }
  
  // `tim` and `dec`, like `pArr` and `sArr`, mean nothing
  // to someone reading the code, try to think of better names

  // The arrays should be collections of numbers by this point.
  // `Number` should be completely unecessary here
  // **If** you're putting the numbers in an array, convert to
  // numbers in the loop instead
  let tim = integers[n+1] + squaredIntegers[n+1];
  // You never declared `dec`, you just tried to use it -
  // `dec = 1`, you need to declare it like `let dec =`
  let dec = tim >= 10 ? 1 : 0;
  
  tim = integers[n] + squaredIntegers[n] + dec;

  return tim % 10;
}

To optimise, you don’t need to put anything in an array (and as this is a possibly infinite sequence, you don’t want to build two possibly huge objects). You can just calculate in the loop.

Did you red the task? :slight_smile:

N is a number if final Sum

Codewars link: https://www.codewars.com/kata/find-nth-digit-in-a-infinite-addition-result/train/javascript

Yes, I did, and your code does not quite make sense. Explain why you have those two arrays declared, and given that, how pArr += i + 1 can possibly work. You are looping over the two lines, but what is inside the loop does not make sense: you need numbers, so why are you not converting to numbers there?

pArr is making string like 1234567 … n
sArr is making Double like 1 4 9 16 … n
Think, that problem is here because it can’t calculate n = 1000000000
image

I need to sum pArr and sArr, and return n-th char of sum

Your integers don’t work…

Code is not correct

**Don’t work without number

No, it won’t: it isn’t an optimisation, it was suggestions on making the code understandable and needs more work. At the minute, you are creating two arrays, except neither of them are actually arrays and the += only works dues to coersion rules: first array is actually a string and the second array is a double (?? except doubles are not a thing that exist in JS, so this doesn’t quite make sense)

Ok. Maybe you can recomend me a solution?
image
How to make it without +=?
I still have “Timed out”

You don’t need to drop it, just name/declare things as what they actually are, not random data structures. If that isnt an array of numbers/strings, don’t say it’s an array, it’s hyper confusing. I’m off for some lunch atm, I’ll have a look when I get back

uurgh and this is a fairly badly written challenge afaics - for example

272619325597593231536305887388...
 ^
 1st digit

7 is not the first digit, 2 is clearly the first digit :rage:

OK. For developer 7 is first.

But task is still not solved. Waiting for you)

I would argue that it is at index 1, not the first digit…
You can’t disregard the language that us humans communicate with.

Have you changed anything in your code? What’s your code now?

Here you can try to code Here

Current code:

function findDigit(n) {
  let integers = [];
  let squaredIntegers = [];
  
  for(let i = 0; i <= n + 1; i++) {
    integers += i + 1;
    squaredIntegers += (i + 1) * (i + 1);
  }
  
  let tim = Number(integers[n+1]) + Number(squaredIntegers[n+1]);
  let dec = tim >= 10 ? 1 : 0;
  tim = Number(integers[n]) + Number(squaredIntegers[n]) + dec;

  return tim % 10;
}

Have ideas with solution of arrays:

let integers = Array(n + 1).fill().map((, idx) => 1 + idx);;
let squaredIntegers = Array(n + 1).fill().map((
, idx) => (1 + idx) * (1 + idx));

Such idea still not working. Time error.

I think that a thing like findDigit(1000000000) would need at least 1000000000 steps, which I think is enough to trigger the infinite loop protections and/or time out the function

Or there is some trick that can totally reduce the time, and I have no idea what that could be, or this challenge is flawed

It’s possible. 30 people complete…