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
})