From c4b3ad7c7b8a4491f3f938aa97f7fa5be183f34a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 7 Jun 2021 14:52:45 +0800 Subject: [PATCH] Improve for-loop scripts. --- scripts/for1.rhai | 12 +++++++----- scripts/for2.rhai | 4 +++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/scripts/for1.rhai b/scripts/for1.rhai index 39ac7009..1e215da5 100644 --- a/scripts/for1.rhai +++ b/scripts/for1.rhai @@ -1,10 +1,12 @@ // This script runs for-loops -let arr = [1, 2, 3, 4]; +let arr = [1, true, 123.456, "hello", 3, 42]; -for a in arr { - for b in [10, 20] { - print(`${a}, ${b}`); +for (a, i) in arr { + for (b, j) in ['x', 42, (), 123, 99, 0.5] { + if b > 100 { continue; } + + print(`(${i}, ${j}) = (${a}, ${b})`); } if a == 3 { break; } @@ -12,6 +14,6 @@ for a in arr { //print(a); // <- if you uncomment this line, the script will fail to run // because 'a' is not defined here -for i in range(0, 5) { // runs through a range from 0 to 4 +for i in range(5, 0, -1) { // runs from 5 down to 1 print(i); } diff --git a/scripts/for2.rhai b/scripts/for2.rhai index c7b6b816..0f4cdce7 100644 --- a/scripts/for2.rhai +++ b/scripts/for2.rhai @@ -12,6 +12,8 @@ for i in range(0, MAX) { list.push(i); } +print(`Time = ${now.elapsed} seconds...`); + let sum = 0; for i in list { @@ -19,4 +21,4 @@ for i in list { } print(`Sum = ${sum}`); -print(`Finished. Run time = ${now.elapsed} seconds.`); +print(`Finished. Total run time = ${now.elapsed} seconds.`);