1

I am using javascript, and I want the output of the following console.log() to be of a same length.

const str1 = 'English';
console.log(str1.padEnd(10, '.'));

const str2 = '日本語';
console.log(str2.padEnd(10, '.'));

However, the current output is as following:

> "English..."
> "日本語......."

My desired output is as following:

> "English..."
> "日本語....."

I am trying my example based on the explanation here String.prototype.padEnd()

How to achieve this?

philippos
  • 881
  • 3
  • 14
  • 32
  • Have you tried adding monospace font to the console logs? – vSR3P Mar 31 '20 at 06:28
  • No, I don’t know how to do that. Do you have any helpful guidance for this? – philippos Mar 31 '20 at 06:29
  • This should help adding css to console.log https://stackoverflow.com/questions/7505623/colors-in-javascript-console you should be able to add a font-family – vSR3P Mar 31 '20 at 06:31
  • Thanks for your help, but I think that's not exactly what I want. For example, how that could be applied using my example on this page? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd – philippos Mar 31 '20 at 06:38
  • I mean if I copy/paste my code and try the output, how can I get the desired output based on your suggestion? I will add the desired output to my question. – philippos Mar 31 '20 at 06:40

1 Answers1

-1

For doing this you can find the length of both strings using the length() methods of javascript and then add the padEnd method according to the difference

const str1 = 'English';
const str2 = '日本語';

const a=str1.length;
const b=str2.length;

const c= a-b;
console.log(str1);
console.log(str2.padEnd(c,'.'))

This will also make the whole thing dynamic as well

Denis Tsoi
  • 5,957
  • 5
  • 26
  • 46
  • Have you tried your code on the page I mentioned in my question? I tried it there and it gave me the following error: `Error: str1.length is not a function` – philippos Mar 31 '20 at 06:51
  • Basically, what the `length` returns is the count of the characters in the string. What I want is the "real" space that is taken by the character. – philippos Mar 31 '20 at 07:18