Home > Enterprise >  Using Emoji's in Loops in JavaScript
Using Emoji's in Loops in JavaScript

Time:09-21

I am currently writing a JavaScript which needs to display a multiplication table. However, with the output, there needs to be emojis instead of numbers. For example, instead of 1 x 2 = 2, it needs to be 1 x 'emoji' 'emoji' = 2.

This is my current JavaScript code:

          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)); 
          }
       }

I would really appreciate if anyone could give me pointers as to how I can answer this. Thank you!

CodePudding user response:

As @CBroe mentioned in their response, you can use the .repeat() function of String.

Have a look here: 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)); 
    }

CodePudding user response:

You need to find the unicode value of the emoji

https://unicode-table.com/en/1F438/

here you can see that the unicode is U 1F438

then use String.fromCodePoint() to make JS display it as an actual emoji and repeat it with 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)); 
}
  • Related