How to get the value from a for loop in Javascript2019 Community Moderator ElectionHow do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I redirect to another webpage?How can I get query string values in JavaScript?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?Loop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?
How do you talk to someone whose loved one is dying?
Happy pi day, everyone!
Is there a symmetric-key algorithm which we can use for creating a signature?
How could an airship be repaired midflight?
Explaining pyrokinesis powers
Adventure Game (text based) in C++
Why Choose Less Effective Armour Types?
Employee lack of ownership
PTIJ: Who should I vote for? (21st Knesset Edition)
Could the Saturn V actually have launched astronauts around Venus?
Python if-else code style for reduced code for rounding floats
I got the following comment from a reputed math journal. What does it mean?
Math equation in non italic font
Violin - Can double stops be played when the strings are not next to each other?
Can I use USB data pins as a power source?
What options are left, if Britain cannot decide?
Custom alignment for GeoMarkers
How do I hide Chekhov's Gun?
Do the common programs (for example: "ls", "cat") in Linux and BSD come from the same source code?
A single argument pattern definition applies to multiple-argument patterns?
Why is the President allowed to veto a cancellation of emergency powers?
Is there a place to find the pricing for things not mentioned in the PHB? (non-magical)
Relationship between sampajanna definitions in SN 47.2 and SN 47.35
What is the purpose or proof behind chain rule?
How to get the value from a for loop in Javascript
2019 Community Moderator ElectionHow do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I redirect to another webpage?How can I get query string values in JavaScript?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?Loop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?
I have a for loop made to create divs. The problem is: The divs format should be different for certain numbers, but i don't know how to tell that to my IF statement.
Here is the full code:
window.onload = app;
const addDivs =
content: "main-content",
rootEl: ,
minValue: "min-value",
maxValue: "max-value",
fizzValue: "fizz-value",
buzzValue: "buzz-value",
submitDivs: "add-divs",
clearDivs: "clear-divs",
defaultValues:
min: 1,
max: 100,
fizz: 3,
buzz: 5
,
submitValue: function(event)
addDivs.defaultValues.min = parseInt(
document.getElementById(addDivs.minValue).value
);
addDivs.defaultValues.max = parseInt(
document.getElementById(addDivs.maxValue).value
);
addDivs.defaultValues.fizz = parseInt(
document.getElementById(addDivs.fizzValue).value
);
addDivs.defaultValues.buzz = parseInt(
document.getElementById(addDivs.buzzValue).value
);
for (
let i = addDivs.defaultValues.min;
i <= addDivs.defaultValues.max;
i++
)
const newDiv = document.createElement("div");
if (i % addDivs.defaultValues.fizz === 0 && i % addDivs.defaultValues.buzz
=== 0)
newDiv.className = "fizzbuzz generated-divs";
newDiv.innerText = "Fizz Buzz!";
addDivs.rootEl.appendChild(newDiv);
else
newDiv.className = "other generated-divs";
newDiv.innerText = i;
addDivs.rootEl.appendChild(newDiv);
,
clearValue: function(event)
document.getElementById(addDivs.content).innerHTML = "";
,
initializeState: function()
addDivs.rootEl = document.getElementById(addDivs.content);
document.getElementById(addDivs.submitDivs).onclick = addDivs.submitValue;
document.getElementById(addDivs.clearDivs).onclick = addDivs.clearValue;
;
function app()
addDivs.initializeState();
The if statement isnt working correctly in this for loop. What I want it to do is, print divs, and when a certain number comes, print a different kind of div. In the if statement i'm trying to say something like this: i % 3 === 0 && i % 5 === 0, but it isnt working the way I did it, therefore only the else argument is working and printing all the divs in the same format.
for (
let i = addDivs.defaultValues.min;
i <= addDivs.defaultValues.max;
i++
)
const newDiv = document.createElement("div");
if (
i % addDivs.defaultValues.fizz === 0 &&
i % addDivs.defaultValues.buzz === 0
)
newDiv.className = "fizzbuzz generated-divs";
newDiv.innerText = "Fizz Buzz!";
addDivs.rootEl.appendChild(newDiv);
else
newDiv.className = "other generated-divs";
newDiv.innerText = i;
addDivs.rootEl.appendChild(newDiv);
All the values, min, max, fizz, buzz come from input fields with ID:
minValue: "min-value",
maxValue: "max-value",
fizzValue: "fizz-value",
buzzValue: "buzz-value",
Please let me know if I can explain my self better. Also, if HTML5 is needed, let me know and Ill add it. Thank you
javascript
add a comment |
I have a for loop made to create divs. The problem is: The divs format should be different for certain numbers, but i don't know how to tell that to my IF statement.
Here is the full code:
window.onload = app;
const addDivs =
content: "main-content",
rootEl: ,
minValue: "min-value",
maxValue: "max-value",
fizzValue: "fizz-value",
buzzValue: "buzz-value",
submitDivs: "add-divs",
clearDivs: "clear-divs",
defaultValues:
min: 1,
max: 100,
fizz: 3,
buzz: 5
,
submitValue: function(event)
addDivs.defaultValues.min = parseInt(
document.getElementById(addDivs.minValue).value
);
addDivs.defaultValues.max = parseInt(
document.getElementById(addDivs.maxValue).value
);
addDivs.defaultValues.fizz = parseInt(
document.getElementById(addDivs.fizzValue).value
);
addDivs.defaultValues.buzz = parseInt(
document.getElementById(addDivs.buzzValue).value
);
for (
let i = addDivs.defaultValues.min;
i <= addDivs.defaultValues.max;
i++
)
const newDiv = document.createElement("div");
if (i % addDivs.defaultValues.fizz === 0 && i % addDivs.defaultValues.buzz
=== 0)
newDiv.className = "fizzbuzz generated-divs";
newDiv.innerText = "Fizz Buzz!";
addDivs.rootEl.appendChild(newDiv);
else
newDiv.className = "other generated-divs";
newDiv.innerText = i;
addDivs.rootEl.appendChild(newDiv);
,
clearValue: function(event)
document.getElementById(addDivs.content).innerHTML = "";
,
initializeState: function()
addDivs.rootEl = document.getElementById(addDivs.content);
document.getElementById(addDivs.submitDivs).onclick = addDivs.submitValue;
document.getElementById(addDivs.clearDivs).onclick = addDivs.clearValue;
;
function app()
addDivs.initializeState();
The if statement isnt working correctly in this for loop. What I want it to do is, print divs, and when a certain number comes, print a different kind of div. In the if statement i'm trying to say something like this: i % 3 === 0 && i % 5 === 0, but it isnt working the way I did it, therefore only the else argument is working and printing all the divs in the same format.
for (
let i = addDivs.defaultValues.min;
i <= addDivs.defaultValues.max;
i++
)
const newDiv = document.createElement("div");
if (
i % addDivs.defaultValues.fizz === 0 &&
i % addDivs.defaultValues.buzz === 0
)
newDiv.className = "fizzbuzz generated-divs";
newDiv.innerText = "Fizz Buzz!";
addDivs.rootEl.appendChild(newDiv);
else
newDiv.className = "other generated-divs";
newDiv.innerText = i;
addDivs.rootEl.appendChild(newDiv);
All the values, min, max, fizz, buzz come from input fields with ID:
minValue: "min-value",
maxValue: "max-value",
fizzValue: "fizz-value",
buzzValue: "buzz-value",
Please let me know if I can explain my self better. Also, if HTML5 is needed, let me know and Ill add it. Thank you
javascript
Yes, you can explain yourself better
– Chris Neve
Mar 7 at 15:44
What is the current result (part of the output?) and what result are you expecting instead?
– Bergi
Mar 7 at 15:46
i % addDivs.defaultValues.fizz === 0
what is this supposed to do?
– VLAZ
Mar 7 at 15:47
@Bergi For now I get the amount of divs Im stating in min and max fields, but they all are in the same format. I'd like divs number 3 and 5 to be returned with the format in the if statement with class 'fizzbuzz', the rest with the class 'other'
– Reinis Krūkliņš
Mar 7 at 15:50
@VLAZ My idea there is to basically say that divs with numbers 3 and 5 should have a different class 'fizzbuzz', the rest of the numbers should have a class 'other'. I trying to say: i % 3 === 0 && i % 5 === 0, but i need the 3 and 5 be values coming from the input fields min, max, fizz and buzz
– Reinis Krūkliņš
Mar 7 at 15:53
add a comment |
I have a for loop made to create divs. The problem is: The divs format should be different for certain numbers, but i don't know how to tell that to my IF statement.
Here is the full code:
window.onload = app;
const addDivs =
content: "main-content",
rootEl: ,
minValue: "min-value",
maxValue: "max-value",
fizzValue: "fizz-value",
buzzValue: "buzz-value",
submitDivs: "add-divs",
clearDivs: "clear-divs",
defaultValues:
min: 1,
max: 100,
fizz: 3,
buzz: 5
,
submitValue: function(event)
addDivs.defaultValues.min = parseInt(
document.getElementById(addDivs.minValue).value
);
addDivs.defaultValues.max = parseInt(
document.getElementById(addDivs.maxValue).value
);
addDivs.defaultValues.fizz = parseInt(
document.getElementById(addDivs.fizzValue).value
);
addDivs.defaultValues.buzz = parseInt(
document.getElementById(addDivs.buzzValue).value
);
for (
let i = addDivs.defaultValues.min;
i <= addDivs.defaultValues.max;
i++
)
const newDiv = document.createElement("div");
if (i % addDivs.defaultValues.fizz === 0 && i % addDivs.defaultValues.buzz
=== 0)
newDiv.className = "fizzbuzz generated-divs";
newDiv.innerText = "Fizz Buzz!";
addDivs.rootEl.appendChild(newDiv);
else
newDiv.className = "other generated-divs";
newDiv.innerText = i;
addDivs.rootEl.appendChild(newDiv);
,
clearValue: function(event)
document.getElementById(addDivs.content).innerHTML = "";
,
initializeState: function()
addDivs.rootEl = document.getElementById(addDivs.content);
document.getElementById(addDivs.submitDivs).onclick = addDivs.submitValue;
document.getElementById(addDivs.clearDivs).onclick = addDivs.clearValue;
;
function app()
addDivs.initializeState();
The if statement isnt working correctly in this for loop. What I want it to do is, print divs, and when a certain number comes, print a different kind of div. In the if statement i'm trying to say something like this: i % 3 === 0 && i % 5 === 0, but it isnt working the way I did it, therefore only the else argument is working and printing all the divs in the same format.
for (
let i = addDivs.defaultValues.min;
i <= addDivs.defaultValues.max;
i++
)
const newDiv = document.createElement("div");
if (
i % addDivs.defaultValues.fizz === 0 &&
i % addDivs.defaultValues.buzz === 0
)
newDiv.className = "fizzbuzz generated-divs";
newDiv.innerText = "Fizz Buzz!";
addDivs.rootEl.appendChild(newDiv);
else
newDiv.className = "other generated-divs";
newDiv.innerText = i;
addDivs.rootEl.appendChild(newDiv);
All the values, min, max, fizz, buzz come from input fields with ID:
minValue: "min-value",
maxValue: "max-value",
fizzValue: "fizz-value",
buzzValue: "buzz-value",
Please let me know if I can explain my self better. Also, if HTML5 is needed, let me know and Ill add it. Thank you
javascript
I have a for loop made to create divs. The problem is: The divs format should be different for certain numbers, but i don't know how to tell that to my IF statement.
Here is the full code:
window.onload = app;
const addDivs =
content: "main-content",
rootEl: ,
minValue: "min-value",
maxValue: "max-value",
fizzValue: "fizz-value",
buzzValue: "buzz-value",
submitDivs: "add-divs",
clearDivs: "clear-divs",
defaultValues:
min: 1,
max: 100,
fizz: 3,
buzz: 5
,
submitValue: function(event)
addDivs.defaultValues.min = parseInt(
document.getElementById(addDivs.minValue).value
);
addDivs.defaultValues.max = parseInt(
document.getElementById(addDivs.maxValue).value
);
addDivs.defaultValues.fizz = parseInt(
document.getElementById(addDivs.fizzValue).value
);
addDivs.defaultValues.buzz = parseInt(
document.getElementById(addDivs.buzzValue).value
);
for (
let i = addDivs.defaultValues.min;
i <= addDivs.defaultValues.max;
i++
)
const newDiv = document.createElement("div");
if (i % addDivs.defaultValues.fizz === 0 && i % addDivs.defaultValues.buzz
=== 0)
newDiv.className = "fizzbuzz generated-divs";
newDiv.innerText = "Fizz Buzz!";
addDivs.rootEl.appendChild(newDiv);
else
newDiv.className = "other generated-divs";
newDiv.innerText = i;
addDivs.rootEl.appendChild(newDiv);
,
clearValue: function(event)
document.getElementById(addDivs.content).innerHTML = "";
,
initializeState: function()
addDivs.rootEl = document.getElementById(addDivs.content);
document.getElementById(addDivs.submitDivs).onclick = addDivs.submitValue;
document.getElementById(addDivs.clearDivs).onclick = addDivs.clearValue;
;
function app()
addDivs.initializeState();
The if statement isnt working correctly in this for loop. What I want it to do is, print divs, and when a certain number comes, print a different kind of div. In the if statement i'm trying to say something like this: i % 3 === 0 && i % 5 === 0, but it isnt working the way I did it, therefore only the else argument is working and printing all the divs in the same format.
for (
let i = addDivs.defaultValues.min;
i <= addDivs.defaultValues.max;
i++
)
const newDiv = document.createElement("div");
if (
i % addDivs.defaultValues.fizz === 0 &&
i % addDivs.defaultValues.buzz === 0
)
newDiv.className = "fizzbuzz generated-divs";
newDiv.innerText = "Fizz Buzz!";
addDivs.rootEl.appendChild(newDiv);
else
newDiv.className = "other generated-divs";
newDiv.innerText = i;
addDivs.rootEl.appendChild(newDiv);
All the values, min, max, fizz, buzz come from input fields with ID:
minValue: "min-value",
maxValue: "max-value",
fizzValue: "fizz-value",
buzzValue: "buzz-value",
Please let me know if I can explain my self better. Also, if HTML5 is needed, let me know and Ill add it. Thank you
javascript
javascript
edited Mar 7 at 16:05
Reinis Krūkliņš
asked Mar 7 at 15:41
Reinis KrūkliņšReinis Krūkliņš
11
11
Yes, you can explain yourself better
– Chris Neve
Mar 7 at 15:44
What is the current result (part of the output?) and what result are you expecting instead?
– Bergi
Mar 7 at 15:46
i % addDivs.defaultValues.fizz === 0
what is this supposed to do?
– VLAZ
Mar 7 at 15:47
@Bergi For now I get the amount of divs Im stating in min and max fields, but they all are in the same format. I'd like divs number 3 and 5 to be returned with the format in the if statement with class 'fizzbuzz', the rest with the class 'other'
– Reinis Krūkliņš
Mar 7 at 15:50
@VLAZ My idea there is to basically say that divs with numbers 3 and 5 should have a different class 'fizzbuzz', the rest of the numbers should have a class 'other'. I trying to say: i % 3 === 0 && i % 5 === 0, but i need the 3 and 5 be values coming from the input fields min, max, fizz and buzz
– Reinis Krūkliņš
Mar 7 at 15:53
add a comment |
Yes, you can explain yourself better
– Chris Neve
Mar 7 at 15:44
What is the current result (part of the output?) and what result are you expecting instead?
– Bergi
Mar 7 at 15:46
i % addDivs.defaultValues.fizz === 0
what is this supposed to do?
– VLAZ
Mar 7 at 15:47
@Bergi For now I get the amount of divs Im stating in min and max fields, but they all are in the same format. I'd like divs number 3 and 5 to be returned with the format in the if statement with class 'fizzbuzz', the rest with the class 'other'
– Reinis Krūkliņš
Mar 7 at 15:50
@VLAZ My idea there is to basically say that divs with numbers 3 and 5 should have a different class 'fizzbuzz', the rest of the numbers should have a class 'other'. I trying to say: i % 3 === 0 && i % 5 === 0, but i need the 3 and 5 be values coming from the input fields min, max, fizz and buzz
– Reinis Krūkliņš
Mar 7 at 15:53
Yes, you can explain yourself better
– Chris Neve
Mar 7 at 15:44
Yes, you can explain yourself better
– Chris Neve
Mar 7 at 15:44
What is the current result (part of the output?) and what result are you expecting instead?
– Bergi
Mar 7 at 15:46
What is the current result (part of the output?) and what result are you expecting instead?
– Bergi
Mar 7 at 15:46
i % addDivs.defaultValues.fizz === 0
what is this supposed to do?– VLAZ
Mar 7 at 15:47
i % addDivs.defaultValues.fizz === 0
what is this supposed to do?– VLAZ
Mar 7 at 15:47
@Bergi For now I get the amount of divs Im stating in min and max fields, but they all are in the same format. I'd like divs number 3 and 5 to be returned with the format in the if statement with class 'fizzbuzz', the rest with the class 'other'
– Reinis Krūkliņš
Mar 7 at 15:50
@Bergi For now I get the amount of divs Im stating in min and max fields, but they all are in the same format. I'd like divs number 3 and 5 to be returned with the format in the if statement with class 'fizzbuzz', the rest with the class 'other'
– Reinis Krūkliņš
Mar 7 at 15:50
@VLAZ My idea there is to basically say that divs with numbers 3 and 5 should have a different class 'fizzbuzz', the rest of the numbers should have a class 'other'. I trying to say: i % 3 === 0 && i % 5 === 0, but i need the 3 and 5 be values coming from the input fields min, max, fizz and buzz
– Reinis Krūkliņš
Mar 7 at 15:53
@VLAZ My idea there is to basically say that divs with numbers 3 and 5 should have a different class 'fizzbuzz', the rest of the numbers should have a class 'other'. I trying to say: i % 3 === 0 && i % 5 === 0, but i need the 3 and 5 be values coming from the input fields min, max, fizz and buzz
– Reinis Krūkliņš
Mar 7 at 15:53
add a comment |
0
active
oldest
votes
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047653%2fhow-to-get-the-value-from-a-for-loop-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047653%2fhow-to-get-the-value-from-a-for-loop-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Yes, you can explain yourself better
– Chris Neve
Mar 7 at 15:44
What is the current result (part of the output?) and what result are you expecting instead?
– Bergi
Mar 7 at 15:46
i % addDivs.defaultValues.fizz === 0
what is this supposed to do?– VLAZ
Mar 7 at 15:47
@Bergi For now I get the amount of divs Im stating in min and max fields, but they all are in the same format. I'd like divs number 3 and 5 to be returned with the format in the if statement with class 'fizzbuzz', the rest with the class 'other'
– Reinis Krūkliņš
Mar 7 at 15:50
@VLAZ My idea there is to basically say that divs with numbers 3 and 5 should have a different class 'fizzbuzz', the rest of the numbers should have a class 'other'. I trying to say: i % 3 === 0 && i % 5 === 0, but i need the 3 and 5 be values coming from the input fields min, max, fizz and buzz
– Reinis Krūkliņš
Mar 7 at 15:53