1

I have this function that generates a random string!

function randString() {
  var char = "0123456789abcdefghijklmnopqrstuvwxyz",
      fullchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
      genHash  = "",
      i;

  for (i = 0; i < 8; i++) {
    var rnum = Math.floor(Math.random() * char.length)
    return genHash += char.substring(rnum, rnum + 1)
  }
}

What I'm trying to do is when I click a button I want my genHash to be various class names.

$("[data-action=newlist]").on("click", function() {
  randString()

  var newBtnList = $("<em>", {
    text: genHash,
    title: genHash,
    class: "new-"+ genHash
  }).appendTo("[data-action=list-tree]").on("click", function() {
    // Show active editor
    $("." + genHash).removeClass("hide")
  })
})

I know I can do it inside the randString() function (such as set it as an input's value and grab the value back from the input), but what I'm trying to figure out is how can I to it outside of the function, thus return the string from the function as states in my title.

function randString() {
  var char = "0123456789abcdefghijklmnopqrstuvwxyz",
      fullchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
      genHash  = "",
      i;

  for (i = 0; i < 8; i++) {
    var rnum = Math.floor(Math.random() * char.length)
    return genHash += char.substring(rnum, rnum + 1)
  }
}

// Create a new list
$("[data-action=newlist]").on("click", function() {
  randString()

  var newBtnList = $("<em>", {
    text: genHash,
    title: genHash,
    class: "new-"+ genHash
  }).appendTo("[data-action=list-tree]").on("click", function() {
    // Show active editor
    $("." + genHash).removeClass("hide")
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" data-action="newlist">
  New
</button>
<div data-action="list-tree"></div>

3 Answers 3

2

Your randString() function only returns the first random character due to the return statement in the for loop. Move it after the for:

function randString() {
    var char = "0123456789abcdefghijklmnopqrstuvwxyz",
        fullchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
        genHash = "",
        i;

    for (i = 0; i < 8; i++) {
        var rnum = Math.floor(Math.random() * char.length)
        genHash += char.substring(rnum, rnum + 1)
    }
    return genHash;
}

You can then assign the returned string to a variable in your click event handler:

var genHash = randString();

Working example

Also note that you can use the this keyword in your second click handler to reference the element instead of building a selector from the genHash string. Try this:

$("[data-action=newlist]").on("click", function() {
    var genHash = randString();

    var newBtnList = $("<em>", {
        text: genHash,
        title: genHash,
        class: "new-" + genHash + ' hide'
    }).appendTo("[data-action=list-tree]").on("click", function() {
        $(this).removeClass("hide")
    })
});

Working example

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

1 Comment

And the OP is not assigning the return of randString to do anything, var genHash=randString()
1

Try to change your function's main cycle to

  for (i = 0; i < 8; i++) {
    var rnum = Math.floor(Math.random() * char.length)
    genHash += char.substring(rnum, rnum + 1)
  }
  return genHash;

Then change string

 randString()

in your code to

 var genHash=randString()

And, yes, in class definition remove "new-". Or replace "."+genHash with ".new-"+genHash

Comments

1

You are trying to return the genHash variable value within for loop which is incorrect. just return the genHash variable value after the loop.

try this one

 function randString() {
      var char = "0123456789abcdefghijklmnopqrstuvwxyz",
          fullchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
          genHash  = "",
          i;

      for (i = 0; i < 8; i++) {
        var rnum = Math.floor(Math.random() * char.length)
        genHash  += char.substring(rnum, rnum + 1)
      }
    return genHash ; //change here
    }

Comments

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.