0

I'm trying to build a list of Urls. The structure is like this: http://somedomain.com/game_CATEGORY?page=NUMBER.

I have an array of game categories, ranging from action games category to word games category.

I have an array of numbers, 1 through 20.

I have pieces of the url saved as strings.

I've been trying for a day to combine them in this way:

cats = ["action","adventure","arcade","board","card","casino","casual","educational","family","music","puzzle","racing","role_playing","simulation","sports","strategy","trivia","word"],
nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
urlString1 = "http://example.com/game_",
urlString2 = "?page=",
madeUrl1 = [],
x = 1, // counter for page numbers
madeUrl2 = [];

for (var i = 0; i < cats.length; i++) {
    madeUrl1.push(urlString1+cats[i]+urlString2);
};


for (var i = 0; i < madeUrl1.length; i++) {
    madeUrl2.push(madeUrl1[i]+x);
    x++;
};
console.log(madeUrl2);

This gets me partially there. But its printing out one number per category. I need each category printout to have ALL 20 numbers added, then move on to the next category.

0

2 Answers 2

1

You'd need to nest another for loop inside your second one. Something like:

for (var i = 0; i < madeUrl1.length; i++) {
    for (int j = 0; j < nums.length; j++) {
        madeUrl2.push(madeUrl1[i]+nums[j]);
    }
};

That way you're iterating through the base URLs you prepared in madeUrl1, and then for each of those you're iterating through each number you have in the array.

If the numbers are simply sequential from 1 to 20, you don't even need the nums array:

for (var i = 0; i < madeUrl1.length; i++) {
    for (var x = 1; x <= 20; x++) {
        madeUrl2.push(madeUrl1[i]+x);
    }
};

And the whole thing could be accomplished with a single nested for loop:

for (var i = 0; i < cats.length; i++) {
    for (var x = 1; x <= 20; x++) {
        madeUrl1.push(urlString1+cats[i]+urlString2+x);
    }
};
Sign up to request clarification or add additional context in comments.

1 Comment

Nice. That's what I tried yesterday but I kept thinking I needed to return a value from the first loop in order to let the second loop get its data. Thanks for showing me the proper way. Wish I could've figure that out on my own!
0

You can use the code below:

cats = ["action","adventure","arcade","board","card","casino","casual","educational","family","music","puzzle","racing","role_playing","simulation","sports","strategy","trivia","word"],
nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
urlString1 = "http://example.com/game_",
urlString2 = "?page=",
madeUrl1 = [],
x = 1;

for (var i = 0; i < cats.length; i++) {
    for (var j = 0; j < nums.length; j++) {
        madeUrl1.push(urlString1+cats[i]+urlString2+nums[j]);
        x++;
    };
};

console.log(madeUrl1);

What we did here, is first nesting our loops. E.g., it will first loop through the first array, and when it arrives at it first item, in this case a category, it will run the nested loop 20 times, appending each number to the page. After done, it continues to the second category and so on.

2 Comments

Damn, nesting was the first thing I tried, but I didn't have it set up properly. Thanks.
Glad I could help you. If this is a correct answer for you, please use the check icon on top of this answer to mark it as correct, and/or vote it up. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.