随机化显示

时间:2014-12-15 15:49:13

标签: javascript math if-statement random hex

我有一个随机发生器,它给了我6个不同的字母和6个不同的数字。最后,这将用于十六进制数字。

我正在尝试创建一个随机数发生器,它将在十六进制(1-6)的每个插槽的数字或字母之间进行选择。

我可以创建一个在它们之间选择的,但是一旦它选择,它就不知道它们在字母转换中的编号是什么

示例:

if (mega === 1) { return "a"; }

它不记得在字母/数字选择器内部的转换。

如果有人理解我的目标,请提供帮助!谢谢。 :)

//编辑 - 整个代码 `

Variables Below Here

//Numbers
var ayy2 = Math.random() * 10;
var ayy = Math.floor(ayy2);

var lmd2 = Math.random() * 10;
var lmd = Math.floor(lmd2);

var qua2 = Math.random() * 10;
var qua = Math.floor(qua2);

var abc2 = Math.random() * 10;
var abc = Math.floor(abc2);

var cdf2 = Math.random() * 10;
var cdf = Math.floor(cdf2);

var lgt2 = Math.random() * 10;
var lgt = Math.floor(lgt2);

//Letters
var mega2 = Math.random() * 7;
var mega = Math.floor(mega2);

var nade2 = Math.random() * 7;
var nade = Math.floor(nade2);

var shot2 = Math.random() * 7;
var shot = Math.floor(shot2);

var suck2 = Math.random() * 7;
var suck = Math.floor(suck2);

var pepe2 = Math.random() * 7;
var pepe = Math.floor(pepe2);

var lols2 = Math.random() * 7;
var lols = Math.floor(lols2);

//Choices Between Hexes\\

var shrek2 = Math.random() * 3;
var shrek = Math.floor(shrek2);

var tpkek2 = Math.random() * 3;
var tpkek = Math.floor(tpkek2);

var waffl2 = Math.random() * 3;
var waffl = Math.floor(waffl2);

var snipe2 = Math.random() * 3;
var snipe = Math.floor(snipe2);

var optic2 = Math.random() * 3;
var optic = Math.floor(optic2);

var fazed2 = Math.random() * 3;
var faze = Math.floor(fazed2);

// Just for Dylan: ff88c6

//Functions Below Here\\

//Letter Returns

function returnLetter1() {
    if (mega === 1) {
        return "a";
    }
    if (mega === 2) {
        return "b";
    }
    if (mega === 3) {
        return "c";
    }
    if (mega === 4) {
        return "d";
    }
    if (mega === 5) {
        return "e";
    }
    if (mega === 6) {
        return "f";
    } else {
        return "f";
    }
}

function returnLetter2() {
    if (nade === 1) {
        return "a";
    }
    if (nade === 2) {
        return "b";
    }
    if (nade === 3) {
        return "c";
    }
    if (nade === 4) {
        return "d";
    }
    if (nade === 5) {
        return "e";
    }
    if (nade === 6) {
        return "f";
    } else {
        return "f";
    }
}

function returnLetter3() {
    if (shot === 1) {
        return "a";
    }
    if (shot === 2) {
        return "b";
    }
    if (shot === 3) {
        return "c";
    }
    if (shot === 4) {
        return "d";
    }
    if (shot === 5) {
        return "e";
    }
    if (shot === 6) {
        return "f";
    } else {
        return "f";
    }
}

function returnLetter4() {
    if (suck === 1) {
        return "a";
    }
    if (suck === 2) {
        return "b";
    }
    if (suck === 3) {
        return "c";
    }
    if (suck === 4) {
        return "d";
    }
    if (suck === 5) {
        return "e";
    }
    if (suck === 6) {
        return "f";
    } else {
        return "f";
    }
}

function returnLetter5() {
    if (pepe === 1) {
        return "a";
    }
    if (pepe === 2) {
        return "b";
    }
    if (pepe === 3) {
        return "c";
    }
    if (pepe === 4) {
        return "d";
    }
    if (pepe === 5) {
        return "e";
    }
    if (pepe === 6) {
        return "f";
    } else {
        return "f";
    }
}

