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 16/* 17 * @tc.name:findIndex 18 * @tc.desc:test array.findIndex 19 * @tc.type: FUNC 20 * @tc.require: 21 */ 22 23const array1 = [5, 12, 8, 130, 44]; 24const isLargeNumber = (element) => element > 13; 25print(array1.findIndex(isLargeNumber)); 26 27const arrayLike = { 28 length: 3, 29 0: 2, 30 1: 7.3, 31 2: 4, 32}; 33print(Array.prototype.findIndex.call(arrayLike, (x) => !Number.isInteger(x))); 34print([1, , 3].findIndex((x) => x === undefined)); 35function isPrime(element) { 36 if (element % 2 === 0 || element < 2) { 37 return false; 38 } 39 for (let factor = 3; factor <= Math.sqrt(element); factor += 2) { 40 if (element % factor === 0) { 41 return false; 42 } 43 } 44 return true; 45} 46print([4, 6, 8, 9, 12].findIndex(isPrime)); 47print([4, 6, 7, 9, 12].findIndex(isPrime)); 48 49const words = ["spray", "limit", "limits"]; 50const deleteWords = words.findIndex((word, index, arr) => { 51 arr.length=2 52 return word == "limits" 53}); 54print(deleteWords); 55print(words.length); 56 57var array = ["first", "second"]; 58Object.defineProperty(array, 0, { 59 get: function() { 60 array.length = 0; 61 return "first"; 62 } 63}); 64const hasFirst = (element) => element == "first"; 65print(array.findIndex(hasFirst)); 66