[英]Using Emoji's in Loops in JavaScript
我目前正在编写需要显示乘法表的 JavaScript。 但是,对于 output,需要使用表情符号而不是数字。 例如,不是 1 x 2 = 2,而是 1 x 'emoji' 'emoji' = 2。
这是我当前的 JavaScript 代码:
function generate() {
var input = document.getElementById("value1");
var value = Number(input.value);
for (var i = 1; i <= 10; i++) {
document.write("<br />" + value + " x " + i + " = " + (value * i));
}
}
如果有人能给我指点我如何回答这个问题,我将不胜感激。 谢谢你!
正如@CBroe 在他们的回复中提到的,您可以使用String
的.repeat()
function。
看看这里: https://codepen.io/Rebouben/pen/MWoGwmM
const frog = String.fromCodePoint(0x1F438);
var num1 = 3;
for(var i = 1; i <= 10; i++){
console.log(num1+" x "+frog.repeat(i)+" = "+(num1 * i));
}
您需要找到表情符号的 unicode 值
https://unicode-table.com/en/1F438/
在这里你可以看到 unicode 是U+1F438
然后使用String.fromCodePoint()
使 JS 将其显示为实际的表情符号并使用String.repeat()
重复它
const emojiUnicode = 0x1F438;
let repeatedString;
for(var i = 1; i <= 10; i++){
repeatedString = String.fromCodePoint(emojiUnicode).repeat(i);
document.write("<br />" + num1 + " x " + repeatedString + " = " + (num1 * i));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.