ama trying to speed up solution for this: https://www.codewars.com/kata/59f6e1af3640ce12510000ad/train/javascript

last time I’ve searched digits with building giant strings and failed tests for n = `100000000`

Now I figured how to avoid building giant strings(see below).

and now I am failing last test for `n === 1000000000`

So I need to speed up more.

Where do I go from here?

I see possible problems:

I am still doing some stuff with every next number - parsing to string. Don’t know how to avoid that.

I am using two functions rn - the one below, and very similar func to get digits from square sequence. I can probably bake one function from those two, but not sure if it’ll help with perfomancy stuff.

To figure out function behaviour, use tests at the very bottom.

If anyone will want to look at the whole current version of my solution or more context is needed, I can provide. Not doing it right away, since the post is becoming long already.

```
const getDigitsFromNatSequence2 = (n) => {
let sequenceLength = 0;
let currentNumber = 1;
let lengthToAdd;
let currentNumberAsString;
while (sequenceLength <= n + 1) {
currentNumberAsString = currentNumber.toString();
lengthToAdd = currentNumberAsString.length;
if (sequenceLength + lengthToAdd >= n + 1) {
//this block should fire WHEN required digit
// somewhere in the current Number
// console.log(currentNumber);
let digit = currentNumberAsString[0]
let digitPosInSequence = sequenceLength;
let digitPosInNumber = 0;
while (digitPosInSequence !== n) {
digitPosInNumber++
digitPosInSequence++
digit = currentNumberAsString[digitPosInNumber];
}
// after the above loop digit with position n should be
// assigned to digit
// console.log(digit)
// for getting digit on n + 1 position >>>
// different steps depending on
// do we need to get next number or not
nextDigit = digitPosInNumber !== lengthToAdd - 1
? currentNumberAsString[digitPosInNumber + 1]
: (currentNumber + 1).toString()[0];
return [Number(digit), Number(nextDigit)];
}
currentNumber++;
sequenceLength += lengthToAdd;
}
return 'some stuff definitely went wrong if we not return early'
}
```

here goes the test for this utility

```
let natSequence = "";
for (let i = 1; i < 100; i++) {
natSequence += i;
};
// console.log(natSequence)
// for n === x >>> results should be [natSequence[x], natSequence[x + 1]]
for (let n = 0; n < 20; n++) {
console.log(`CASE n === ${n}`);
console.log(`EXPECTED:`);
console.log([Number(natSequence[n]), Number(natSequence[n + 1])])
console.log(`RESULT:`);
console.log(getDigitsFromNatSequence2(n));
console.log(`***************`);
};
```