Skip to content

Instantly share code, notes, and snippets.

@yonisetiawan
Last active January 13, 2017 02:50
Show Gist options
  • Save yonisetiawan/d2ccf5d5ca59877e28cec3fc2c010306 to your computer and use it in GitHub Desktop.
Save yonisetiawan/d2ccf5d5ca59877e28cec3fc2c010306 to your computer and use it in GitHub Desktop.
LiveCoading Hacktiv8 Soal 3b
var masalah2 = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]
]
var hasil = []
function spriral(input, pembanding = masalah2[0].length*masalah2[0].length) {
for (var i = 0; i < input[0].length; i++) {
for (var j = 0; j < input[0].length; j++) {
if (i == 0) {
if (hasil.length == pembanding) {
return hasil
}else {
hasil.push(input[i][j])
}
} else if (i < input[0].length - 1 && j == input[0].length - 1) {
if (hasil.length == pembanding) {
return hasil
}else {
hasil.push(input[i][j])
}
}
}
}
for (var i = input[0].length - 1; i >= 0; i--) {
for (var j = input[0].length - 1; j >= 0; j--) {
if (i == input[0].length - 1) {
if (hasil.length == pembanding) {
return hasil
}else {
hasil.push(input[i][j])
}
} else if (i > 0 && j == 0) {
if (hasil.length == pembanding) {
return hasil
}else {
hasil.push(input[i][j])
}
}
}
}
var tmp = []
for (var i = 1; i < input[0].length - 1; i++) {
var tampung = []
for (var j = 1; j < input[0].length - 1; j++) {
tampung.push(input[i][j])
}
tmp.push(tampung)
}
return spriral(input = tmp)
}
console.log(spriral(masalah2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment