{"id":735,"date":"2024-07-30T20:36:35","date_gmt":"2024-07-30T18:36:35","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=735"},"modified":"2024-07-30T20:36:35","modified_gmt":"2024-07-30T18:36:35","slug":"006-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\/006-ejercicios-de-logica-de-programacion\/","title":{"rendered":"006. Ejercicios de l\u00f3gica de programaci\u00f3n"},"content":{"rendered":"\n<p>En este cap\u00edtulo haremos ejercicios relacionados con <strong>strings<\/strong> y <strong>expresiones regulares<\/strong>, y son los siguientes:<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">1. N\u00famero de vocales y consonantes<\/h2>\n\n\n\n<p>Vamos a programar una funci\u00f3n que dada una cadena de texto, cuente el n\u00famero de vocales y consonantes de dicha cadena.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; const contarLetras = (cadena = \"\") =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (!cadena) return console.log(\"No ingresaste una cadena de texto\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (typeof cadena !== \"string\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"No enviaste un string correcto\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; let vocales = 0,\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; consonantes = 0;\n\n&nbsp; &nbsp; &nbsp; &nbsp; cadena = cadena.toLowerCase();\n\n&nbsp; &nbsp; &nbsp; &nbsp; for (let letra of cadena) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (\/[aeiou\u00e1\u00e9\u00ed\u00f3\u00fa]\/.test(letra)) vocales++;\n\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (\/[bcdfghjklmn\u00f1pqrstvwxyz]\/.test(letra)) consonantes++;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; return console.log({\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cadena,\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vocales,\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; consonantes,\n&nbsp; &nbsp; &nbsp; &nbsp; });\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; contarLetras();\n&nbsp; &nbsp; &nbsp; contarLetras(5);\n&nbsp; &nbsp; &nbsp; contarLetras(\"Hola Mundo\");\n&nbsp; &nbsp; &nbsp; contarLetras(\"bbbbb\");<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. Nombre v\u00e1lido<\/h2>\n\n\n\n<p>Crearemos una funci\u00f3n que valide que un texto sea un nombre v\u00e1lido o no.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; const validarNombre = (nombre = \"\") =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (!nombre) return console.log(\"No se ha enviado nada\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (typeof nombre !== \"string\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"El dato enviado no es una cadena de texto\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; let expReg = \/^[A-Za-z\u00d1\u00f1\u00c1\u00e1\u00c9\u00e9\u00cd\u00ed\u00d3\u00f3\u00da\u00fa\\s]+$\/g.test(nombre);\n\n&nbsp; &nbsp; &nbsp; &nbsp; return expReg\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? console.log(\"El nombre ingresado es v\u00e1lido\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : console.log(\"El nombre ingresado NO es v\u00e1lido\");\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; validarNombre(\"Francisco\");\n&nbsp; &nbsp; &nbsp; validarNombre(9);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. Email v\u00e1lido<\/h2>\n\n\n\n<p>Programaremos una funci\u00f3n que valide que igualmente un texto sea un email v\u00e1lido o no.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  &nbsp; &nbsp; const validarEmail = (email = \"\") =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (!email) return console.log(\"No enviaste nada\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (typeof email !== \"string\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"No enviaste una cadena de texto\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; let expReg =\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/[a-z0-9]+(\\.[_a-z0-9]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,15})\/i.test(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; email\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );\n\n&nbsp; &nbsp; &nbsp; &nbsp; return expReg\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? console.log(\"Email correcto\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : console.log(\"Email incorrecto\");\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; validarEmail(\"fran@fran.es\");\n&nbsp; &nbsp; &nbsp; validarEmail();<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En este cap\u00edtulo haremos ejercicios relacionados con strings y expresiones regulares, y son los siguientes:<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":720,"menu_order":5,"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-735","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 este cap\u00edtulo haremos ejercicios relacionados con strings y expresiones regulares, y son los siguientes:","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/735","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=735"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/735\/revisions"}],"predecessor-version":[{"id":737,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/735\/revisions\/737"}],"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=735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}