最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Looping through array and back to beginning when reaching end - Stack Overflow

programmeradmin3浏览0评论

I have a simple array

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

I have 16 list items, what I'm trying to acplish 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.

I have a simple array

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

I have 16 list items, what I'm trying to acplish 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.

Share edited Feb 13, 2018 at 16:55 user9274775 asked Feb 13, 2018 at 16:30 thelgloriouspastthelgloriouspast 812 silver badges12 bronze badges 8
  • 2 What's your question? Your condition should be <, not <=. Other than that, I don't know what your specific issue is. – user9274775 Commented Feb 13, 2018 at 16:31
  • Prematurely posted, finishing the question. Sorry about that. – thelgloriouspast Commented Feb 13, 2018 at 16:32
  • 12 Ah, premature askulation. It happens. :D – user9274775 Commented Feb 13, 2018 at 16:33
  • adding to @clockwork ment answerAttribute[i] should be changed to answerAttribute[i%4]otherwise you'll get index Out of bond if quizContent.length >3 – Sanchit Patiyal 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... – Jonas Wilms Commented Feb 13, 2018 at 16:35
 |  Show 3 more ments

4 Answers 4

Reset to default 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.

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 );

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);

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.

发布评论

评论列表(0)

  1. 暂无评论