@@ -11,28 +11,27 @@ let javaScript = {
1111} ;
1212
1313function countLetterA ( str ) {
14- let counter = 0 ;
1514 let arrStr = str . split ( '' ) ;
16- arrStr . forEach ( function ( elem , index , arr ) {
17- if ( elem === 'a' ) {
18- counter ++ ;
15+ let countLetterA2 = arrStr . reduce ( function ( newElem , elem ) {
16+ if ( elem === 'a' ) {
17+ newElem ++ ;
1918 } ;
20- return counter ;
21- } ) ;
22- return counter ;
19+ return newElem ;
20+ } , 0 ) ;
21+ return countLetterA2 ;
2322} ;
24-
2523console . log ( 'Task1 ' , countLetterA ( randomString ) ) ;
2624console . log ( 'Task1 ' , countLetterA ( javaScript . html + user . name ) ) ;
2725
2826
2927
28+
3029//Task2
3130
3231function reverseEachWord ( str ) {
3332 let arrStr = str . split ( ' ' ) ;
3433 let newArrStr = [ ] ;
35- arrStr . forEach ( function ( elem , index , arr ) {
34+ arrStr . forEach ( function ( elem ) {
3635 let arrElem = elem . split ( '' ) ;
3736 let reversElemInArr = arrElem . reverse ( ) ;
3837 let reversElemInString = reversElemInArr . join ( '' ) ;
@@ -48,16 +47,16 @@ console.log('Task2 ', reverseEachWord('The Document Object Model (DOM) is a prog
4847
4948//Task3
5049
51- function reverseEachWord2 ( str , boolean ) {
50+ function reverseEachWord2 ( str , shouldReverse ) {
5251 let arrStr = str . split ( ' ' ) ;
5352 let newArrStr = [ ] ;
54- arrStr . forEach ( function ( elem , index , arr ) {
53+ arrStr . forEach ( function ( elem ) {
5554 let arrElem = elem . split ( '' ) ;
5655 let reversElemInArr = arrElem . reverse ( ) ;
5756 let reversElemInString = reversElemInArr . join ( '' ) ;
5857 newArrStr . push ( reversElemInString ) ;
5958 } ) ;
60- if ( boolean === true ) {
59+ if ( shouldReverse === true ) {
6160 newArrStr . reverse ( ) ;
6261 } ;
6362 return newArrStr . join ( ' ' ) ;
@@ -72,24 +71,15 @@ console.log('Task3 ', reverseEachWord2('Hi my Name is', true));
7271
7372//Task4
7473
75- function wordCounter ( sentence ) {
74+ function wordCounter2 ( sentence ) {
7675 let arrayOfSentences = sentence . split ( ' ' ) ;
77- let elements = arrayOfSentences . reduce ( function ( newValue , elem , index , arr ) {
78- let initialElem = elem ;
79- let counter = 0 ;
80- arrayOfSentences . forEach ( function ( elem , index , arr ) {
81- if ( initialElem === elem ) {
82- counter ++ ;
83- } ;
84- } ) ;
85- newValue [ initialElem ] = counter ;
76+ let elements = arrayOfSentences . reduce ( function ( newValue , elem ) {
77+ newValue [ elem ] = newValue [ elem ] ? newValue [ elem ] + 1 : 1 ;
8678 return newValue ;
8779 } , { } ) ;
8880 return elements ;
8981} ;
90-
91- console . log ( 'Task4 ' , wordCounter ( 'Both Java and Java Script is programming and programming OOPBased Language' ) ) ;
92-
82+ console . log ( 'Task4 ' , wordCounter2 ( 'Both Java and Java Script is programming and programming OOPBased Language' ) ) ;
9383
9484
9585
@@ -186,5 +176,41 @@ function createHashTags(arr) {
186176console . log ( 'Task5 ' , createHashTags ( listOfCompanys ) ) ;
187177
188178//@ SUPER1
179+ let notUniqArray = [ 1 , 1 , 2 , 2 , 2 , 5 , 10 , 25 , 30 , 5 , 1 , 0 , 22 , 3 , 10 , 3 ] ;
180+
181+ function uniqueElements ( arr ) {
182+ let uniqueElements = [ ] ;
183+ nextInput:
184+ for ( let i = 0 ; i < arr . length ; i ++ ) {
185+ let initialElem = arr [ i ] ;
186+ for ( let k = 0 ; k < uniqueElements . length ; k ++ ) {
187+ if ( uniqueElements [ k ] == initialElem ) {
188+ continue nextInput;
189+ } ;
190+ } ;
191+ uniqueElements . push ( initialElem ) ;
192+ } ;
193+ return uniqueElements ;
194+ } ;
195+
196+ console . log ( 'SUPER1 ' , uniqueElements ( notUniqArray ) ) ;
197+
198+
199+ //@ SUPER2
200+ let array = [ 1 , - 1 , 0 , 15 , - 59 , 7 ] ;
201+ function filter ( callback , arr ) {
202+ let newArr = [ ] ;
203+ for ( let i = 0 ; i < arr . length ; i ++ ) {
204+ if ( ! callback ( arr [ i ] ) ) {
205+ continue ;
206+ } ;
207+ newArr . push ( arr [ i ] ) ;
208+ } ;
209+ return newArr ;
210+ } ;
211+
212+ let res = function exampleCallbackFunction ( value ) {
213+ return value < 0 ;
214+ } ;
189215
190- //@ SUPER2
216+ console . log ( ' SUPER2 ' , filter ( res , array ) ) ;
0 commit comments