diff --git a/README.md b/README.md index 16397637c16d79fa23575bb1ec055e30ddf9cdca..ab966948f1fa1a37688d4eaec0780111ea87f918 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ ## 约束与限制 -1. 本示例仅支持标准系统上运行,支持设备:华为手机。 +1. 本示例仅支持预览器运行。 2. HarmonyOS系统:HarmonyOS 6.0.0 Release及以上。 diff --git a/entry/src/main/ets/pages/BasicPage.ets b/entry/src/main/ets/pages/BasicPage.ets index 3da3d043ef565cc7212fc2bbb78f6b87ba1b06b0..7a5ea8157f0a6bf821bf049ae1c55bcf22f2f08b 100644 --- a/entry/src/main/ets/pages/BasicPage.ets +++ b/entry/src/main/ets/pages/BasicPage.ets @@ -74,13 +74,29 @@ function referenceTypes() { let o1: Object = 'Alice'; let o2: Object = ['a', 'b']; let o3: Object = 1; - console.log(`o1: ${o1.toString()}`); - console.log(`o2: ${o2.toString()}`); - console.log(`o3: ${o3.toString()}`); + console.log(`o1: ${o1}`); + console.log(`o2: ${o2}`); + console.log(`o3: ${o3}`); + + let strObject: String = new String('ArkTS'); + let obj: Object = 'ArkTS'; + let str: string = 'ArkTS'; + console.log(`${str.length}`); + console.log(`${str.indexOf('T')}`); + console.log(`strObject: ${strObject}`); + console.log(`obj: ${obj}`); // Array let students: string[] = ['XiaoMing', 'XiaoZhang', 'XiaoWang', 'XiaoLi']; console.log(students[0]); // Get element at index 0. + students.push('XiaoHong'); + students.pop(); + + let arr1: number[] = [1, 2]; + let arr2: number[] = arr1; + arr2.push(3); + console.log(`arr1: ${arr1}`); + console.log(`arr2: ${arr2}`); } referenceTypes(); @@ -105,7 +121,7 @@ function typeSystem() { // Type Aliases. type Matrix = number[][]; // Define alias Matrix for a 2D array. let arr: Matrix = [[1, 2, 3]]; - console.log(`arr: ${arr.toString()}`); + console.log(`arr: ${arr}`); type NullableObject = Object | null; // Define alias NullableObject for a nullable object. let obj: NullableObject = null; @@ -217,7 +233,7 @@ function nullSafety() { let x: number | null = null; x = 1; // ok. x = null; // ok. - if (x != null) { + if (x !== null) { // ... } } @@ -225,7 +241,7 @@ function nullSafety() { nullSafety(); function nullSafetyWithIf(name: string | null) { - if (name != null) { + if (name !== null) { // ... } } @@ -282,11 +298,19 @@ function showSwitchStatements() { let day = 6; switch (day) { case 1: + console.log('Monday'); + break; case 2: + console.log('Tuesday'); + break; case 3: + console.log('Wednesday'); + break; case 4: + console.log('Thursday'); + break; case 5: - console.log('Weekday'); + console.log('Friday'); break; case 6: case 7: @@ -443,9 +467,6 @@ function makeMilkTeaWithoutReturnType(flavor: string, sweetness: string) { } makeMilkTeaWithoutReturnType('strawberry', 'half sugar'); -// Function scope. -let outerVar = 'I am outer'; -console.log(`out func: ${outerVar}`); function hi1() { console.info('hi1'); @@ -458,11 +479,15 @@ function hi2(): void { hi1(); hi2(); +// Function scope. +let outerVar = 'I am outer'; + function func() { let outerVar = 'I am inside'; console.log(outerVar); } +console.log(outerVar); func(); @Entry diff --git a/entry/src/main/ets/pages/BubbleSortPage.ets b/entry/src/main/ets/pages/BubbleSortPage.ets index d2f57694a5347607c31394710d24295506122f1e..adbbd6ff287bc169715d4c234970306e7c32c019 100644 --- a/entry/src/main/ets/pages/BubbleSortPage.ets +++ b/entry/src/main/ets/pages/BubbleSortPage.ets @@ -28,7 +28,7 @@ function BubbleSort(arr: number[]): number[] { } let result = BubbleSort([85, 92, 78, 95, 88, 60, 72]); -console.log(result.toString()); +console.log(`${result}`); @Entry @Component diff --git a/entry/src/main/ets/pages/DaffodilsNumberPage.ets b/entry/src/main/ets/pages/DaffodilsNumberPage.ets index cd827030892053b476a5eff27b3a322934604b90..24f35970f53b6528c7d37f1a2e0659fd8047e6c6 100644 --- a/entry/src/main/ets/pages/DaffodilsNumberPage.ets +++ b/entry/src/main/ets/pages/DaffodilsNumberPage.ets @@ -17,7 +17,7 @@ function DaffodilsNumber() { let result: number[] = []; for (let i = 100; i < 1000; i++) { let unitsDigit: number = i % 10; - let tenthsDigit: number = Math.floor(i / 10) - Math.floor(i / 100) * 10; + let tenthsDigit: number = Math.floor(i / 10) % 10; let hundredthsDigit: number = Math.floor(i / 100); if (i === unitsDigit * unitsDigit * unitsDigit + tenthsDigit * tenthsDigit * tenthsDigit + hundredthsDigit * hundredthsDigit * hundredthsDigit) { @@ -28,7 +28,7 @@ function DaffodilsNumber() { } let daffodilsNumberResult = DaffodilsNumber(); -console.log(daffodilsNumberResult.toString()); +console.log(`${daffodilsNumberResult}`); @Entry @Component diff --git a/entry/src/main/ets/pages/MultiplicationTablePage.ets b/entry/src/main/ets/pages/MultiplicationTablePage.ets index bb171159c3bc70faf135c17a1a5dd97bf95de4fb..abc64a193e0b9dedd26e70fc42e08bcb399aac59 100644 --- a/entry/src/main/ets/pages/MultiplicationTablePage.ets +++ b/entry/src/main/ets/pages/MultiplicationTablePage.ets @@ -14,8 +14,8 @@ */ // Print 9x9 multiplication table. function MultiplicationTable() { + let result = ''; for (let i = 1; i <= 9; i++) { - let result = ''; for (let j = 1; j <= i; j++) { let temp: string = j + ' * ' + i + ' = ' + i * j; result += temp; diff --git a/oh-package.json5 b/oh-package.json5 index c72aa05d549507e5ea6ce0bc0a8080c450508c7d..96463c34a1d8fb87fe2b01d9e47f510104e035cc 100644 --- a/oh-package.json5 +++ b/oh-package.json5 @@ -3,8 +3,5 @@ "description": "Please describe the basic information.", "dependencies": { }, - "devDependencies": { - "@ohos/hypium": "1.0.24", - "@ohos/hamock": "1.0.0" - } + "devDependencies": {} }