Revise using string interpolation.
This commit is contained in:
@@ -21,9 +21,9 @@ for n in range(0, 5) {
|
||||
result = fib(target);
|
||||
}
|
||||
|
||||
print("Finished. Run time = " + now.elapsed + " seconds.");
|
||||
print(`Finished. Run time = ${now.elapsed} seconds.`);
|
||||
|
||||
print("Fibonacci number #" + target + " = " + result);
|
||||
print(`Fibonacci number #${target} = ${result}`);
|
||||
|
||||
if result != 317_811 {
|
||||
print("The answer is WRONG! Should be 317,811!");
|
||||
|
@@ -4,7 +4,7 @@ let arr = [1, 2, 3, 4];
|
||||
|
||||
for a in arr {
|
||||
for b in [10, 20] {
|
||||
print(a + "," + b);
|
||||
print(`${a}, ${b}`);
|
||||
}
|
||||
|
||||
if a == 3 { break; }
|
||||
|
@@ -1,6 +1,6 @@
|
||||
const MAX = 1_000_000;
|
||||
|
||||
print("Iterating an array with " + MAX + " items...");
|
||||
print(`Iterating an array with ${MAX} items...`);
|
||||
|
||||
print("Ready... Go!");
|
||||
|
||||
@@ -18,5 +18,5 @@ for i in list {
|
||||
sum += i;
|
||||
}
|
||||
|
||||
print("Sum = " + sum);
|
||||
print("Finished. Run time = " + now.elapsed + " seconds.");
|
||||
print(`Sum = ${sum}`);
|
||||
print(`Finished. Run time = ${now.elapsed} seconds.`);
|
||||
|
@@ -68,4 +68,4 @@ for i in range(0, SIZE) {
|
||||
}
|
||||
*/
|
||||
|
||||
print("Finished. Run time = " + now.elapsed + " seconds.");
|
||||
print(`Finished. Run time = ${now.elapsed} seconds.`);
|
||||
|
@@ -7,7 +7,7 @@ let last_value = ();
|
||||
let obj1 = #{
|
||||
_data: 42, // data field
|
||||
get_data: || this._data, // property getter
|
||||
action: || print("Data=" + this._data), // method
|
||||
action: || print(`Data=${this._data}`), // method
|
||||
update: |x| { // property setter
|
||||
this._data = x;
|
||||
last_value = this._data; // capture 'last_value'
|
||||
@@ -38,4 +38,4 @@ if obj2.get_data() > 0 { // property access
|
||||
obj2.update(42); // call method
|
||||
}
|
||||
|
||||
print("Should be 84: " + last_value);
|
||||
print(`Should be 84: ${last_value}`);
|
||||
|
@@ -25,8 +25,8 @@ for p in range(2, MAX_NUMBER_TO_CHECK) {
|
||||
}
|
||||
}
|
||||
|
||||
print("Total " + total_primes_found + " primes <= " + MAX_NUMBER_TO_CHECK);
|
||||
print("Run time = " + now.elapsed + " seconds.");
|
||||
print(`Total ${total_primes_found} primes <= ${MAX_NUMBER_TO_CHECK}`);
|
||||
print(`Run time = ${now.elapsed} seconds.`);
|
||||
|
||||
if total_primes_found != 78_498 {
|
||||
print("The answer is WRONG! Should be 78,498!");
|
||||
|
@@ -10,4 +10,4 @@ while x > 0 {
|
||||
x -= 1;
|
||||
}
|
||||
|
||||
print("Finished. Run time = " + now.elapsed + " seconds.");
|
||||
print(`Finished. Run time = ${now.elapsed} seconds.`);
|
||||
|
@@ -11,17 +11,18 @@ print("foo" >= "bar"); // string comparison
|
||||
print("the answer is " + 42); // string building using non-string types
|
||||
|
||||
let s = "\u2764 hello, world! \U0001F603"; // string variable
|
||||
print("length=" + s.len); // should be 17
|
||||
print(`length=${s.len}`); // should be 17
|
||||
|
||||
s[s.len-3] = '?'; // change the string
|
||||
print("Question: " + s); // should print 'Question: hello, world?'
|
||||
print(`Question: ${s}`); // should print 'Question: hello, world?'
|
||||
|
||||
// Line continuation:
|
||||
let s = "This is a long \
|
||||
string constructed using \
|
||||
line continuation";
|
||||
|
||||
print("One string: " + s);
|
||||
// String interpolation
|
||||
print(`One string: ${s}`);
|
||||
|
||||
// Multi-line literal string:
|
||||
let s = `
|
||||
|
@@ -75,7 +75,7 @@ let keys = [];
|
||||
for animal in animals {
|
||||
for adjective in adjectives {
|
||||
for adverb in adverbs {
|
||||
keys.push(adverb + " " + adjective + " " + animal)
|
||||
keys.push(`${adverb} ${adjective} ${animal}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -99,5 +99,5 @@ for key in keys {
|
||||
map.remove(key);
|
||||
}
|
||||
|
||||
print("Sum = " + sum);
|
||||
print("Finished. Run time = " + now.elapsed + " seconds.");
|
||||
print(`Sum = ${sum}`);
|
||||
print(`Finished. Run time = ${now.elapsed} seconds.`);
|
||||
|
Reference in New Issue
Block a user