-1

I'm taking an online course which uses a bot to correct JavaScript code. The challenge is to Create an array a and make so that a.length === 2 is true, a[0].length === 1 is true,, and a.flat() should 'print out' ['a', 'b' 'c']

The bot output tells me that 'defines a such that a[0].length === 1 evaluates to true' isn't correct.

This is my code so far:

let a = ['a','b','c']

a.length === 2
a[0].length === 1
a.flat()

So far I've tried to put an array inside the array, but I can't figure out where to put the brackets, like this, but it doesn't work.

let a = [['a','b'],'c']

a.length === 2
a[0].length === 1
a.flat()

1 Answers1

1

I would go for the following:

let a = ['a', ['b', 'c']]

console.log('a.length === 2', a.length === 2)
// a.length === 2 true

console.log('a[0].length === 1', a[0].length === 1)
// a[0].length === 1 true

console.log('a.flat()', a.flat())
// a.flat() ['a', 'b', 'c']

The first "test" states that the length of the a array has to be two, hence I'll start with:

let a = [ _, _ ]

The second "test" states that the first element of a must have a length of 1. Having a total of 3 elements, this brought me to:

let a = [ _, [ _, _ ] ]

Filling the gaps, then:

let a = ['a', ['b', 'c']]

Hope it is clearer this way :)

0xc14m1z
  • 3,675
  • 1
  • 14
  • 23