JavaScript How to run variables in if and for?










0















I want to use FOR loop instead, please.



let int1, int1, int3;//... (int4 to int999)
let op1, op;//... (op2 to op999)
if(int1 < 200)
op1 = int1;

else
op1 = '';

if(int2 < 200) {... (same IF statement for int2 to int999)


Instead, I want this.



for(i = 1; i < 5; i++) 
let int = [int1, int2, int3, int4];
let op = [op1, op2, op3, op4];
if(int[i] < 200)
op[i] = int[i];

else
op[i] = '';


alert(op1 + op2 + op3 + op4);


But that doesn't work somebody help.










share|improve this question
























  • Let input be an array let input = [input1, input2, ...];

    – Jonas Wilms
    Nov 14 '18 at 21:22











  • Any time you find yourself using variables with sequential names like that, you almost always should be using an array instead.

    – Barmar
    Nov 14 '18 at 21:24











  • I updated the code but it still doesn't work.

    – Liquidice Slayer
    Nov 14 '18 at 21:55















0















I want to use FOR loop instead, please.



let int1, int1, int3;//... (int4 to int999)
let op1, op;//... (op2 to op999)
if(int1 < 200)
op1 = int1;

else
op1 = '';

if(int2 < 200) {... (same IF statement for int2 to int999)


Instead, I want this.



for(i = 1; i < 5; i++) 
let int = [int1, int2, int3, int4];
let op = [op1, op2, op3, op4];
if(int[i] < 200)
op[i] = int[i];

else
op[i] = '';


alert(op1 + op2 + op3 + op4);


But that doesn't work somebody help.










share|improve this question
























  • Let input be an array let input = [input1, input2, ...];

    – Jonas Wilms
    Nov 14 '18 at 21:22











  • Any time you find yourself using variables with sequential names like that, you almost always should be using an array instead.

    – Barmar
    Nov 14 '18 at 21:24











  • I updated the code but it still doesn't work.

    – Liquidice Slayer
    Nov 14 '18 at 21:55













0












0








0








I want to use FOR loop instead, please.



let int1, int1, int3;//... (int4 to int999)
let op1, op;//... (op2 to op999)
if(int1 < 200)
op1 = int1;

else
op1 = '';

if(int2 < 200) {... (same IF statement for int2 to int999)


Instead, I want this.



for(i = 1; i < 5; i++) 
let int = [int1, int2, int3, int4];
let op = [op1, op2, op3, op4];
if(int[i] < 200)
op[i] = int[i];

else
op[i] = '';


alert(op1 + op2 + op3 + op4);


But that doesn't work somebody help.










share|improve this question
















I want to use FOR loop instead, please.



let int1, int1, int3;//... (int4 to int999)
let op1, op;//... (op2 to op999)
if(int1 < 200)
op1 = int1;

else
op1 = '';

if(int2 < 200) {... (same IF statement for int2 to int999)


Instead, I want this.



for(i = 1; i < 5; i++) 
let int = [int1, int2, int3, int4];
let op = [op1, op2, op3, op4];
if(int[i] < 200)
op[i] = int[i];

else
op[i] = '';


alert(op1 + op2 + op3 + op4);


But that doesn't work somebody help.







javascript for-loop if-statement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 21:59







Liquidice Slayer

















asked Nov 14 '18 at 21:20









Liquidice SlayerLiquidice Slayer

35




35












  • Let input be an array let input = [input1, input2, ...];

    – Jonas Wilms
    Nov 14 '18 at 21:22











  • Any time you find yourself using variables with sequential names like that, you almost always should be using an array instead.

    – Barmar
    Nov 14 '18 at 21:24











  • I updated the code but it still doesn't work.

    – Liquidice Slayer
    Nov 14 '18 at 21:55

















  • Let input be an array let input = [input1, input2, ...];

    – Jonas Wilms
    Nov 14 '18 at 21:22











  • Any time you find yourself using variables with sequential names like that, you almost always should be using an array instead.

    – Barmar
    Nov 14 '18 at 21:24











  • I updated the code but it still doesn't work.

    – Liquidice Slayer
    Nov 14 '18 at 21:55
















Let input be an array let input = [input1, input2, ...];

– Jonas Wilms
Nov 14 '18 at 21:22





Let input be an array let input = [input1, input2, ...];

– Jonas Wilms
Nov 14 '18 at 21:22













Any time you find yourself using variables with sequential names like that, you almost always should be using an array instead.

– Barmar
Nov 14 '18 at 21:24





Any time you find yourself using variables with sequential names like that, you almost always should be using an array instead.

– Barmar
Nov 14 '18 at 21:24













I updated the code but it still doesn't work.

– Liquidice Slayer
Nov 14 '18 at 21:55





I updated the code but it still doesn't work.

– Liquidice Slayer
Nov 14 '18 at 21:55












2 Answers
2






active

oldest

votes


















0














Your code should be as follows with an example:



let int = [1, 2, 3, 4];
let op = [999, 999, 999, 999];

for(i = 0; i < 4; i++)
if(int[i] < 200)
op[i] = int[i];

else
op[i] = '';


alert(op[0] + op[1] + op[2] + op[3]);


Out:



285


Or in your kind of "symbolic form":



let int = [int1, int2, int3, int4];
let op = [op1, op2, op3, op4];

for(i = 0; i < 4; i++)
if(int[i] < 200)
op[i] = int[i];

else
op[i] = 0;


alert(op[0] + op[1] + op[2] + op[3]);





share|improve this answer

























  • close but op1 isn't being added because its undefined somehow

    – Liquidice Slayer
    Nov 15 '18 at 0:11












  • @LiquidiceSlayer to solve on the mystery you have to provide me the actual values of op1 to op4 and int1 to int4 and I answer the rests. Because as you can see with a real example data it works.

    – Geeocode
    Nov 15 '18 at 0:18












  • op1-4 can be whatever you want(input: 3, 4, 2, 5 but output 425)

    – Liquidice Slayer
    Nov 15 '18 at 0:25












  • if int1-5 is 12345, the code outputs 2345 (op5 is in for some reason while op1 is left out)

    – Liquidice Slayer
    Nov 15 '18 at 0:28











  • The problem is solved if int[i] is replaced with int[i-1]. However, that doesn't seem elegant.

    – Liquidice Slayer
    Nov 15 '18 at 0:32


















0














Instead of a set of variables, You should use an array instead.
Then, Your code would look something like this:



let input = [input1, input2 ... input999];
let output = ;
for(let i = 0; i < input.length; i++)
if(input[i])
output[i] = input[i];
else
alert("error")

)





share|improve this answer

























  • I'm only allowed if while and for.

    – Liquidice Slayer
    Nov 14 '18 at 21:58











  • Here, updated with simple for loop

    – Mateusz
    Nov 14 '18 at 22:08











  • it doesn't work, it says int is undefined.

    – Liquidice Slayer
    Nov 14 '18 at 23:37












  • Int doesn't get transfered to op.

    – Liquidice Slayer
    Nov 15 '18 at 0:01











  • Have you filled the input array with actual data ?

    – Mateusz
    Nov 15 '18 at 11:33










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308901%2fjavascript-how-to-run-variables-in-if-and-for%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Your code should be as follows with an example:



let int = [1, 2, 3, 4];
let op = [999, 999, 999, 999];

for(i = 0; i < 4; i++)
if(int[i] < 200)
op[i] = int[i];

else
op[i] = '';


alert(op[0] + op[1] + op[2] + op[3]);


Out:



285


Or in your kind of "symbolic form":



let int = [int1, int2, int3, int4];
let op = [op1, op2, op3, op4];

for(i = 0; i < 4; i++)
if(int[i] < 200)
op[i] = int[i];

else
op[i] = 0;


alert(op[0] + op[1] + op[2] + op[3]);





share|improve this answer

























  • close but op1 isn't being added because its undefined somehow

    – Liquidice Slayer
    Nov 15 '18 at 0:11












  • @LiquidiceSlayer to solve on the mystery you have to provide me the actual values of op1 to op4 and int1 to int4 and I answer the rests. Because as you can see with a real example data it works.

    – Geeocode
    Nov 15 '18 at 0:18












  • op1-4 can be whatever you want(input: 3, 4, 2, 5 but output 425)

    – Liquidice Slayer
    Nov 15 '18 at 0:25












  • if int1-5 is 12345, the code outputs 2345 (op5 is in for some reason while op1 is left out)

    – Liquidice Slayer
    Nov 15 '18 at 0:28











  • The problem is solved if int[i] is replaced with int[i-1]. However, that doesn't seem elegant.

    – Liquidice Slayer
    Nov 15 '18 at 0:32















0














Your code should be as follows with an example:



let int = [1, 2, 3, 4];
let op = [999, 999, 999, 999];

for(i = 0; i < 4; i++)
if(int[i] < 200)
op[i] = int[i];

else
op[i] = '';


alert(op[0] + op[1] + op[2] + op[3]);


Out:



285


Or in your kind of "symbolic form":



let int = [int1, int2, int3, int4];
let op = [op1, op2, op3, op4];

for(i = 0; i < 4; i++)
if(int[i] < 200)
op[i] = int[i];

else
op[i] = 0;


alert(op[0] + op[1] + op[2] + op[3]);





share|improve this answer

























  • close but op1 isn't being added because its undefined somehow

    – Liquidice Slayer
    Nov 15 '18 at 0:11












  • @LiquidiceSlayer to solve on the mystery you have to provide me the actual values of op1 to op4 and int1 to int4 and I answer the rests. Because as you can see with a real example data it works.

    – Geeocode
    Nov 15 '18 at 0:18












  • op1-4 can be whatever you want(input: 3, 4, 2, 5 but output 425)

    – Liquidice Slayer
    Nov 15 '18 at 0:25












  • if int1-5 is 12345, the code outputs 2345 (op5 is in for some reason while op1 is left out)

    – Liquidice Slayer
    Nov 15 '18 at 0:28











  • The problem is solved if int[i] is replaced with int[i-1]. However, that doesn't seem elegant.

    – Liquidice Slayer
    Nov 15 '18 at 0:32













0












0








0







Your code should be as follows with an example:



let int = [1, 2, 3, 4];
let op = [999, 999, 999, 999];

for(i = 0; i < 4; i++)
if(int[i] < 200)
op[i] = int[i];

else
op[i] = '';


alert(op[0] + op[1] + op[2] + op[3]);


Out:



285


Or in your kind of "symbolic form":



let int = [int1, int2, int3, int4];
let op = [op1, op2, op3, op4];

for(i = 0; i < 4; i++)
if(int[i] < 200)
op[i] = int[i];

else
op[i] = 0;


alert(op[0] + op[1] + op[2] + op[3]);





share|improve this answer















Your code should be as follows with an example:



let int = [1, 2, 3, 4];
let op = [999, 999, 999, 999];

for(i = 0; i < 4; i++)
if(int[i] < 200)
op[i] = int[i];

else
op[i] = '';


alert(op[0] + op[1] + op[2] + op[3]);


Out:



285


Or in your kind of "symbolic form":



let int = [int1, int2, int3, int4];
let op = [op1, op2, op3, op4];

for(i = 0; i < 4; i++)
if(int[i] < 200)
op[i] = int[i];

else
op[i] = 0;


alert(op[0] + op[1] + op[2] + op[3]);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 0:40

























answered Nov 14 '18 at 23:31









GeeocodeGeeocode

2,4001921




2,4001921












  • close but op1 isn't being added because its undefined somehow

    – Liquidice Slayer
    Nov 15 '18 at 0:11












  • @LiquidiceSlayer to solve on the mystery you have to provide me the actual values of op1 to op4 and int1 to int4 and I answer the rests. Because as you can see with a real example data it works.

    – Geeocode
    Nov 15 '18 at 0:18












  • op1-4 can be whatever you want(input: 3, 4, 2, 5 but output 425)

    – Liquidice Slayer
    Nov 15 '18 at 0:25












  • if int1-5 is 12345, the code outputs 2345 (op5 is in for some reason while op1 is left out)

    – Liquidice Slayer
    Nov 15 '18 at 0:28











  • The problem is solved if int[i] is replaced with int[i-1]. However, that doesn't seem elegant.

    – Liquidice Slayer
    Nov 15 '18 at 0:32

















  • close but op1 isn't being added because its undefined somehow

    – Liquidice Slayer
    Nov 15 '18 at 0:11












  • @LiquidiceSlayer to solve on the mystery you have to provide me the actual values of op1 to op4 and int1 to int4 and I answer the rests. Because as you can see with a real example data it works.

    – Geeocode
    Nov 15 '18 at 0:18












  • op1-4 can be whatever you want(input: 3, 4, 2, 5 but output 425)

    – Liquidice Slayer
    Nov 15 '18 at 0:25












  • if int1-5 is 12345, the code outputs 2345 (op5 is in for some reason while op1 is left out)

    – Liquidice Slayer
    Nov 15 '18 at 0:28











  • The problem is solved if int[i] is replaced with int[i-1]. However, that doesn't seem elegant.

    – Liquidice Slayer
    Nov 15 '18 at 0:32
















close but op1 isn't being added because its undefined somehow

– Liquidice Slayer
Nov 15 '18 at 0:11






close but op1 isn't being added because its undefined somehow

– Liquidice Slayer
Nov 15 '18 at 0:11














@LiquidiceSlayer to solve on the mystery you have to provide me the actual values of op1 to op4 and int1 to int4 and I answer the rests. Because as you can see with a real example data it works.

– Geeocode
Nov 15 '18 at 0:18






@LiquidiceSlayer to solve on the mystery you have to provide me the actual values of op1 to op4 and int1 to int4 and I answer the rests. Because as you can see with a real example data it works.

– Geeocode
Nov 15 '18 at 0:18














op1-4 can be whatever you want(input: 3, 4, 2, 5 but output 425)

– Liquidice Slayer
Nov 15 '18 at 0:25






op1-4 can be whatever you want(input: 3, 4, 2, 5 but output 425)

– Liquidice Slayer
Nov 15 '18 at 0:25














if int1-5 is 12345, the code outputs 2345 (op5 is in for some reason while op1 is left out)

– Liquidice Slayer
Nov 15 '18 at 0:28





if int1-5 is 12345, the code outputs 2345 (op5 is in for some reason while op1 is left out)

– Liquidice Slayer
Nov 15 '18 at 0:28













The problem is solved if int[i] is replaced with int[i-1]. However, that doesn't seem elegant.

– Liquidice Slayer
Nov 15 '18 at 0:32





The problem is solved if int[i] is replaced with int[i-1]. However, that doesn't seem elegant.

– Liquidice Slayer
Nov 15 '18 at 0:32













0














Instead of a set of variables, You should use an array instead.
Then, Your code would look something like this:



let input = [input1, input2 ... input999];
let output = ;
for(let i = 0; i < input.length; i++)
if(input[i])
output[i] = input[i];
else
alert("error")

)





share|improve this answer

























  • I'm only allowed if while and for.

    – Liquidice Slayer
    Nov 14 '18 at 21:58











  • Here, updated with simple for loop

    – Mateusz
    Nov 14 '18 at 22:08











  • it doesn't work, it says int is undefined.

    – Liquidice Slayer
    Nov 14 '18 at 23:37












  • Int doesn't get transfered to op.

    – Liquidice Slayer
    Nov 15 '18 at 0:01











  • Have you filled the input array with actual data ?

    – Mateusz
    Nov 15 '18 at 11:33















0














Instead of a set of variables, You should use an array instead.
Then, Your code would look something like this:



let input = [input1, input2 ... input999];
let output = ;
for(let i = 0; i < input.length; i++)
if(input[i])
output[i] = input[i];
else
alert("error")

)





share|improve this answer

























  • I'm only allowed if while and for.

    – Liquidice Slayer
    Nov 14 '18 at 21:58











  • Here, updated with simple for loop

    – Mateusz
    Nov 14 '18 at 22:08











  • it doesn't work, it says int is undefined.

    – Liquidice Slayer
    Nov 14 '18 at 23:37












  • Int doesn't get transfered to op.

    – Liquidice Slayer
    Nov 15 '18 at 0:01











  • Have you filled the input array with actual data ?

    – Mateusz
    Nov 15 '18 at 11:33













0












0








0







Instead of a set of variables, You should use an array instead.
Then, Your code would look something like this:



let input = [input1, input2 ... input999];
let output = ;
for(let i = 0; i < input.length; i++)
if(input[i])
output[i] = input[i];
else
alert("error")

)





