Answer the question
In order to leave comments, you need to log in
How to solve a problem in Swift?
Good afternoon!
Help solve the problem
Create a loop with an interval from 1 to 10 in which a number in the range from 1 to 10 is randomly calculated. If the number 5 comes up, print a message on the console with the iteration number and stop the loop. Message example: "It took <...> iterations to roll the number 5".
To stop the loop, use the break statement. This statement is intended for early termination of the loop. In this case, all subsequent code in the loop body is ignored.
To determine a random number, use the random function: Int.random(1...10)
Answer the question
In order to leave comments, you need to log in
Try removing node_modules and reinstalling all packages:
sudo rm -rf node_modules
npm install
npm rebuild
var count = 0
for number in 1...10 {
count += 1
if number == 5 {
print("It took \(number) \(count) iterations to get a number \(number)")
break
}
}
I'm sorry, but this is hardly here.
For in
and if
this is the very base. And how to get a random number you wrote. And who wrote this text?
breack
Written in error. Run away from this teacher
var randomInt = Array(1...10)
// Number of iterations
var numberOfIterations = 0
for index in randomInt.shuffled() {
numberOfIterations += 1
if index == 5 {
print("It takes \(numberOfIterations) to roll the number 5 ) iterations")
break
}
}
Also now on this task, it turned out to be solved using the .shuffled() method. With Int.random(1...10) it doesn't work yet...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question