Hi I have small problem with this challenge.
My solution is:
let givenNumber = 10
var cube1 = 1
var cube2 = 1
var count = 0
let allCombinations = 36
while (cube1 != 6 || cube2 != 6) {
if cube1 + cube2 == givenNumber {
count += 1
}
if cube1 != 6 {
cube1 += 1
} else {
cube1 = 1
cube2 += 1
}
}
let probability = Double(count) / Double(allCombinations)
print(probability)
It works but I don’t understand why becouse I belive it should be:
while (cube1 != 6 && cube2 != 6)
I want that this loop is repeated as long as cube1 and cube2 are not equal to 6 when both cube1 and cube2 are equal to 6 it should stop. But it stops when only cube1 is equal to 6. I thought it will do this with || :/.
Becouse I didn’t understand it I did this different:
let givenNumber = 10
var cube1 = 1
var cube2 = 1
var count = 0
let allCombinations = 36
while true {
if cube1 + cube2 == givenNumber {
count += 1
}
if cube1 != 6 {
cube1 += 1
} else {
cube1 = 1
cube2 += 1
}
if cube1 == 6 && cube2 == 6 {
break
}
}
let probability = Double(count) / Double(allCombinations)
print(probability)
And it is ok, but maybe you can explain me why I’m wrong with this “while && / ||”?
Thanks!