-1

I have this piece of Javascript code (using jQuery) which is 146 Bytes :

$("#b").click(s=>{t=$("#a").val();o=[...t];for(p=0;p<t.length;p++){r=Math.random();if(.1>r)if(.03>r)o[p]=t[p--];else r>.07?o[p]="":o[p]+=t[p]}$("p").html(o)})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="a" placeholder="Insert here some text"/>
<button id="b">Generate typos</button>
<p></p>

Basically what happens is when your click the button, each letter of the inserted text has a 10% chance of getting a typo, replacing the letter with the previous letter or just vanishes.

This piece of code is based on this codegolf question, however I have modified the question to my personal liking's.

Is there any way I can reduce the amount of bytes used for the Javascript code? Note: I don't need any security improvements nor performance improvements.

here is an tidied version of the Javascript code to understand what is written down:

$("#b").click(s => { //onclick
t = $("#a").val(); //get value of input
  o = [...t]; //create array with each char in t as an index
  for (p in o) { //loop through o
    r = Math.random(); //generate random number
    if (.1 > r) //if the random number is lower than 0.1 (~10% chance)
      if (.03 > r) o[p] = t[p--]; //if the random number is lower than 0.03
      else r > .07 ? o[p] = "" : o[p] += t[p] //else check if the random number is higher than 0.07
  } //closing for loop
  $("p").html(o) //replace content in p tags
})

update: i have improved it to the max of my knowledge (137 bytes):

$("#b").click(a=>{t=$("#a").val(),o=[...t];for(p in o)r=Math.random(),.1>r&&(.03>r?o[p]=t[p--]:r>.07?o[p]="":o[p]+=t[p]);$("p").html(o)})

which tidied look like this:

$("#b").click( //onclick
  a => { //arrow function
  t = $("#a").val(), //get value of #a
  o = [...t]; //make t an array
  for (p in o) //loop through the array
  r = Math.random(), //generate random number
  .1 > r //if the number is smaller than 0.1
  && //and
  (.03 > r ? o[p] = t[p--] : r > .07 ? o[p] = "" : o[p] += t[p]);//check if the number is smaller than 0.03 else check if number is smaller than 0.7
  $("p").html(o)//write to p tag
})
Community
  • 1
  • 1
Kevin Kloet
  • 1,086
  • 1
  • 11
  • 21
  • 4
    If you feel 146 bytes is too much you must be very upset with the size of jQuery also being required :) Why are you worrying about such a tiny script? – iCollect.it Ltd Dec 06 '16 at 12:41
  • this is just because i felt like writing the most minimalistic script for this i could come up with. – Kevin Kloet Dec 06 '16 at 12:43
  • 4
    [You could replace `Math.random()` with 0.4.](https://xkcd.com/221/) That would save 10 bytes and would probably be as much real-world help as any other answers to this question. – lonesomeday Dec 06 '16 at 12:45
  • Why don't you write normal code and then just minify it at the end with some tools? – zmuci Dec 06 '16 at 12:46
  • @GoneCoding this is basically a question about if someone knows how to minimize my already minimized code – Kevin Kloet Dec 06 '16 at 12:56
  • @AnubhavSaini yes except this time you don't create code but minize code – Kevin Kloet Dec 06 '16 at 12:57
  • 1
    There is nothing to answer here. This suits on code golf. –  Dec 06 '16 at 12:59
  • 1
    wrong, codegolf is about creating code based on a question that is an minimal amounts of bytes, this is about reducing already existing code. – Kevin Kloet Dec 06 '16 at 13:03
  • Possibly better for http://codereview.stackexchange.com/ – Liam Dec 06 '16 at 13:15
  • @Liam this question was posted there a couple of minutes before it was posted here and they said it would be better on another site like stackoverflow. – Kevin Kloet Dec 06 '16 at 13:17
  • 1
    Maybe it's not relevant anywhere then. IMO This is open ended and not an actual defined issue anyone is likely to need again. So it may benefit you but no one else – Liam Dec 06 '16 at 13:19

1 Answers1

1

DOM elements with id attribute are global variables, so for example you can replace $("#b") with $(b).

$(b).click(s=>{t=$(a).val(),o=[...t];for(q in o)r=Math.random(),.1>r&&(.03>r?o[q]=t[q--]:r>.07?o[q]="":o[q]+=t[q]);$(p).html(o)});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="a" placeholder="Insert here some text"/>
<button id="b">Generate typos</button>
<p id="p"></p>
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177