{"id":724,"date":"2024-07-30T20:31:52","date_gmt":"2024-07-30T18:31:52","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=724"},"modified":"2024-07-30T20:31:52","modified_gmt":"2024-07-30T18:31:52","slug":"002-ejercicios-de-logica-de-programacion","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/07-ejercicios-de-logica-de-programacion\/002-ejercicios-de-logica-de-programacion\/","title":{"rendered":"002. Ejercicios de l\u00f3gica de programaci\u00f3n"},"content":{"rendered":"\n<p>En estos primeros ejercicios estamos manejando <strong>cadenas de texto<\/strong> y como podemos manipularlas. Comencemos a resolver los ejercicios:<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">1. Invertir palabras<\/h2>\n\n\n\n<p>Vamos a programar una funci\u00f3n que invierta las palabras de una cadena de texto.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; const invertirCadena = (cadena = \"\") =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; !cadena\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? console.log(\"No ingresaste una cadena\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : console.log(cadena.split(\"\").reverse().join(\"\"));\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; invertirCadena(\"Hola Mundo\");<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. N\u00famero de veces que se repite una palabra<\/h2>\n\n\n\n<p>Vamos a programar una funci\u00f3n que cuente el n\u00famero de veces que se repite una palabra.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; const textoEnCadena = (cadena = \"\", texto = \"\") =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (!cadena) return console.log(\"No ingresaste una cadena\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (!texto) return console.log(\"No ingresaste un texto a evaluar\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; let i = 0,\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contador = 0;\n\n&nbsp; &nbsp; &nbsp; &nbsp; while (i !== -1) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = cadena.indexOf(texto, i);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i !== -1) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contador++;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; return console.log(`La palabra \"${texto}\" se repite ${contador} veces`);\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; textoEnCadena(\"Hola Mundo Mundo, Adi\u00f3s Mundo\", \"Mundo\");<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. Pal\u00edndromo<\/h2>\n\n\n\n<p>Vamos a programar una funci\u00f3n que valide si una palabra o frase es un <strong>pal\u00edndromo<\/strong> (se lee igual en un sentido que en otro)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; const palindromo = (palabra = \"\") =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (!palabra) return console.log(\"No ingresaste una palabra o frase\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; palabra = palabra.toLowerCase();\n\n&nbsp; &nbsp; &nbsp; &nbsp; let alReves = palabra.split(\"\").reverse().join(\"\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; return palabra === alReves\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? console.log(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `S\u00ed es pal\u00edndromo. Palabra original ${palabra} - Palabra al rev\u00e9s: ${alReves}`\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : console.log(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `No es pal\u00edndromo. Palabra original ${palabra} - Palabra al rev\u00e9s: ${alReves}`\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; palindromo(\"Salas\");\n&nbsp; &nbsp; &nbsp; palindromo(\"Francisco\");<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. Eliminar cierto patr\u00f3n de caracteres<\/h2>\n\n\n\n<p>Vamos a programar una funci\u00f3n expresada que elimine cierto patr\u00f3n de caracteres de un texto dado<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  &nbsp; &nbsp; const eliminarCaracteres = (texto = \"\", patron = \"\") =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; !texto\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? console.log(\"No ingresaste un texto\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : !patron\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? console.log(\"No ingresaste un patr\u00f3n de caracteres\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : console.log(texto.replace(new RegExp(patron, \"ig\"), \"\"));\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; eliminarCaracteres();\n&nbsp; &nbsp; &nbsp; eliminarCaracteres(\"x1, x2, x3, x4\", \"x\");<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En estos primeros ejercicios estamos manejando cadenas de texto y como podemos manipularlas. Comencemos a resolver los ejercicios:<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":720,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-724","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"En estos primeros ejercicios estamos manejando cadenas de texto y como podemos manipularlas. Comencemos a resolver los ejercicios:","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=724"}],"version-history":[{"count":1,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/724\/revisions"}],"predecessor-version":[{"id":725,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/724\/revisions\/725"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/720"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}