function returnLetter6() {
    if (lols === 1) {
        return "a";
    }
    if (lols === 2) {
        return "b";
    }
    if (lols === 3) {
        return "c";
    }
    if (lols === 4) {
        return "d";
    }
    if (lols === 5) {
        return "e";
    }
    if (lols === 6) {
        return "f";
    } else {
        return "f";
    }
}

//Return Numbers

function returnNumber1() {
    var zz = qua;
    return zz;
}

function returnNumber2() {
    var mm = lmd;
    return mm;
}

function returnNumber3() {
    var hh = ayy;
    return hh;
}

function returnNumber4() {
    var jj = abc;
    return jj;
}

function returnNumber5() {
    var ww = cdf;
    return ww;
}

function returnNumber6() {
    var rr = lgt;
    return rr;
}

/*
This next little bit here is for my enjoyment and because it's necessary for the script. :P
*/

//Obviously, the above are to get a number of 1 or 2 (0 being else so 66/33% chances w/e) to find out if it will be a number or letter. 

//But, I can't find a way for the function below to show me what the hexes will be WITH the number>letter from the returnL's.

//Could it be that I possibly would have to move the mega === x below the hexchoices? sigh 

function returnHexChoice1() {
    if (shrek === 1) {
        return zz;
    } else {
        return mega;
    }
}

//Finals of the script display necessary.

//DID I WIN???????
function getRandomColor() {
   var letters = '0123456789ABCDEF'.split('');
   var color = '#';
   for (var i = 0; i < 6; i++ ) 
   {
       color += letters[Math.floor(Math.random() * 16)];
   }
   return color;
}

function hex() {
    return "philosophy";
}

1 个答案:

答案 0 :(得分:0)

哇,你对变量命名非常满意。我通常将地雷命名为“屁”或类似的东西。这也是因为如果你试图利用编程语言的结构,你将不需要那么多的变量。

在你提出任何其他问题之前,tou可能想查看一个教程。如果javascript是您感兴趣的内容,请尝试此处:例如:http://www.htmldog.com/guides/javascript/。了解函数,数组等。

  1. 例如,除了使用不同的值外,您的所有returnLetterX()都是相同的。尝试只使用一个使用returnLetter(value)的{​​{1}}知道要返回的内容。此外,您可以使用开关,而不是同一个变量上的一长串if / else语句。这正是他们的目的!

  2. 然后您的value函数不做任何事情,除了从您的姓名到数字的映射。您可能希望使用数组,因此名称较少。

  3. 最后,您在开始时的所有returnNumberX来电都是重复的。结合数组,您可以轻松地循环执行此操作。

  4. 您的代码可能看起来像:

    rand()

    你可以看到它更小,更容易为程序员阅读。

    一旦你理解了所有这些代码并且你有能力自己写类似的东西,你可能想知道选择随机十六进制数字的最佳方法,尽管它已经在你从某处复制的function letter(let) { switch(let) { case 0: return 'a'; case 1: return 'b'; case 2: return 'c'; case 3: return 'd'; case 4: return 'e'; case 5: return 'f'; } // even better, use an array : // var abcdef = ['a', 'b', 'c', 'd', 'e', 'f']; // return abcdef[let]; } function hexChoice(num, let, choice) { if (choice == 0) // 0 for 33%, 1-2 for 67% return letter(let); else return num; } // this is not the best way to pick hex values ! for(var i=0; i<6; i++) { var number = Math.floor( Math.random() * 10 ); var letter = Math.floor( Math.random() * 6 ); // there are only 6 letters ! var letnum = Math.floor( Math.random() * 3 ); var hex_digit = hexChoice(number, letter, letnum); // now do something with your hex digit... print it, add it to the "color" string ? } 中。搜索互联网(这个网站,教程......)这个问题已经被问了很多,不需要重新询问。