Welcome Guest [Log In] [Register]
Important Links
Contests & Events
Latest Announcements
Forum Guidelines
Meet the Staff
Update Log
  • SOTM #3 [Winner]
  • TOTM #3 [Winner]
  • October Updates!
  • Become a Contributor!
  • Affiliate with us!
    Add Reply
    [C] Help with each() function
    Topic Started: Sep 22 2015, 12:47 AM (204 Views)
    Flash
    Member Avatar


    So I'm creating my own each() function which lets you iterate over stuff in JavaScript. I've done strings, but arrays are giving me a bit of trouble. Here's my code...

    Code:
     
    var each, characters, i, a;

    each = function (data, callback) {
    switch (typeof data) {
    case 'string':
    words = data.split(' ');
    for (i = 0; i < words.length; i++) {
    callback(words[i]);
    }
    break;
    case 'object':
    if (data.isArray) {
    for (i = 0; i < data.length; i++) {
    callback(data[i]);
    }
    }
    else {
    // do stuff
    }
    break;
    default:
    return false;
    }
    };

    a = [1, 2, 3];

    each('This is a string. Every word in this string will be displayed seperately using the alert() function.', function(blah) {
    alert(blah);
    });

    each(a, function(obj) {
    alert(obj);
    });



    Any help in figuring out why my code for iterating over arrays won't work?

    Edit: Oh, and now my code doesn't work with alert() but it does with console.log(), document.write(), etc. :/

    Edit #2: Fixed. isArray is a method, so it should be isArray(). However, alert() still doesn't work...

    Edit #3: Meh, doesn't really matter, since I only use alert() for quick debugging, anyway, so, go ahead and mark this as solved. :)

    Edited by Flash, Sep 22 2015, 05:18 AM.
    Posted Image
    Off
    Profile
    Quote
    Top
     
    -The-
    Member Avatar
    #the { display: all; }

    Based on Edit #3 I'll close this, but users can still comment if they wish within this topic.
    -The- | Foundation Manager
    Skype: JamesAdams1001 | PM me | Ask Me Anything
    Suggest Anything. Ask Anything. Foundation.
    Posted Image Posted Image Posted Image Posted Image
    Off
    Profile
    Quote
    Top
     
    -The-
    Member Avatar
    #the { display: all; }


    Support Issue Resolved!

    This support request has been resolved and has been marked as complete. This is only to help sort the forum, the topic will remain open in case you experience more issues with this topic.

    If you have another support request with another issue, please post a new topic.


    -The- l Foundation Staff
    -The- | Foundation Manager
    Skype: JamesAdams1001 | PM me | Ask Me Anything
    Suggest Anything. Ask Anything. Foundation.
    Posted Image Posted Image Posted Image Posted Image
    Off
    Profile
    Quote
    Top
     
    1 user reading this topic (1 Guest and 0 Anonymous)
    « Previous Topic · Support Zone · Next Topic »
    Add Reply


    Foundation Affiliates (Click Here)

    Discuss Academy Foundation