简体   繁体   中英

JavaScript-Random Greek Letters Not Working

So I have this absolutely fantastic javascript random stuff generator, except for one thing. You see, I have it set so that if it brings up a certain random value, it throws in a Greek letter too. It worked great until I worked on it some and now that part is broken. The rest of it works, but that one part is necessary for it to make sense to those using it. Here is the code:

<div id="unitInfo"></div>

<script type="text/javascript">
var pre = ["Research", "Storage", "Containment", "Mobile", "Biological", "Armed",     "Dimensional", "Reliquary"];
var types = ["Command", "Site", "Sector", "Area", "Unit", "Task Force"];
var greek = ["Alpha", "Beta", "Gamma", "Delta", "Epsilon", "Zeta", "Eta", "Theta",     "Iota", "Kappa", "Lambda", "Mu", "Nu", "Xi", "Omicron", "Pi", "Rho", "Sigma", "Tau",     "Upsilon", "Phi", "Chi", "Psi", "Omega"];
var update = ["online", "offline", "classified", "under repair", "experiencing     containment breach", "under attack", "[CONNECTION SEVERED]", "[DATA EXPUNGED]",     "terminated", "rogue", "secure", "secure", "secure", "secure", "secure", "secure",     "secure", "secure", "secure", "secure", "secure", "secure", "secure", "secure", "secure",     "secure", "secure", "secure", "secure", "secure", "secure", "secure", "secure", "secure",     "secure", "secure", "secure", "secure", "secure", "secure", "secure", "secure", "secure",     "secure", "secure", "secure"];

var info = pre[Math.floor(Math.random() * pre.length)];
    info += " " + types[Math.floor(Math.random() * types.length)];
if ( types == "Task Force" )
{
    info += " " + greek[Math.floor(Math.random() * greek.length)];
    info += "-" + ( 1 + Math.floor(Math.random() * 50) );
    info += " is " + update[Math.floor(Math.random() * update.length)];
}
else
{
    info += "-" + ( 1 + Math.floor(Math.random() * 50) );
    info += " is " + update[Math.floor(Math.random() * update.length)];
}
document.getElementById("unitInfo").innerHTML = info;
</script>

The info picks from pre and hence can never be "Task force" , so the info += greek[...] part of the if branch is never triggered.


May be this is what you want -

var info = types[Math.floor(Math.random() * types.length)];

if ( info == "Task Force" )
{
    info = pre[Math.floor(Math.random() * pre.length)] + " " + info;
    info += " " + greek[Math.floor(Math.random() * greek.length)];
    info += "-" + ( 1 + Math.floor(Math.random() * 50) );
    info += " is " + update[Math.floor(Math.random() * update.length)];
}
else
{
    info = pre[Math.floor(Math.random() * pre.length)] + " " + info;
    info += "-" + ( 1 + Math.floor(Math.random() * 50) );
    info += " is " + update[Math.floor(Math.random() * update.length)];
}

PS. Very cool selection of words.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM