function getSumDiceValue(){
var dice = diceWindowWrapper.getElementsByClassName("dice");
var diceTotal = 0;
for(var i = 0; i < dice.length; i++){
diceTotal += Number(dice[i].getAttribute("data-diceValue"));
};
return diceTotal;
};
我想将零的类名设置为 getSumDiceValue()的值,但是用字母表示。
即如果我滚动6然后 getSumDiceValue()的值等于6,那么我希望zero.className等于6。那就是:
zero.className = six;
然后我需要“插入”这段代码:
var zero = document.createElement("li");
zero.className = <here>;
diceToolbarCounterWrapper.appendChild(zero);
我想我需要制作一些类型的数组。但我是javaScript的新手,不知道我会怎么做。请在您的答案中添加代码。
答案 0 :(得分:1)
我之前编写的函数用于转换范围为
的整数
-999 999 999 999 999 ... 999 999 999 999 999
它的全英文名称(简称)
var numberWord = (function () {
var words = [
'zero', 'one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine',
'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
];
words.length = 99;
var i, j, tens = [
'', '', 'twenty', 'thirty', 'forty',
'fifty', 'sixty', 'seventy', 'eighty', 'ninety'
];
for (i = 2; i < 10; ++i) {
words[10 * i] = tens[i];
for (j = 1; j < 10; ++j) {
words[10 * i + j] = tens[i] + ' ' + words[j];
}
}
var power = [
{e: 1e2, str: 'hundred', and: false},
{e: 1e3, str: 'thousand', and: false},
{e: 1e6, str: 'million', and: false},
{e: 1e9, str: 'billion', and: false},
{e: 1e12, str: 'trillion', and: false},
];
function strAdd(a, b) {
if (a && b) return a + ' ' + b;
return a || b || '';
}
function strAndAdd(a, b) {
if (a && b) return a + ' and ' + b;
return a || b || '';
}
return function numberWord(x) {
var number = '';
x = Math.floor(x);
if (x !== x) return 'NaN';
if (x === 0) return words[0];
if (x < 0) {
number = strAdd(number, 'minus');
x = Math.abs(x);
}
if (x > 999999999999999) throw new RangeError('Number must be in -999999999999999..999999999999999');
var i = power.length - 1, j, substr;
for (; i >= 0; --i) {
if (x >= power[i].e) {
j = Math.floor(x / power[i].e);
substr = numberWord(j) + ' ' + power[i].str;
if (power[i].and) number = strAndAdd(number, substr);
else number = strAdd(number, substr);
x %= power[i].e;
}
}
if (x > 0) {
number = strAndAdd(number, words[x]);
}
return number;
}
}());
使用示例
numberWord(+'-999,999,999,999,999'.replace(/,/g, ''));
// "minus nine hundred and ninety nine trillion nine hundred and ninety nine billion nine hundred and ninety nine million nine hundred and ninety nine thousand nine hundred and ninety nine"
numberWord(1999); // "one thousand nine hundred and ninety nine"
numberWord(2016); // "two thousand and sixteen"
答案 1 :(得分:0)
检查此功能。它获取一个数值作为参数,并将此数字作为文本返回。 (骰子的数字1到6):
var diceResult = 6;
var getNumberText = function(num) {
//Create an object that maps the relation between numeric and literal values of numbers
var numTxtSet = {
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six'
}
//Check if the given parameter is a number
if (num && !isNaN(num)) {
return numTxtSet[num];
}
}
//Call the function passing 'diceResult' as parameter and store it's value to a variable
var res = getNumberText(diceResult);
alert(res);
&#13;
答案 2 :(得分:0)
如果你的getSumDiceValue()函数将返回一个0到6范围内的数值,你可能会遇到这样的事情:
var numTxtSet = "zero,one,two,three,four,five,six".split(",");
zero.className = numTxtSet[ getSumDiceValue() ];