1

I have a simple array

var answerAttribute = ['A','B','C','D']; 

I have 16 list items, what I'm trying to accomplish is loop through the length of the list and regardless of if the list 2 items or 300. I'd lke to have a data attribute associated with it of A,B, C or D.

Here's what I'm working with:

var questionOption = ''; for(var i = 0; i < quizContent.length; i++) { questionOption = answerAttribute[i % answerAttribute.length]; console.log(questionOption); } 

When logging this to the console, it logs A, AB, ABC, ABCD, ABCDundefined, and keeps repeating undefined until it's reached the loops conclusion. My question is what am I doing incorrectly so that it only logs one letter per loop.

8
  • 2
    What's your question? Your condition should be <, not <=. Other than that, I don't know what your specific issue is. Commented Feb 13, 2018 at 16:31
  • Prematurely posted, finishing the question. Sorry about that. Commented Feb 13, 2018 at 16:32
  • 12
    Ah, premature askulation. It happens. :D Commented Feb 13, 2018 at 16:33
  • adding to @clockwork comment answerAttribute[i] should be changed to answerAttribute[i%4]otherwise you'll get index Out of bond if quizContent.length >3 Commented Feb 13, 2018 at 16:34
  • I guess you want sth like for(var i = 0; i < quizContent.length; i++) questionOption += answerAttribute[i % answerAttribute.length]; but its hard to tell. Please rephrase your description... Commented Feb 13, 2018 at 16:35

4 Answers 4

2
questionOption += answerAttribute[i] 

This statement is short-form for questionOption = questionOption + answerAttribute[i]. It will append the next element to questionOption in every iteration of the loop.

It looks like what you want is probably questionOption = answerAttribute[i]. This will replace the value in questionOption with the new element instead of appending it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the explanation. With that said, that was exactly what I needed but however after the arrays conclusion(reach 'D'), it started outputting undefined. However, I combined your suggestion with @Jonas W and seemed to have reached a conclusion. I have updated my code to reflect. Thank you!
As others have mentioned, it outputs undefined because your answerAttribute array has only 4 elements, but the value of i could be larger, depending on the length of quizContent.
1

You could simply log only the current value, like this:

var questionOption = ''; for (var i = 0; i < quizContent.length; i++) { //what is questionOption used for? questionOption += answerAttribute[i]; console.log(answerAttribute[i]); } 

or if you want questionOption to refer to the current value

questionOption = answerAttribute[i]; console.log(questionOption ); 

4 Comments

You'll get undefined if quizContent.length>3
@SanchitPatiyal -- no Index out of Bound in Javascript!
@Malvolio I mean't undefined :p
If there is no answer for the question, I guess having a undefined would make sens tbh
1

You're looping the quizContent indexes and applying them to the answerAttribute array. I believe what you want is a nested loop...

var quizContent = Array(10); // assume you have 10 quiz questions... var answerAttribute = ['A','B','C','D']; for (var i = 0; i < quizContent.length; i++) { // generate a string for each quiz option var questionOption = ''; for (var n = 0; n < answerAttribute.length; n++) { questionOption += answerAttribute[n]; } quizContent[i] = questionOption; console.log(questionOption); } console.log(quizContent);

Comments

0

Somehow I doubt that the question is actually about the logging, and is actually about the resulting string.

Either way, I'd do this without loops.

var answerAttribute = ['A','B','C','D']; var quizContent = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]; var questionOption = answerAttribute .join("") .repeat(Math.ceil(quizContent.length / answerAttribute.length)) .slice(0, quizContent.length); console.log(questionOption);

It just joins the answerAttribute into a string of characters, and repeats that string the number of times that the length of answerAttribute can be divided into quizContent.length (rounded up).

Then the final string is trimmed down to the size of the quizContent to remove any extra content from the rounding up.

Note that this approach assumes a single character per attribute. If not a single, but they're all the same length, it can be adjusted to still work.

3 Comments

in the spirit of functional programming.. quizContent = Array(15).fill(1)
also, he said "data-attributes" so i'm assuming he'll have to append the string as a data attribute to an element, which would require a loop
Yeah, that could be. Just wanted to give a different approach.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.