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.
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?
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)
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
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;
}
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