share|improve this answer















Instead of a set of variables, You should use an array instead.
Then, Your code would look something like this:



let input = [input1, input2 ... input999];
let output = ;
for(let i = 0; i < input.length; i++)
if(input[i])
output[i] = input[i];
else
alert("error")

)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 11:31

























answered Nov 14 '18 at 21:43









MateuszMateusz

263




263












  • I'm only allowed if while and for.

    – Liquidice Slayer
    Nov 14 '18 at 21:58











  • Here, updated with simple for loop

    – Mateusz
    Nov 14 '18 at 22:08











  • it doesn't work, it says int is undefined.

    – Liquidice Slayer
    Nov 14 '18 at 23:37












  • Int doesn't get transfered to op.

    – Liquidice Slayer
    Nov 15 '18 at 0:01











  • Have you filled the input array with actual data ?

    – Mateusz
    Nov 15 '18 at 11:33

















  • I'm only allowed if while and for.

    – Liquidice Slayer
    Nov 14 '18 at 21:58











  • Here, updated with simple for loop

    – Mateusz
    Nov 14 '18 at 22:08











  • it doesn't work, it says int is undefined.

    – Liquidice Slayer
    Nov 14 '18 at 23:37












  • Int doesn't get transfered to op.

    – Liquidice Slayer
    Nov 15 '18 at 0:01











  • Have you filled the input array with actual data ?

    – Mateusz
    Nov 15 '18 at 11:33
















I'm only allowed if while and for.

– Liquidice Slayer
Nov 14 '18 at 21:58





I'm only allowed if while and for.

– Liquidice Slayer
Nov 14 '18 at 21:58













Here, updated with simple for loop

– Mateusz
Nov 14 '18 at 22:08





Here, updated with simple for loop

– Mateusz
Nov 14 '18 at 22:08













it doesn't work, it says int is undefined.

– Liquidice Slayer
Nov 14 '18 at 23:37






it doesn't work, it says int is undefined.

– Liquidice Slayer
Nov 14 '18 at 23:37














Int doesn't get transfered to op.

– Liquidice Slayer
Nov 15 '18 at 0:01





Int doesn't get transfered to op.

– Liquidice Slayer
Nov 15 '18 at 0:01













Have you filled the input array with actual data ?

– Mateusz
Nov 15 '18 at 11:33





Have you filled the input array with actual data ?

– Mateusz
Nov 15 '18 at 11:33

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308901%2fjavascript-how-to-run-variables-in-if-and-for%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3