skip to Main Content

I already know that the largest string within the array has 12 letters, how do I display the value of this String, which in this case is "rioDeJaneiro"?

var cidades = ["Sãopaulo", "Riodejaneiro", "Fortaleza"]

var biggerWord = 0
for (var i = 0; i < cidades.length; i++) {
  console.log(cidades[i].length)

  if (cidades[i].length > biggerWord) {
    biggerWord = cidades[i].length 
  }
}

console.log("--------------------------------------------------")
console.log("A maior palavra tem: " + biggerWord)

I already looked for methods in the documentation but I couldn’t find them.

var cidades = ["Sãopaulo", "Riodejaneiro", "Fortaleza"]

var biggerWord = 0
for (var i = 0; i < cidades.length; i++) {
  console.log(cidades[i].length)

  if (cidades[i].length > biggerWord) {
    biggerWord = cidades[i].length
  }


}

console.log("--------------------------------------------------")
console.log("A maior palavra tem: " + biggerWord)

6

Answers


  1. Save the longest word, or the index of the longest word, instead of its length. When you have the longest word or its array index then you can access the value and any other derived "data" from it, like its length easily.

    Examples:

    const cidades = ["Sãopaulo", "Riodejaneiro", "Fortaleza"];
    
    let biggestIndex = 0;
    
    for (let i = 0; i < cidades.length; i++) {
      console.log(cidades[i], cidades[i].length);
    
      if (cidades[i].length > cidades[biggestIndex].length) {
        biggestIndex = i;
      }
    }
    
    console.log("--------------------------------------------------");
    console.log(`Longest word: ${cidades[biggestIndex]} at index: ${biggestIndex}`);
    const cidades = ["Sãopaulo", "Riodejaneiro", "Fortaleza"];
    
    let longestWord = "";
    
    for (let i = 0; i < cidades.length; i++) {
      console.log(cidades[i], cidades[i].length);
    
      if (cidades[i].length > longestWord.length) {
        longestWord = cidades[i];
      }
    }
    
    console.log("--------------------------------------------------");
    console.log(`Longest word: ${longestWord}`);
    Login or Signup to reply.
  2. You can use reduce, this way:

    var cidades = ["Sãopaulo", "Riodejaneiro", "Fortaleza"];
    
    var longestCity = cidades.reduce((a, b) => a.length > b.length ? a : b);
    
    console.log("The longest city is: " + longestCity);
    Login or Signup to reply.
  3. Way 1:

    var cidades = ["Sãopaulo", "Riodejaneiro", "Fortaleza"]
    
    var biggestWord = ""
    for (var i = 0; i < cidades.length; i++) {
      console.log(cidades[i].length)
    
      if (cidades[i].length > biggestWord.length) {
        biggestWord = cidades[i]
      }
    }
    
    console.log("--------------------------------------------------")
    console.log(`Biggest word is ${biggestWord}, with the length of: ${biggestWord.length}`)

    Way 2:

    var cidades = ["Sãopaulo", "Riodejaneiro", "Fortaleza"]
    var biggestWord = cidades.reduce((accumulator, current) => {
      if (current.length > accumulator.length) {
        return current
      }
      return accumulator
    }, "")
    console.log(`Biggest word is ${biggestWord}, with the length of: ${biggestWord.length}`)
    Login or Signup to reply.
  4. Here we will talk about how to use JavaScript properly To show off position and value of the largest string in an array, based on the length of string that is used.

    javascript

    let arr = [‘apple’, ‘banana’, ‘strawberry’, ‘kiwi’,’pineapple’];

    let largestString = arr. (y, x) => y.length > x.length? a : b);

    let position = arr. indexOf(largestString);

    console. log(Position: ${position}, Value: ${largestString})

    In this case the output will be aware of this array: []

    Rank: 2, Fruit: strawberry

    Edit, answer lacked a reduce call to be valid JS, correct example below

    First declare a stringArr to be used in example

    const stringArr = ["a", "bb", "ccc", "d"];
    

    Then find the longest string by comparing using the JS function "reduce", and assign that to const longestString.

    const longestString = stringArr.reduce(
      (longestString, currentString) => longestString.length> + currentString.length ? longestString:currentString);
    

    The reduce function takes whatever is returned from the function and uses it on the next index of the array, so here we simply compare the length of currentIndex with whatever the current returned value is (longeststring). then returns which of those are the longest.

    Finally, console log to prove its correctly implemented

    let position = stringArr.indexOf(longestString);
    console.log(position,longestString);//output: 2,"ccc"
    
    Login or Signup to reply.
  5. Here is a version that is still O(n) but avoids

    1. looking up the word each time
    2. getting the length of the array for each iteration
    3. Measuring the saved word each iteration

    https://jsperf.app/jadapa

    All this is of course irrelevant for 3 words.

    const cidades = ["Sãopaulo", "Riodejaneiro", "Fortaleza"];
    
    let biggestIndex = 0, biggestWord = "", biggestLength = 0;
    
    for (let i = 0, n = cidades.length; i < n; i++) {
      let word = cidades[i];
      let wordLength = word.length;
      
      if (wordLength > biggestLength) {
        biggestIndex = i;
        biggestWord = word;
        biggestLength = wordLength; 
      }
    }
    
    console.log(`Longest word: ${biggestWord} at index: ${biggestIndex}`);
    Login or Signup to reply.
  6. Another alternative is;

    var cidades = ["Sãopaulo", "Riodejaneiro", "Fortaleza"];
    const arrLength = cidades.map((x) => x.length);
    
    var i=(arrLength.indexOf(Math.max(...arrLength)));
    
    console.log(cidades[i]);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search