Issues regarding code for even or odd from 3 numbers


#1


I generated 3 random digits from 0 to 9 using math.random. for e.g: 2,5,8.
Now I have to write a code whether all the numbers are even or odd.
If all the numbers are even/odd, then i will add some value to other variable 'x', else i will add 0.
Can someone help me to write this code please. Thank you.


#2

what have you written so far?


#3

<html>
<head>
</head>
<body>

<h1>NUMBER GAME</h1>
<p id="points">You Have <span id="money">1000</span> INR.</p>
<button onclick="myFunction()">Start</button>
<p id="demo"></p>
<script>

function myFunction(){

if(document.getElementById('money') .innerHTML>=100){
var arr = []
while(arr.length < 3){
    var randomnumber = Math.floor(Math.random()*10)
    if(arr.indexOf(randomnumber) > -1) continue;
    arr[arr.length] = randomnumber;
}
document.getElementById('demo') .innerHTML=arr;
var x;
x= document.getElementById('money') .innerHTML;
console.log(x);
x-=100;
 document.getElementById('money') .innerHTML=x;

}
else{
	alert("you cannot play as you are out of cash");
}
}
</script>

</body>
</html>

#4

function myFunction(){

if(document.getElementById('money') .innerHTML>=100){
var arr = []
while(arr.length < 3){
var randomnumber = Math.floor(Math.random()*10)
if(arr.indexOf(randomnumber) > -1) continue;
arr[arr.length] = randomnumber;
}
document.getElementById('demo') .innerHTML=arr;
var x;
x= document.getElementById('money') .innerHTML;
console.log(x);
x-=100;
document.getElementById('money') .innerHTML=x;

}


#5

you should be able to finish this with a simple for loop:

allEven = true
for(var i in arr){
      if (i % 2){
           allEven = false
           break
     }
}
if(allEven){
     /* whatever you want to do when all numbers are even */
}

#6

Hi, stetim94, I'm unable to get any result. can you please add the code in my code and send it please. Thank You.


#7

i gave you some inspiration on how you could possible solve the problem, you will have to implement it. Do you have questions about the code

we use the remainder to see if the number is even


#8

I didn't mention any 'i' variable in my code. where should I mention 'i' variable in my code.


#9

oops, been programming a bit too much python lately:

allEven = true
for(var i = 0; i < arr.length; i++){
      if (i % 2){
           allEven = false
           break
     }
}
if(allEven){
     /* whatever you want to do when all numbers are even */
}

a for loop to loop over your array

i could be named anything you like


#10

Hi stetim94, can you tell me how to write a code if 3 numbers are consecutive or not from 3 random numbers. for e.g: if i get 2,3,4 or 2,4,3, it should take that as sequence regardless of its position. Can you help me Please. Thank You.


#11

this depends, is 3 5 7 sequential as well?


#12

no, only like 2,3,4 or 6,7,8 or 9,7,8 regardless of its position.


#14

I used this code, but it's not working.

function checkSequence(num) {
arrnew=arr.sort();
for(var i=0;i<arrnew.length-1;i++){
return arrnew[i+1]-arrnew[i]==1;
}
}


#15

i would personally be very lazy, i would just do:

arrnew=arr.sort();

if (arrnew[0] == arrnew[1] + 1 && arrnew[1] == arrnew[2] +1)
`
not very pretty, is it?


#16

I believe it should be arrnew=num.sort(); as num is the input list in the function.


#17

will it work if the numbers are in any order like 6,8,7?


#18

It will work for any order because of .sort(). Therefore, no matter if you do 2,4,3 or 4,3,2 or 3,2,4 you will end up with 2,3,4 after the .sort().


#19

But it's not working for me. Can you tell me what went wrong in this code please?

function checkSequence(num) {
arrnew=arr.sort();
for(var i=0;i<arrnew.length-1;i++){
return arrnew[i+1]-arrnew[i]==1;
}
}

if(arr.every(checkSequence)){
console.log("seqr");
x+=800;
document.getElementById('money') .innerHTML=x;
}


#20

you should use the condition i suggest:

if (arrnew[0] == arrnew[1] + 1 && arrnew[1] == arrnew[2] +1)

after sorting the list of course


#21

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.