The idea is to generate a random reverse acronym for the acronym 'REG' using a list of words that begin with 'R', 'E' and 'G'. Every time you visit the page that the code is on it should pick a word from each list and place them in the REG order (example: Rodent Echo Ghost, Ronald Evening Garden, etc.)
我们的想法是使用以“R”,“E”和“G”开头的单词列表为首字母缩略词“REG”生成一个随机反向首字母缩略词。每次访问代码所在的页面时,都应从每个列表中选择一个单词并将其置于REG顺序中(例如:Rodent Echo Ghost,Ronald Evening Garden等)
The result of the code should be displayed as text on a webpage ofcourse.
代码的结果应该显示为课程网页上的文本。
Optional would be the ability to choose the font family, size and colour.
可选的是能够选择字体系列,大小和颜色。
I searched around for this kind of code but to no avail. Probably also good to mention that I don't have much experience with Javascript at all, so any help is appreciated.
我四处寻找这种代码,但无济于事。可能还好提一下,我对Javascript没有多少经验,所以任何帮助都表示赞赏。
2 个解决方案
#1
1
You can use this code to get the string that you want. You can add in each array any number of words that start with the letters..
您可以使用此代码来获取所需的字符串。您可以在每个数组中添加以字母开头的任意数量的单词。
Then, the variable acron will hold the string that you want. You can inject it wherever you need. Since I do not know what you want to do with it, I can't elaborate.
然后,变量acron将保存您想要的字符串。您可以在任何需要的地方注射它。由于我不知道你想用它做什么,我无法详细说明。
var r=[
"Rodent",
"Ronald",
"Robocop",
];
var e=[
"Echo",
"Evening",
"Everyone",
];
var g=[
"Ghost",
"Garden",
"Green"
]
var randr=Math.floor((Math.random() * r.length) );
var rande=Math.floor((Math.random() * e.length) );
var randg=Math.floor((Math.random() * g.length) );
var acron=r[randr]+" "+e[rande]+" " + g[randg];
小提琴
#2
1
You just need to split the acronym into letters, and map each letter to an array of words. Here's the jsFiddle.
您只需要将首字母缩写词分成字母,并将每个字母映射到一个单词数组。这是jsFiddle。
function findWordsForAcronym(acronym) {
var words = {
'a': [],
'b': [],
'c': [],
'd': [],
'e': ['Echo', 'Evening'],
'f': [],
'g': ['Ghost', 'Garden'],
'h': [],
'i': [],
'j': [],
'k': [],
'l': [],
'm': [],
'n': [],
'o': [],
'p': [],
'q': [],
'r': ['Rodent', 'Ronald'],
's': [],
't': [],
'u': [],
'v': [],
'w': [],
'z': [],
'y': [],
'z': []
};
return acronym.toLowerCase().split('').map(
function(letter){
return words[letter][parseInt(Math.random()*words[letter].length)];
}
).join(' ');
}
findWordsForAcronym('REG'); // "Rodent Echo Garden"
#1
1
You can use this code to get the string that you want. You can add in each array any number of words that start with the letters..
您可以使用此代码来获取所需的字符串。您可以在每个数组中添加以字母开头的任意数量的单词。
Then, the variable acron will hold the string that you want. You can inject it wherever you need. Since I do not know what you want to do with it, I can't elaborate.
然后,变量acron将保存您想要的字符串。您可以在任何需要的地方注射它。由于我不知道你想用它做什么,我无法详细说明。
var r=[
"Rodent",
"Ronald",
"Robocop",
];
var e=[
"Echo",
"Evening",
"Everyone",
];
var g=[
"Ghost",
"Garden",
"Green"
]
var randr=Math.floor((Math.random() * r.length) );
var rande=Math.floor((Math.random() * e.length) );
var randg=Math.floor((Math.random() * g.length) );
var acron=r[randr]+" "+e[rande]+" " + g[randg];
小提琴
#2
1
You just need to split the acronym into letters, and map each letter to an array of words. Here's the jsFiddle.
您只需要将首字母缩写词分成字母,并将每个字母映射到一个单词数组。这是jsFiddle。
function findWordsForAcronym(acronym) {
var words = {
'a': [],
'b': [],
'c': [],
'd': [],
'e': ['Echo', 'Evening'],
'f': [],
'g': ['Ghost', 'Garden'],
'h': [],
'i': [],
'j': [],
'k': [],
'l': [],
'm': [],
'n': [],
'o': [],
'p': [],
'q': [],
'r': ['Rodent', 'Ronald'],
's': [],
't': [],
'u': [],
'v': [],
'w': [],
'z': [],
'y': [],
'z': []
};
return acronym.toLowerCase().split('').map(
function(letter){
return words[letter][parseInt(Math.random()*words[letter].length)];
}
).join(' ');
}
findWordsForAcronym('REG'); // "Rodent Echo Garden"