File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ // Ejercicio 753: Verificar si al ordenar dos cadenas de caracteres éstas resultan similares.
2+
3+ // abc => abc
4+ // bca => abc
5+
6+ function cadenasSimilares ( texto1 , texto2 ) {
7+ if ( typeof texto1 === 'string' && typeof texto2 === 'string' ) {
8+ let caracteresTexto1 = texto1 . split ( '' ) ;
9+ let caracteresTexto2 = texto2 . split ( '' ) ;
10+
11+ caracteresTexto1 . sort ( ) ;
12+ caracteresTexto2 . sort ( ) ;
13+
14+ for ( let i = 0 ; i < Math . min ( caracteresTexto1 . length , caracteresTexto2 . length ) ; ++ i ) {
15+ if ( caracteresTexto1 [ i ] !== caracteresTexto2 [ i ] ) {
16+ return false ;
17+ }
18+ }
19+
20+ return true ;
21+ } else {
22+ throw TypeError ( 'Ambos parámetros deben ser de tipo cadena de caracteres.' ) ;
23+ }
24+ }
25+
26+ try {
27+ console . log ( cadenasSimilares ( 'abc' , 'cba' ) ) ; // true
28+ } catch ( e ) {
29+ console . log ( `Error: ${ e . message } ` ) ;
30+ }
31+
32+ console . log ( ) ;
33+
34+ try {
35+ console . log ( cadenasSimilares ( 'abc' , 'Cba' ) ) ; // false
36+ } catch ( e ) {
37+ console . log ( `Error: ${ e . message } ` ) ;
38+ }
39+
40+ console . log ( ) ;
41+
42+ try {
43+ console . log ( cadenasSimilares ( 'abc' , 101 ) ) ; // Error
44+ } catch ( e ) {
45+ console . log ( `Error: ${ e . message } ` ) ;
46+ }
You can’t perform that action at this time.
0 commit comments