I'm trying to write a chatbot for entertainment, and one of its primary functions is meme lookup. I first wrote this in Python, but I'm now rewriting it in JavaScript, as it can then run totally client side.
Here is my JavaScript meme() function:
function meme(srch) {
  reqUrl = "http://api.pixplorer.co.uk/image?amount=1&size=tb&word=meme";
  memesrch = "";
  while (srch) {
    memesrch += "+" + srch[0];
    srch.slice(1);
  }
  reqUrl += memesrch;
  $.get(reqUrl, function( result ) {
    memeUrl = result['images'][0]['imageurl'];
  });
  return "<a href='" + memeUrl + "'><img src='" + memeUrl + "' style='height: 130px;' /></a>";
}
Here's the original Python function for it, if it might help:
def meme(srch):
    reqUrl = "http://api.pixplorer.co.uk/image?amount=1&size=tb&word=meme"
    if srch:
        memesrch = ""
        while srch:
            memesrch += srch[0] + "+"
            srch = srch[1:]
        memesrch = memesrch[:-1]
        reqUrl += memesrch
    memeUrl = eval(urllib2.urlopen(reqUrl).read())['images'][0]['imageurl']
    return "<a href='" + memeUrl + "'><img src='" + memeUrl + "' style='height: 130px;' /></a>"
My problem with this is that when I first run meme() in the console after a page load/reload, it says the variable memeUrl isn't defined. Then, from the second time onwards, it works fine. However, if I then type meme(["doge"]) or give any string in an array, or even just a string like meme("hello") to the meme() function, it doesn't return anything, not even an error. After that, anything I type returns nothing, not even something like 1+1, or 3, or $. After a few seconds the webpage crashes. Here are some screenshots: Screenshot of DevTools, Screenshot of Webpage Crash.
I have no idea what's causing these, as the only thing I can think of that could cause this problem is an infinite loop, but there isn't one in my code.
 
     
    