• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16const array1 = ['one', 'two', 'three'];
17print('array1:', array1);
18// Expected output: "array1:" Array ["one", "two", "three"]
19
20const reversed1 = array1.reverse();
21print('reversed1:', reversed1);
22// Expected output: "reversed1:" Array ["three", "two", "one"]
23
24// Careful: reverse is destructive -- it changes the original array.
25print('array1:', array1);
26// Expected output: "array1:" Array ["three", "two", "one"]
27
28print([1, , 3].reverse()); // [3, empty, 1]
29print([1, , 3, 4].reverse()); // [4, 3, empty, 1]
30
31const numbers = [3, 2, 4, 1, 5];
32const reverted = [...numbers].reverse();
33reverted[0] = 5;
34print(numbers[0]); // 3
35
36const numbers1 = [3, 2, 4, 1, 5];
37const reversed = numbers1.reverse();
38reversed[0] = 5;
39print(numbers1[0]); // 5
40
41var array2 = new Uint8Array([1, 2, 3, 4]);
42Object.defineProperty(array2, "length", { value: 2 });
43Array.prototype.reverse.call(array2);
44print(array2)
45try {
46    var array3 = new Uint8Array([1, 2, 3, 4]);
47    Object.defineProperty(array3, "length", { value: 5 });
48    Array.prototype.reverse.call(array3);
49    print(array3)
50} catch (error) {
51    print(error.name)
52}
53