Last active
March 22, 2020 02:24
-
-
Save krowter/cb492ea2f21fac629d92147f681cbd58 to your computer and use it in GitHub Desktop.
Solution 1 for Impact Byte Coding Challenge #12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//array input | |
let input = [9, -1, 8, 3, -2, 9, 4, -3, 9] | |
//bilangan yang ingin kita pindahkan ke belakang | |
const numberToMove = 9 | |
//gunakan loop forEach untuk mengecek tiap element | |
input.forEach((number, index) => { | |
//kalau element sama dengan 9 | |
if(number === numberToMove) { | |
//hapus dia dari array | |
input.splice(index, 1) | |
//lalu tambahkan dia ke bagian belakang | |
input.push(number) | |
} | |
}) | |
console.log(input) //output: [-1, 8, 3, -2, 4, -3, 9, 9, 9] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment