Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
HamdiHarahap committed Aug 2, 2024
1 parent 23784cb commit 9bd490d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 53 deletions.
58 changes: 58 additions & 0 deletions assets/js/function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
function updateSaldo() {
jumlahSaldo.textContent = `${saldo}`
}

function spinImage(element) {
const randomIndex = Math.floor(Math.random() * images.length)
const image = document.createElement('img')
image.src = images[randomIndex]
element.innerHTML = ''
element.appendChild(image)
return randomIndex
}

function singleSpin() {
if (saldo < 1000) {
alert('Saldo tidak cukup untuk bermain!')
return
}

saldo -= 1000
updateSaldo()

const firstIndex = spinImage(firstImage)
const secondIndex = spinImage(secondImage)
const thirdIndex = spinImage(thirdImage)

setTimeout(function () {
if (firstIndex === secondIndex && secondIndex === thirdIndex) {
alert('Selamat, kamu menang!')
saldo += 10000
updateSaldo()
}
}, 300)
}

function spinMultipleTimes(count) {
let spinCount = 0

const interval = setInterval(function () {
if (saldo < 1000) {
clearInterval(interval)
alert('Saldo tidak cukup untuk bermain!')
return
}

singleSpin()

spinCount++
if (spinCount >= count) {
clearInterval(interval)
}
}, 1200)

const stopSpin = document.querySelector('.stop')
stopSpin.addEventListener('click', function () {
clearInterval(interval)
})
}
91 changes: 38 additions & 53 deletions assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const images = [
const deposit = document.querySelector('.saldo')
let saldo = 0

function updateSaldo() {
jumlahSaldo.textContent = `${saldo}`
}

deposit.addEventListener('click', function () {
const isiSaldo = prompt('Masukkan jumlah saldo!')
saldo += parseInt(isiSaldo)
Expand All @@ -19,10 +23,6 @@ deposit.addEventListener('click', function () {

const jumlahSaldo = document.querySelector('.data-saldo')

function updateSaldo() {
jumlahSaldo.textContent = `${saldo}`
}

const firstImage = document.querySelector('.first')
const secondImage = document.querySelector('.second')
const thirdImage = document.querySelector('.third')
Expand All @@ -38,11 +38,7 @@ function spinImage(element) {

const spin = document.querySelector('.spin')

spin.addEventListener('click', function () {
if (saldo < 1000) {
alert('Saldo tidak cukup untuk bermain!')
}

function singleSpin() {
saldo -= 1000
updateSaldo()

Expand All @@ -57,13 +53,21 @@ spin.addEventListener('click', function () {
updateSaldo()
}
}, 300)
}

spin.addEventListener('click', function () {
if (saldo < 1000) {
alert('Saldo tidak cukup untuk bermain!')
return
}
singleSpin()
})

const autoSpin = document.querySelector('.auto')

autoSpin.addEventListener('click', function () {
if (saldo < 1000) {
alert('Saldo tidak cukup untuk bermain!')
return
}

const interval = setInterval(function () {
Expand All @@ -72,37 +76,16 @@ autoSpin.addEventListener('click', function () {
alert('Saldo tidak cukup untuk bermain!')
return
}

saldo -= 1000
updateSaldo()

const firstIndex = spinImage(firstImage)
const secondIndex = spinImage(secondImage)
const thirdIndex = spinImage(thirdImage)

setTimeout(function () {
if (
saldo >= 0 &&
firstIndex === secondIndex &&
secondIndex === thirdIndex
) {
alert('Selamat, kamu menang!')
saldo += 10000
updateSaldo()
}
}, 300)
singleSpin()
}, 1200)

const stopSpin = document.querySelector('.stop')

stopSpin.addEventListener('click', function () {
clearInterval(interval)
})
})

const tenSpin = document.querySelector('.tenSpin')

tenSpin.addEventListener('click', function () {
function spinMultipleTimes(count) {
let spinCount = 0

const interval = setInterval(function () {
Expand All @@ -112,34 +95,36 @@ tenSpin.addEventListener('click', function () {
return
}

saldo -= 1000
updateSaldo()

const firstIndex = spinImage(firstImage)
const secondIndex = spinImage(secondImage)
const thirdIndex = spinImage(thirdImage)

setTimeout(function () {
if (
saldo >= 0 &&
firstIndex === secondIndex &&
secondIndex === thirdIndex
) {
alert('Selamat, kamu menang!')
saldo += 10000
updateSaldo()
}
}, 300)
singleSpin()

spinCount++
if (spinCount >= 10) {
if (spinCount >= count) {
clearInterval(interval)
}
}, 1200)

const stopSpin = document.querySelector('.stop')

stopSpin.addEventListener('click', function () {
clearInterval(interval)
})
}

const tenSpin = document.querySelector('.tenSpin')
tenSpin.addEventListener('click', function () {
spinMultipleTimes(10)
})

const twentySpin = document.querySelector('.twentySpin')
twentySpin.addEventListener('click', function () {
spinMultipleTimes(20)
})

const fiftySpin = document.querySelector('.fiftySpin')
fiftySpin.addEventListener('click', function () {
spinMultipleTimes(50)
})

const hundredSpin = document.querySelector('.hundredSpin')
hundredSpin.addEventListener('click', function () {
spinMultipleTimes(100)
})
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ <h3>Saldo anda sekarang sebesar : <span class="data-saldo">0</span></h3>
</section>

<script src="assets/js/main.js"></script>
<script src="assets/js/function.js"></script>
</body>
</html>

0 comments on commit 9bd490d

Please sign in to comment.