|
7 | 7 |
|
8 | 8 | c) A function that determines the number of elements k that precede the first negative element. If all elements are non-negative, k is equal to the number of elements in the array.
|
9 | 9 |
|
| 10 | +* |
10 | 11 | */
|
11 | 12 |
|
| 13 | +import{formRandomArray}from'./task-formArray'; |
| 14 | + |
| 15 | +functionchangeSignA(array:number[]):void{ |
| 16 | +array.forEach((element,index)=>{ |
| 17 | +if(index%2===0)array[index]=-element; |
| 18 | +}); |
| 19 | +} |
| 20 | + |
| 21 | +functiondetermineEvenElements(array:number[]):number{ |
| 22 | +letcounter=0; |
| 23 | +array.forEach((element,index)=>{ |
| 24 | +if(index%2!==0&&element%2===0)counter+=1; |
| 25 | +}); |
| 26 | +returncounter; |
| 27 | +} |
| 28 | + |
| 29 | +functionelementsReturned(array:number[]):number{ |
| 30 | +letcounter=0; |
| 31 | +for(leti=0;i<array.length;i++){ |
| 32 | +if(array[i]<0)break; |
| 33 | +elsecounter+=1; |
| 34 | +} |
| 35 | +returncounter; |
| 36 | +} |
| 37 | + |
| 38 | +functionelementsReturned2(array:number[]):number{ |
| 39 | +for(leti=0;i<array.length;i++){ |
| 40 | +if(array[i]<0)returni; |
| 41 | +} |
| 42 | +returnarray.length; |
| 43 | +} |
| 44 | + |
| 45 | +functionelementsReturned3(array:number[]):number{ |
| 46 | +leti=0; |
| 47 | +while(array[i]>0)i+=1; |
| 48 | +returni; |
| 49 | +} |
| 50 | + |
| 51 | +functionrun():void{ |
| 52 | +console.log(' Run 08 '); |
| 53 | +constarray:number[]=formRandomArray(15,-30,90); |
| 54 | +console.log(' array: ',array); |
| 55 | +// changeSignA(array); |
| 56 | +// console.log(' array: ', array); |
| 57 | +console.log( |
| 58 | +'Count even elements odd indencies ', |
| 59 | +determineEvenElements(array), |
| 60 | +); |
| 61 | +console.log('Elements returned: ',elementsReturned(array)); |
| 62 | +console.log('Elements returned: ',elementsReturned2(array)); |
| 63 | +console.log('Elements returned: ',elementsReturned3(array)); |
| 64 | +} |
| 65 | + |
| 66 | +exportdefaultrun; |
| 67 | + |
12 | 68 | /***
|
13 | 69 | *
|
14 | 70 |
|
|