A
A
Anto2021-04-25 19:03:55
Node.js
Anto, 2021-04-25 19:03:55

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

4 answer(s)
I
Ivan Sharapenkov, 2018-08-25
@sM0kfyz

Try removing node_modules and reinstalling all packages:
sudo rm -rf node_modules
npm install
npm rebuild

E
Eldar Tengizov, 2021-04-28
@ZIROKUL

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
}
}

A
Alexey Golovin, 2021-04-27
@dadduUrsa

I'm sorry, but this is hardly here.
For inand ifthis is the very base. And how to get a random number you wrote. And who wrote this text?
breackWritten in error. Run away from this teacher

P
PaIZ, 2021-09-27
@PaIZ

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 question

Ask a Question

731 491 924 answers to any question