0

By pressing the first button I should have some random numbers,

817
754
692

By pressing the second button '123:123', the numbers should appear like this:

817:817
754:754

But what I get is:

817
754
:817
754

How can i combine them please?

function ra(length) {
  var consonants = "123456789",
    vowels = '123456789',
    rand = function(limit) {
      return Math.floor(Math.random() * limit);
    },
    i, word = '',
    length = parseInt(length, 10),
    consonants = consonants.split(''),
    vowels = vowels.split('');
  for (i = 0; i < length / 2; i++) {
    var randConsonant = consonants[rand(consonants.length)],
      randVowel = vowels[rand(vowels.length)];
    word += (i === 0) ? randConsonant.toLowerCase() : randConsonant;
    word += i * 2 < length - 1 ? randVowel : '';
  }
  return word;
}
$("#click").click(function() {
  $("#test").text('');
  for (var p = 0; p < 5; p++) {
    var pass1 = ra;
    $("#test").append(pass1(3) + '\n');
  }
});

$("#combine").click(function() {
  var userpass = document.getElementById('test').value;
  $("#test").append("" + userpass + ":" + userpass + "");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="click">Click</button>
<textarea id="test" rows="5"></textarea>
<button id="combine">123:123</button>

http://jsfiddle.net/t0deq5zu/1/

Pete
  • 57,112
  • 28
  • 117
  • 166
sofyumigni
  • 13
  • 1

3 Answers3

1

You would need to split and loop through each line - currently you get all values as one variable. See edit below in the combine click event (comments in code)

function ra(length) {
  var consonants = "123456789",
    vowels = '123456789',
    rand = function(limit) {
      return Math.floor(Math.random() * limit);
    },
    i, word = '',
    length = parseInt(length, 10),
    consonants = consonants.split(''),
    vowels = vowels.split('');
  for (i = 0; i < length / 2; i++) {
    var randConsonant = consonants[rand(consonants.length)],
      randVowel = vowels[rand(vowels.length)];
    word += (i === 0) ? randConsonant.toLowerCase() : randConsonant;
    word += i * 2 < length - 1 ? randVowel : '';
  }
  return word;
}
$("#click").click(function() {
  $("#test").text('');
  for (var p = 0; p < 5; p++) {
    var pass1 = ra;
    $("#test").append(pass1(3) + '\n');
  }
});

$("#combine").click(function() {
  var userpass = document.getElementById('test').value;
  var lines = userpass.split('\n');  // split the values into each line
  
  if (lines.length) {
    $("#test").empty();             // empty the textarea
    for (var i = 0; i < lines.length; i++) {
        if (lines[i].trim() != '') {   // don't append empty line
          if (lines[0].indexOf(':') > -1) {  // button already pressed so re-append same
            $("#test").append(lines[i] + "\n")
          } else {
            $("#test").append(lines[i] + ":" + lines[i] + "\n");   // apend the new values
          }
        }
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="click">Click</button>
<textarea id="test" rows="5"></textarea>
<button id="combine">123:123</button>
Pete
  • 57,112
  • 28
  • 117
  • 166
0

You're working on the $("#test") which has all the numbers together as its content, so all the numbers together are decoupled. you need to decouple every row separately (using split('\n')).

working JSFiddle

Programmer
  • 1,973
  • 1
  • 12
  • 20
0

I know that you have your answer already but I wanted to elaborate a bit with some comments on your code which I hope will help you write a bit cleaner and better code (+ some new stuff from ECMAScript6). There two major things I would recommend - always indent your code and do add some comments in it.

So here is the code with some comments:

// always use better names of your functions (names that would be like a hint what is the purpose of that function)
function ra (length) {
    // all the variables that won't change during your function should be declared as constansts
  const consonants = "123456789".split('');
  const vowels = '123456789'.split('');
  const consonantLength = consonants.length;
  const vowelsLength = vowels.length;

  let rand = function(limit){
    return Math.floor(Math.random()*limit);
  }
  // no need to declar i here when you can do so in the for loop
  //-- var i = '';

  // not sure why would you use this radix (the second argument of parseInt)
  length = parseInt(length,10);

  // there is no need to perform the split() on a separate row as this can be done with the initial declaration of your 
  //-- consonants = consonants.split('');
  //-- vowels = vowels.split('');

    let word = '';
  let loopLength =  length/2; // check this for more explanations https://stackoverflow.com/questions/8452317/do-loops-check-the-array-length-every-time-when-comparing-i-against-array-length
  for (let i = 0; i < loopLength; i++) {

    // in your code you where using consonants.length which will be executed each time so instead of this I will use the constant consonantLength that I have declared above
        let randConsonant = consonants[rand(consonantLength)];
    let randVowel = vowels[rand(vowelsLength)];
    // not sure what are you doing here, so can't comment much. It is a very good practice to leave some comments on places where the main logic of your code is happening
    word += (i === 0) ? randConsonant.toLowerCase() : randConsonant;
    word += i*2 < length-1 ? randVowel : '';
  }

  return word;
 }

 // the jQuery event 'click' is a bit old and will get some day deprecated, instead I would suggest to use 'on' 
 $("#click").on('click',function(){
  $("#test").text('');
  //-- var pass1 = ra; // there is no need of this row. you can just call the function ra down below in the loop
  for(var p = 0; p < 5; p++){
    $("#test").append(ra(3)+'\n');
    }
});

$("#combine").on('click',function(){
  // let's get the value from the textarea, and as it will be a string we need to split it by rows
  // btw: I believe there is a better regex that will exclude the last \n 
  let userpass = document.getElementById('test').value.split(/\n/g);
  // and we remove the last element from the array that will be an empty one
  userpass.pop();
  // we loop through all the elements from the array 
    for (let row of userpass) {
    // we do check if there is value in row as the last element will be an empty string
    $("#test").append(row+":"+row+'\n');
  }
});

And a JSFiddle

Ale
  • 944
  • 3
  • 14
  • 34