-4

I have three String that I want to format:

   var a= "James"

   var b= "Stephen Curry"

   var c= "Kobe"

I'd like to comine them corresponding to their ages in one string. So it will look like this:

   James           33

   Stephen Curry   30

   Kobe            40

Although the length of the name is different, but I want the ages align to right. Is there a way to form this string?

Thanks

  • 3
    Strings don't typically have alignment information in them, that'd be something you'd do in your HTML or whatever other medium you're displaying them with. – Paul Jul 11 '18 at 18:11
  • check the length of your strings, space pad your shorties to the length of the longest, then slap your number beside it. – blaze_125 Jul 11 '18 at 18:13
  • You could try formatting using backticks. Check https://stackoverflow.com/a/27678299/8252164 – samuellawrentz Jul 11 '18 at 18:13
  • 2
    Please spend some time showing what you've tried when attempting to solve this yourself. Without an attempt we can't gauge your skill level to know what sort of answer would be helpful. Instead this turns into a work order rather than a question. – zzzzBov Jul 11 '18 at 18:13
  • 1
    Get the length of the longest string, add a few extra spaces to that, then pad each name by that length. –  Jul 11 '18 at 18:13
  • 1
    You have to try with your own code and then ask. – zfrisch Jul 11 '18 at 18:14
  • `.padEnd(20)` ? – Chris Jul 11 '18 at 18:14
  • I think that by giving the examples he shows that he understands the result he expects, so I think it is his right to ask. – Yone Jul 11 '18 at 18:19
  • 1
    @Enoy People *very* frequently put no effort into solving their tasks and expect someone else to do it for them. I'm not suggesting that's the case here, but it does have that appearance. Where is his code? Where are his attempts? What did the OP try and how did it not meet his needs? Sure, he has the *right* to ask, just as we have the right to not answer and insist he at least try. SO is not a code-writing service. –  Jul 11 '18 at 18:27
  • i do believe, there should escape-symbols which gives you tab(5 spaces as one symbol). try "/t" – Malakai Jul 11 '18 at 18:55
  • @Reborn Modern languages use `\t`. The direction of the slash matters and is generally consistent across modern languages. C-variant languages, Ruby, Python, PostScript, etc. all use these escape sequences –  Jul 11 '18 at 19:05
  • @Amy Sorry, i made typo. You are right. – Malakai Jul 11 '18 at 19:07

3 Answers3

1

var theStrings = ["Name1", "Naame2", "My Name is soooo the longest in here!", "Naaame3", "I have a very long name"];
var longestCount = 0;

//Get the longest name
for(var i = 0; i<theStrings.length;i++)
  {
if(longestCount < theStrings[i].length)
  {longestCount = theStrings[i].length}
  }
//pad your strings to longest + some
for(var i = 0; i<theStrings.length;i++)
  {
theStrings[i] = theStrings[i].padEnd(longestCount + 3, " ") + "TheAge";
console.log(theStrings[i]);
  }

resulting in

Name1                                   TheAge
Naame2                                  TheAge
My Name is soooo the longest in here!   TheAge
Naaame3                                 TheAge
I have a very long name                 TheAge
blaze_125
  • 2,262
  • 1
  • 9
  • 19
0

If this is for a web application, like the other answer said, do not format your strings with spaces in Javascript, rather use HTML and CSS to get the desired layout (Flexbox works well for this).

If this is for formatting output in a command line Node.js application, you could use String.prototype.padEnd() like so:

console.log(`${a.padEnd(20)} 33`)
console.log(`${b.padEnd(20)} 30`)
console.log(`${c.padEnd(20)} 40`)
Skylar Brown
  • 3,234
  • 2
  • 14
  • 7
0

If this is for a web application, you can just put the Strings inside a table with a new table row for each person and a new table column for the age.

<table>
<tr><td>James</td><td>33</td></tr>
<tr><td>Stephen Curry</td><td>30</td></tr>
<tr><td>Kobe</td><td>40</td></tr>
</table>

Putting in the values with Javascript:

<script>
     window.onload = function(){
     var players = ["James", "Stephen Curry", "Kobe"];
     var ages = [33, 30, 40];
     for(var i = 0; i < players.length; i++){
     var player = players[i];
     var age = ages[i];
     document.getElementById("players").innerHTML += "<tr><td>"+player+"</td><td>"+age+"</td></tr>";
     }
     }
</script>
<table id="players">  
</table>
Unmitigated
  • 76,500
  • 11
  • 62
  • 80