java script problem ! it not + and put it toggether !

hello
37 Replies
NIMA
NIMAOP8mo ago
hello '''js let text = ''; let neshon = ''; ar = [1,2,3,4,5,6] win=[1] loss=[1] function submiter1(){
let getter = document.getElementById('n1').value = 1; let random = ar[(Math.floor(Math.random() * ar.length))];
if(getter == random ){ for(let i =0; i<=win; i++){ neshon += win; }
text = random; document.getElementById('brande').innerHTML = 'brande shodid '; document.getElementById('shower').innerHTML = 'adad bod'+' ' + text; document.getElementById('win').innerHTML = neshon;
}else{ loss + 1 document.getElementById('brande').innerHTML = 'shoma bakhtid '; document.getElementById('shower').innerHTML = 'adad bod'+' ' + random; document.getElementById('loss').innerHTML = loss; } } "'' why in for it not give +1 to win ? and it just show the 1 close it it ? like 1+1 = 2 but it show 11 i was know problem but i forget
Jochem
Jochem8mo ago
+ concatenates strings (sticks them together), and when one half of the arguments around + is a string, and the other is a number, it converts the number into a string and puts them together
NIMA
NIMAOP8mo ago
witch one is string?
Jochem
Jochem8mo ago
neshon is a string let neshon = '';
NIMA
NIMAOP8mo ago
hmm
Jochem
Jochem8mo ago
(also, the code blocks use backticks, not quotes. It's left of the 1 key on a qwerty keyboard) and if you type, say ```js, it will do syntaxt highlighting which makes code a lot easier to read
NIMA
NIMAOP8mo ago
i do but not work
let text = '';
let neshon = '';
ar = [1,2,3,4,5,6]



win=[1]
loss=[1]

function submiter1(){

let getter = document.getElementById('n1').value = 1;
let random = ar[(Math.floor(Math.random() * ar.length))];

if(getter == random ){
for(let i =0; i<=win; i++){
neshon += win;
}




text = random;
document.getElementById('brande').innerHTML = 'brande shodid ';
document.getElementById('shower').innerHTML = 'adad bod'+' ' + text;
document.getElementById('win').innerHTML = neshon;

}else{
loss + 1
document.getElementById('brande').innerHTML = 'shoma bakhtid ';
document.getElementById('shower').innerHTML = 'adad bod'+' ' + random;
document.getElementById('loss').innerHTML = loss;
}

}
let text = '';
let neshon = '';
ar = [1,2,3,4,5,6]



win=[1]
loss=[1]

function submiter1(){

let getter = document.getElementById('n1').value = 1;
let random = ar[(Math.floor(Math.random() * ar.length))];

if(getter == random ){
for(let i =0; i<=win; i++){
neshon += win;
}




text = random;
document.getElementById('brande').innerHTML = 'brande shodid ';
document.getElementById('shower').innerHTML = 'adad bod'+' ' + text;
document.getElementById('win').innerHTML = neshon;

}else{
loss + 1
document.getElementById('brande').innerHTML = 'shoma bakhtid ';
document.getElementById('shower').innerHTML = 'adad bod'+' ' + random;
document.getElementById('loss').innerHTML = loss;
}

}
oh wrong key "'' so i should re place neshon?
Jochem
Jochem8mo ago
if it's only used to store a number, you can initialize it as 0 instead of ''
NIMA
NIMAOP8mo ago
let neshon = 0; u mean like this?
Jochem
Jochem8mo ago
yeah, that would probably be enough
NIMA
NIMAOP8mo ago
no i try but not work still not + only show each other near each other Win : 01111111
Jochem
Jochem8mo ago
try neshon = parseInt(neshon) + parseInt(win);
NIMA
NIMAOP8mo ago
hmm i think when im using this it not show 1 anymore let getter = document.getElementById('n1').value = 1; so it never give me win
Jochem
Jochem8mo ago
maybe make a codepen then, so that people can run the code themselves. I've got a meeting to go to now
NIMA
NIMAOP8mo ago
Aright i will try thath
13eck
13eck8mo ago
This is returning a string, which is causing the issue. You want to use valueAsNumber
NIMA
NIMAOP8mo ago
, let getter = document.getElementById('n1').valueAsNumber = 1; but why it still not + and show 1 near each other Win : 1111
13eck
13eck8mo ago
Are you trying to set or get the value? You named the variable getter but you’re using = with is setting the value
NIMA
NIMAOP8mo ago
im making a game if the getter be az same == az random number it show u win and 1+ to the win leaderboard and all code fine but it not +1 to win and show it near each other like this Win : 1111 but i want to show like win 1 win 2 win 3 do u know the problem? dont look at the names cuse first i was getting number from input but i chenge it to stactic value so i make 6 button with 6 number 1.2.3.4.5.6
13eck
13eck8mo ago
Instead of saying “post no attention to the name” you should name your variables better. Is confusing to anyone looking at your code (like those trying to help)
NIMA
NIMAOP8mo ago
yes i know i was gonna re name all stuff after i fix this bug idk why this is happening do u know what should i do for the bug?
13eck
13eck8mo ago
Your win variable is an array. You’re trying to add an array and a number…which can’t be done. So JS is making everything a string. Change win to a number and it should work
NIMA
NIMAOP8mo ago
i chenge it to what?
13eck
13eck8mo ago
A number
NIMA
NIMAOP8mo ago
it is 1 win=[1]
13eck
13eck8mo ago
That’s an array, not a number
NIMA
NIMAOP8mo ago
it work let win=1; there is no way i can use it in aray?
13eck
13eck8mo ago
Of course there’s a way. But you’ll have to be more specific about your goal. What exactly sure you trying to accomplish?
NIMA
NIMAOP8mo ago
i think for this part i dont need araye right ? cuse all button put number in 1 so it only need 1
13eck
13eck8mo ago
That depends on what you want to do. An array is used when you want to store and use multiple data points. Do you need more than one number? If so, why?
NIMA
NIMAOP8mo ago
nop i only need 1 to deposite score for win and another one to deposite score for loss
13eck
13eck8mo ago
Then there’s no need for an array. You’re good to go 👍
NIMA
NIMAOP8mo ago
tnx for help it work fine now just one thing let win=1; when i put win to 0 its not adding it 1 more and when its 1 score start from 2 not 1 how should i fix this? ' ' and 0 not fix the problem
13eck
13eck8mo ago
Share your correct code
NIMA
NIMAOP8mo ago
let text = '';

let neshon = 1;
let neshonloss = 1;

ar = [1,2,3,4,5,6]



let win=1;
let loss =1;

function submiter1(){


let getter = document.getElementById('n1').valueAsNumber = 1;
let random = ar[(Math.floor(Math.random() * ar.length))];

if(getter == random ){
for(let i =0; i<win; i++){

neshon += win;
}






text = random;
document.getElementById('brande').innerHTML = 'brande shodid ';
document.getElementById('shower').innerHTML = 'adad bod'+' ' + text;
document.getElementById('win').innerHTML = neshon;

}else{
for(let i=0;i<win; i++){
neshonloss += loss;

}
document.getElementById('brande').innerHTML = 'shoma bakhtid ';
document.getElementById('shower').innerHTML = 'adad bod'+' ' + random;
document.getElementById('loss').innerHTML = neshonloss;
}

}
let text = '';

let neshon = 1;
let neshonloss = 1;

ar = [1,2,3,4,5,6]



let win=1;
let loss =1;

function submiter1(){


let getter = document.getElementById('n1').valueAsNumber = 1;
let random = ar[(Math.floor(Math.random() * ar.length))];

if(getter == random ){
for(let i =0; i<win; i++){

neshon += win;
}






text = random;
document.getElementById('brande').innerHTML = 'brande shodid ';
document.getElementById('shower').innerHTML = 'adad bod'+' ' + text;
document.getElementById('win').innerHTML = neshon;

}else{
for(let i=0;i<win; i++){
neshonloss += loss;

}
document.getElementById('brande').innerHTML = 'shoma bakhtid ';
document.getElementById('shower').innerHTML = 'adad bod'+' ' + random;
document.getElementById('loss').innerHTML = neshonloss;
}

}
13eck
13eck8mo ago
Sorry, been at work all day >_< Can you step me through your function here, let me know what you want it to do? Reading it as-is I can't make out what exactly you're trying to accomplish
NIMA
NIMAOP8mo ago
hello sorry i dont cheak channel im getting home in few hours and im gonna uplode it in code open
Want results from more Discord servers?
Add your server