suit sederhana javascript
suit sederhana javascript, menggunakan localstorage untuk menyimpan skorwolep
javascript
a year ago
1.4 kB
9
Indexable
function suit (userChoice){
const sObj = '{"g":{"emoji":"✂️","label":"gunting"},"b":{"emoji":"🪨","label":"batu"},"k":{"emoji":"📄","label":"kertas"}}'
const suit = JSON.parse(sObj)
let validInput = false
for(x in suit){
if (x==userChoice){validInput=true;break}
}
// langsung akhiri program dengan tampilkan pesan input salah
if(!validInput){console.log(`input '${userChoice}' salah`); return}
//cek storage
if (localStorage.getItem('skorSuit') == null) localStorage.setItem('skorSuit', JSON.stringify([0,0]))
let tempArraySkor = JSON.parse(localStorage['skorSuit'])
let rand = Math.floor(Math.random()*Object.keys(suit).length)
let botChoice = Object.keys(suit)[rand]
if(userChoice==botChoice){
console.log(`seri! ${suit[userChoice].label} ${suit[userChoice].emoji}`)
}else{
console.log(`kamu ${suit[userChoice].label} ${suit[userChoice].emoji} vs ${suit[botChoice].emoji} ${suit[botChoice].label} bot`)
if('gkbg'.includes(userChoice+botChoice)){
console.log(`kamu menang!`)
tempArraySkor[0] += 1
}else{
console.log(`bot menang!`)
tempArraySkor[1] += 1
}
console.log(`skor: kamu ${tempArraySkor[0]}, bot ${tempArraySkor[1]}`)
localStorage['skorSuit'] = JSON.stringify(tempArraySkor)
}
}Editor is loading...
Leave a Comment