Script – 13-35 Jan 19

I have this code which is supposed to return lonely integers(the one that occurs once in the array)
function lonelyinteger(a) {
let allA = [];
let allB;

for (i = 0; i < a.length; i++) {
allA.push(a.filter((string) => string === a[i]));

if (allA[i].length === 1) {
allB = allA[i].toString();
}
}

return allB;
}
function lonelyinteger(a) {
let allA = [];
let allB;

for (i = 0; i < a.length; i++) {
allA.push(a.filter((string) => string === a[i]));

if (allA[i].length === 1) {
allB = allA[i].toString();
}
}

return allB;
}
It works perfectly but hacker rank keeps rejecting it and I'm not really sure why This is the error I get
S
ScriptyChris820d ago
for (i = 0; i < a.length; i++) {
for (i = 0; i < a.length; i++) {
you have undeclared i variable in strict mode that's error https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#converting_mistakes_into_errors
S
Script820d ago
ohhh I defined i now and it works is it recommended to define i?
for (let i=0; i < a.length; i++) {
for (let i=0; i < a.length; i++) {
UU
Unknown User820d ago
S
ScriptyChris820d ago
you ask about variable name or the fact of declaring a variable? For name, it's up to you - the clue is to name variable according to it's usage context so it will be easy for any dev to know what's used for. For not declaring variables - it's a risky thing and works only in non-strict mode (which is less and less present these days, because it's automatically turned on in ESM modules and Classes)
S
Script820d ago
noted I meant declaring variable in for loops yhhh thanks catjam
S
ScriptyChris820d ago
you don't have do declare variable in the loop, you can declare it outside the loop - it depends on use case. But still, it's a good practice (and must have in strict mode) to declare all variables
S
Script820d ago
roger that
UU
Unknown User820d ago
S
Script820d ago
my linter didnt<:emoji_21:773183935264849930> do u mean without using strict mode?
UU
Unknown User820d ago
S
Script820d ago
sheesh I just discovered I do not have any js linter just the one built-in vs code which one do u recommend I see
UU
Unknown User820d ago
S
Script820d ago
oh I have that <:emoji_21:773183935264849930> nah just vanilla js
UU
Unknown User820d ago
S
Script820d ago
<:roger_okay:932725392098349157>
UU
Unknown User819d ago