Create array of arrays. The country name, the first three letters of the country name and the length of the country name

Viewed 44

I want to create an array of arrays by using below array:

const countries = ['Albania','Bolivia','Canada','Denmark','Ethiopia','Finland','Germany','Hungary','Ireland','Japan','Kenya' ];

The output should look like as below:

[['Albania', 'ALB', 7],['Bolivia', 'BOL', 7],['Canada', 'CAN', 6],['Denmark', 'DEN', 7],['Ethiopia', 'ETH', 8],['Finland', 'FIN', 7],['Germany', 'GER', 7],['Hungary', 'HUN', 7],['Ireland', 'IRE', 7],['Japan', 'JAP', 5],['Kenya', 'KEN', 5]]

I have to do this only using 'loops' or nested 'loops' but not using any functions method, so my code is as below:

const countries = ['Albania', 'Bolivia', 'Canada', 'Denmark', 'Ethiopia', 'Finland', 'Germany', 'Hungary', 'Ireland', 'Japan', 'Kenya'];

let arr = [];

let i = 0;

while (i < countries.length) {


  arr.push(countries[i], countries[i].slice(0, 3).toUpperCase(), countries[i].length);
  i++
  console.log(arr);

}

I'm getting output as below:

[ 'Albania', 'ALB', 7 ][ 'Albania', 'ALB', 7, 'Bolivia', 'BOL', 7 ]['Albania', 'ALB',7,'Bolivia','BOL',7,'Canada', 'CAN',6]['Albania', 'ALB',7,'Bolivia','BOL',7,'Canada', 'CAN',6,'Denmark','DEN',7]['Albania','ALB',7,'Bolivia','BOL',7,'Canada','CAN',6,     'Denmark','DEN',7,'Ethiopia', 'ETH',8]['Albania', 'ALB', 7,'Bolivia',  'BOL', 7,'Canada','CAN',6,'Denmark','DEN', 7,'Ethiopia','ETH', 8,'Finland','FIN', 7]['Albania',  'ALB',7,'Bolivia',  'BOL', 7,'Canada',   'CAN', 6,'Denmark',  'DEN', 7,'Ethiopia','ETH', 8,'Finland',  'FIN', 7,'Germany',  'GER', 7]['Albania',  'ALB', 7,'Bolivia',  'BOL', 7,'Canada',   'CAN', 6,'Denmark',  'DEN', 7,'Ethiopia', 'ETH', 8,'Finland',  'FIN', 7,'Germany',  'GER', 7,'Hungary',  'HUN', 7]['Albania',  'ALB', 7,'Bolivia',  'BOL', 7,'Canada',   'CAN', 6,'Denmark',  'DEN', 7,'Ethiopia', 'ETH', 8,'Finland',  'FIN', 7,'Germany',  'GER', 7,'Hungary',  'HUN', 7,'Ireland',  'IRE', 7]['Albania',  'ALB', 7,'Bolivia',  'BOL', 7,'Canada',   'CAN', 6,'Denmark',  'DEN', 7,'Ethiopia', 'ETH', 8,'Finland',  'FIN', 7,'Germany',  'GER', 7,'Hungary',  'HUN', 7,'Ireland',  'IRE', 7,'Japan',    'JAP', 5]['Albania',  'ALB', 7,'Bolivia',  'BOL', 7,'Canada',   'CAN', 6,'Denmark',  'DEN', 7,'Ethiopia', 'ETH', 8,'Finland',  'FIN', 7,'Germany',  'GER', 7,'Hungary',  'HUN', 7, 'Ireland',  'IRE', 7,'Japan',    'JAP', 5,'Kenya',    'KEN', 5]

Can anyone help me what should be modifed in my code to get the correct output

3 Answers

You can easily make it by using map function.

const countries = ['Albania','Bolivia','Canada','Denmark','Ethiopia','Finland','Germany','Hungary','Ireland','Japan','Kenya' ];

const countriesGroup = countries.map((country) => [
  country, 
  country.slice(0, 3).toUpperCase(), // convert name to three letters
  country.length // get string length
])

console.log(countriesGroup)

You had 2 mistakes in your code

  1. You wanted to push an array into your array but you hadnt surrounded it in [] so you were actually pushing 3 new items into your output array
  2. You were console.log inside the loop so it was outputting every iteration - this should be outside the loop.

const countries = ['Albania', 'Bolivia', 'Canada', 'Denmark', 'Ethiopia', 'Finland', 'Germany', 'Hungary', 'Ireland', 'Japan', 'Kenya'];

let arr = [];
let i = 0;
while (i < countries.length) {
  arr.push([countries[i], countries[i].slice(0, 3).toUpperCase(), countries[i].length]);
  i++
}
console.log(arr);

  • You are logging the result within te loop, so every iteration the complete array is displayed;
  • No need for a while loop, you can use for ... of;
  • Push an Array with the 3 values to the new array;
  • Alternatively you can Array.map, as already worked out in other answers

const countries = [
  'Albania', 'Bolivia', 'Canada', 'Denmark', 
  'Ethiopia', 'Finland', 'Germany', 'Hungary', 
  'Ireland', 'Japan', 'Kenya'];
let arr = [];

// use for ... of
for (let country of countries) {
  arr.push([country, country.slice(0, 3).toUpperCase(), country.length]);
  //       ^ push an array
}

// display the result once
console.log(JSON.stringify(arr));

Alternative may be Array.reduce.

console.log( JSON.stringify( [
  'Albania', 'Bolivia', 'Canada', 'Denmark', 
  'Ethiopia', 'Finland', 'Germany', 'Hungary', 
  'Ireland', 'Japan', 'Kenya']
  .reduce( (acc, val) => 
    [...acc, [val, val.slice(0, 3).toUpperCase(), val.length]], []) )
);

Related