Script
Script3y ago

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
16 Replies
ScriptyChris
ScriptyChris3y 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
Script
ScriptOP3y 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++) {
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
ScriptyChris
ScriptyChris3y 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)
Script
ScriptOP3y ago
noted I meant declaring variable in for loops yhhh thanks catjam
ScriptyChris
ScriptyChris3y 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
Script
ScriptOP3y ago
roger that
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Script
ScriptOP3y ago
my linter didnt<:emoji_21:773183935264849930> do u mean without using strict mode?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Script
ScriptOP3y 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
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Script
ScriptOP3y ago
oh I have that <:emoji_21:773183935264849930> nah just vanilla js
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Script
ScriptOP3y ago
<:roger_okay:932725392098349157>
reactibot
reactibot3y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/933354003000066098