Ricardo Moreira
Ricardo Moreira

Reputation: 1007

All strings with length of the longest string

Hei, I have this function:

function frame(string) {
  var words = string.split(" ");
  var longestWord = 0;
  var y = "*";

  //Find the longest word from the string
  for (var i = 0; i < words.length; i++) {
    if (words[i].length > longestWord) {
      longestWord = words[i].length;
    }
  }
  console.log(y.repeat(longestWord + 4));
  for (var i = 0; i < words.length; i++) {
    console.log(y + " " + words[i] + " " + y);
    //words[i].length = longestWord;
  }
  console.log(y.repeat(longestWord + 4));
}

I want that all the string has the same length as the longest string. The rest is working good. The code that is commented out is the one i tried last but is not working. Apparently, the logic is not totally correct, but what I am missing? Thank you

Upvotes: 0

Views: 302

Answers (6)

shivam dhankani
shivam dhankani

Reputation: 99

The first loop for finding the max length of word seems right in second loop you can use if statement All you have to do is that First define an array let a=[] In second loop in the commented part write

if(word[I].length == longestlength){a.push(word[I])} 

After that you get array containing word whose length is equal to max length Hope it helps

Upvotes: 1

Mark
Mark

Reputation: 92440

I would be tempted to get rid of some of the for loops to help readability. You can use Math.max and map to find the longest word and padEnd to fix the length:

function frame(string, y = '*') {
  var words = string.split(" ");
  let longestlength = Math.max(...words.map(w => w.length))
  let header = y.repeat(longestlength + 4)
  return [
    header,
    ...words.map(word => `${y} ${word.padEnd(longestlength, ' ')} ${y}`),
    header
  ]
}

console.log(frame("hello the word javascript is long").join('\n'))

Upvotes: 2

Artur Czajka
Artur Czajka

Reputation: 18311

Strings are immutable in JS, you can't just overwrite the length attribute ;)

Do you mean, that you need padding after the word up to the length of the longest word? If I understood it correctly, then try something like this:

console.log(y + " " + words[i].padEnd(longestWord, " ") + " " + y);

This looks like the easiest way, but consult Can I use, if all your browsers are covered.

Upvotes: 2

Jonathan Gagne
Jonathan Gagne

Reputation: 4379

Calculate the space left to fill and repeat it as below.

function frame(string) {
  var words = string.split(" ");
  var longestWord = 0;
  var y = "*";

  //Find the longest word from the string
  for (var i = 0; i < words.length; i++) {
    if (words[i].length > longestWord) {
      longestWord = words[i].length;
    }
  }
  console.log(y.repeat(longestWord + 4));
  for (var i = 0; i < words.length; i++) {
    spaceLeft = longestWord - words[i].length;
    console.log(y + " " + words[i] + " ".repeat(spaceLeft) + " " + y);
    //words[i].length = longestWord;
  }
  console.log(y.repeat(longestWord + 4));
}

frame('skfj dslfska sadflkdsflkdsnf ldsknflskdnaf dslkf')

Upvotes: 1

Scott Marcus
Scott Marcus

Reputation: 65808

See comments inline:

function frame(string) {
  var words = string.split(" ");
  var longestWord = "";
  var y = "*";

  //Find the longest word from the string
  for (var i = 0; i < words.length; i++) {
    if (words[i].length > longestWord.length) {
      longestWord = words[i];
    }
  }
  console.log("Longest word is: " + longestWord)
  console.log(y.repeat(longestWord.length + 4));
  for (var i = 0; i < words.length; i++) {
    // Get the difference between the word and the longest word
    var diff = longestWord.length - words[i].length;
      // Pad the words with the right amount of spaces to make up the difference
      console.log(y + " " + words[i] + " ".repeat(diff) + y);
  }
  console.log(y.repeat(longestWord.length + 4));
}

frame("The quick brown fox jumped over the lazy dog.");

Upvotes: 0

fila90
fila90

Reputation: 1459

function frame(string) {
  var words = string.split(" ");
  var longestWord = 0;
  var y = "*";

  //Find the longest word from the string
  for (var i = 0; i < words.length; i++) {
    if (words[i].length > longestWord) {
      longestWord = words[i].length;
    }
  }
  console.log(y.repeat(longestWord));
  for (var i = 0; i < words.length; i++) {
    console.log(words[i] + y.repeat(longestWord - words[i].length));
    //words[i].length = longestWord;
  }
}

frame('Lorem ipsum dolor sit amet, consectetur adipiscing elit')

maybe something like this, now every word has same length as the longest one, empty space is filled with *

Upvotes: 0

Related Questions