-2

I've found this script

<script type="text/javascript">
$(document).ready(function() {
jQuery.fn.wordgen = function(length){
  var i = 0;
  var word = "";
  var vowels = new Array("a","e","u","i","o");
  var consonants = new Array("q","w","r","t","p","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m");

  while(i < (length/2)){      
    i++;
    word += vowels[Math.floor(Math.random() * vowels.length)] + consonants[Math.floor(Math.random() * consonants.length)];
  }
  $(this).val(word);
}

$("#generarmu").click(function(){
    var longitud = $("#largomu").val();
    $("#nombremu").wordgen(longitud);
});

$("#largomu").change(function(){
    var longitud = $("#largomu").val();
    $("#nombremu").wordgen(longitud);
});


});
</script>

and i'm using it with

Longitud del nombre:
<select id="largomu" name="largomu">
<option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option> </select>
<br/>
<input id="nombremu" value="" style="padding-left:10px;height:25px;text-transform:uppercase;width:340px;"/>
<input type="input" id="generarmu" class="search_submit" style="width:200px;height:18px;" value="Generar!">

But i've got problems with even length of strings, the length of even string it's always +1 (odd)

You can check it here

http://jsfiddle.net/1pt3yzbs/

THanks in advance

Zuker
  • 456
  • 2
  • 6
  • 18
  • possible duplicate of [Generate a string of 5 random characters in Javascript](http://stackoverflow.com/questions/1349404/generate-a-string-of-5-random-characters-in-javascript) –  Sep 02 '14 at 01:29
  • The length of even strings are even in your code. The length of *odd* strings is even. You've got it backwards in your question. – therealrootuser Sep 02 '14 at 01:29
  • `jQuery` is a `JavaScript` **framework**, it is not a programming language and is completely irrelevant as a tag! Learn the difference and don't spam the tags! –  Sep 02 '14 at 01:30
  • 1
    @JarrodRoberson I think the `jquery` tag can be quite useful when paired with the `javascript` tag. It denotes that the question involves jQuery, and that a jQuery solution is acceptable. Not everyone that knows JavaScript knows jQuery, and in many cases, people use jQuery without understanding the underlying pure JavaScript. So perhaps not completely irrelevant. – flowstoneknight Sep 02 '14 at 01:38
  • In this case it is completely irrelevant, and the sooner people learn the difference the better off the entire internet will be! –  Sep 02 '14 at 01:40

2 Answers2

0

Because, even number / 2 will be even, then in your while i stop before reach number/2 , check your condition i < (length/2)

Possible Fix:

var stop_condition = length/2;
if (stop_condition % 2 === 0 )
     stop_condition += 1;

    while(i < stop_condition)
    //continue
levi
  • 22,001
  • 7
  • 73
  • 74
0

I don't know JQuery. But based on logic in code, here is what I can deduce:

len = 5

len/2 = 2.5

i=0

Iter1: 0<2.5 -- execute

Iter 2: 1<2.5 -- execute

Iter 3: 2<2.5 -- execute

Plus seems like you are halfing the length, and doing something that is adding word twice.

So 5/2 = 2.5

Based on above - executes 3 times... and based on logic (as it seems to be executed twice), 3x2 = 6 total characters.

Edit: word += vowels[Math.floor(Math.random() * vowels.length)] + consonants[Math.floor(Math.random() * consonants.length)];

The above line is adding word twice per iteration. So any iteration count (even or odd ), words are getting added twice, always showing even count.

Sri
  • 63
  • 1
  • 1
  • 8