mercredi 15 septembre 2021

create triangle numbers pattern javascript and verticaly snake number pattern square

i want create triangle number pattern

let segitigaPola3 = num => {
  let hasil = ''
  for (let i = 1; i <= num; i++) {
    for(let j = 0; j < i; j++) {
      hasil += `${i} `
    }
    
    hasil += ` \n`
  }
  return hasil
}

console.log(segitigaPola3(5))
console.log()
console.log()

currently output is :

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

what i want is something like for triangle :

1
2 3
4 5 4
3 2 1 2
3 4 5 4 3

and for square like :

1 10 11 20 21
2 9  12 19 22
3 8  13 18 23
4 7  14 17 24
5 6  15 16 25

how can i achieve like this? thanks for any advice :)

Aucun commentaire:

Enregistrer un commentaire