How to resolve the algorithm Loops/Nested step by step in the ReScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Loops/Nested step by step in the ReScript programming language

Table of Contents

Problem Statement

Show a nested loop which searches a two-dimensional array filled with random numbers uniformly distributed over

[ 1 , … , 20 ]

{\displaystyle [1,\ldots ,20]}

. The loops iterate rows and columns of the array printing the elements until the value

20

{\displaystyle 20}

is met. Specifically, this task also shows how to break out of nested loops.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Loops/Nested step by step in the ReScript programming language

Source code in the rescript programming language

let m = []

for _ in 0 to 9 {
  let n = []
  for _ in 0 to 9 {
    let _ = Js.Array2.push(n, 1 + Js.Math.random_int(0, 20))
  }
  let _ = Js.Array2.push(m, n)
}

try {
  for i in 0 to 9 {
    for j in 0 to 9 {
      Js.log(m[i][j])
      if m[i][j] == 20 { raise(Exit) }
    }
  }
} catch {
| Exit => Js.log("stop")
}

  

You may also check:How to resolve the algorithm OpenGL step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Write to Windows event log step by step in the Clojure programming language
You may also check:How to resolve the algorithm Farey sequence step by step in the Lua programming language
You may also check:How to resolve the algorithm Summarize and say sequence step by step in the Go programming language
You may also check:How to resolve the algorithm Mouse position step by step in the Common Lisp programming language