Wednesday, November 25, 2015

Making up Lorem Ipsum

The following javascript function showcases two inner functions that have only scope for the outer function. Not something I use often, but seemed apt for what I was doing here. The output generated looks like this (a cross between Hungarian/Turkish and Indonesian?)
ur tojo imfuw ukba aral inoma bodfuy iknedik ofat aav id evajuragonephu nimemobi umajvamjo lo bijpaulim ugpeibgac depi puisrejnahafi getegvoeysepied hu iclupgak parjonuf bos aivhocu us pokehekgerbak mugu itdaceii ikimhup penka iduab tojeme idmethe nalay ugservanuu atub pavdiv sep cowgimaag gon ipokipaberuvapadaehovcahat lonugirfi acasinal giosug dihocadu vumfilocoy ru benumeohuj rejav puj cidomten rom godioncogal likviu hucous cecioieruhubewuda naivac ive.

function randomClauseMarkup() {

    var initials = "bc df gh jk lm np rs tv"; // chance of an initial is 66%
    var vowels = "aeiou"; // 100%, since all syllables must have vowels
    var finals = "y w b c d f g j k l m n p r s t v"; // change of final is 50%
    var randomsyl=function() {
        var initial = initials.charAt( Math.floor(Math.random()*initials.length));
        var vowel = vowels.charAt( Math.floor(Math.random()*vowels.length));
        var final = finals.charAt( Math.floor(Math.random()*finals.length));
        return (initial+vowel+final).trim()
    }

    var randomword=function() {
        var ns= 1 + Math.floor(Math.random()*4);
        var w = '';
        while (w.length<ns*2) w += randomsyl();
        return ' <span class="word">'+w+'</span>';
    }

    var s = '';
    var numwords = 3 +  Math.floor(Math.random()*30); // these clauses will have [3,33] words
    for (w = 0; w < numwords; w++) {
        s += randomword();
    }
    return ' <span class="clause">'+s+'</span>';
}

To turn the string into html, just use $.parseHTML or load element with .html()
 

No comments:

Post a Comment