{"id":738,"date":"2024-07-30T20:37:33","date_gmt":"2024-07-30T18:37:33","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=738"},"modified":"2024-07-30T20:37:34","modified_gmt":"2024-07-30T18:37:34","slug":"007-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\/007-ejercicios-de-logica-de-programacion\/","title":{"rendered":"007. Ejercicios de l\u00f3gica de programaci\u00f3n"},"content":{"rendered":"\n<p>En este cap\u00edtulo abordaremos el tema de los <strong>arrays<\/strong>, para ello vamos a resolver los siguientes ejercicios, creando funciones que los resuelvan. Los ejercicios son:<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">1. N\u00fameros elevados al cuadrado<\/h2>\n\n\n\n<p>Creamos una funci\u00f3n que, dado un array num\u00e9rico, devuelva otro array con los n\u00fameros elevados al cuadrado. Para este ejercicio vamos a utilizar lo que&nbsp; se denomina map, que permite generar un nuevo array a partir del original, y a este nuevo array hacerle las modificaciones que necesitemos. El m\u00e9todo map se utiliza mucho en librer\u00edas reactivas o frameworks como React, Vue&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; const devolverCuadrados = (arr = undefined) =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (!arr) return console.log(\"No ingresaste un array de n\u00fameros\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (!(arr instanceof Array))\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"El valor que ingresaste no es un array\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (arr.length === 0) return console.log(\"El array est\u00e1 vac\u00edo\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; for (let num of arr) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (typeof num !== \"number\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(`El valor \"${num}\" no es un n\u00famero`);\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; const newArr = arr.map((el) =&gt; el * el);\n\n&nbsp; &nbsp; &nbsp; &nbsp; return console.log(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `Array original: ${arr} \\n Array elevado al cuadrado: ${newArr}`\n&nbsp; &nbsp; &nbsp; &nbsp; );\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; devolverCuadrados();\n&nbsp; &nbsp; &nbsp; devolverCuadrados([2, \"3\"]);\n&nbsp; &nbsp; &nbsp; devolverCuadrados([2, 3]);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. N\u00famero m\u00e1s alto<\/h2>\n\n\n\n<p>Proponemos una funci\u00f3n que, dado un array, devuelva el n\u00famero m\u00e1s alto y el m\u00e1s bajo de dicho array. Para este ejercicio vamos a ver dos m\u00e9todos importantes del objeto Math que nos van a permitir resolver este ejercicio. Se trata de los m\u00e9todos min() y max(). Junto con los m\u00e9todos min() max() le pasaremos el par\u00e1metro en forma de spread operator.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; const arrayMinMax = (arr = undefined) =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (!arr) return console.log(\"No ingresaste un array de n\u00fameros\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (!(arr instanceof Array))\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"El valor que ingresaste no es un array\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (arr.length === 0) return console.log(\"El array est\u00e1 vac\u00edo\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; for (let num of arr) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (typeof num !== \"number\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(`El valor \"${num}\" no es un n\u00famero`);\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; return console.info(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `Array original: ${arr}\\nValor mayor: ${Math.max(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...arr\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )}\\nValor menor: ${Math.min(...arr)}`\n&nbsp; &nbsp; &nbsp; &nbsp; );\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; arrayMinMax([2, 3, 5, 7]);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. N\u00fameros pares e impares<\/h2>\n\n\n\n<p>En este \u00faltimo ejercicio, creamos una funci\u00f3n que, dado un array, devuelva otros dos arrays, uno con los n\u00fameros pares, y el segundo con los impares. Para este \u00faltimo ejercicio vamos a utilizar el m\u00e9todo filter(), que como su nombre dice, recibe un array, y en base a una condici\u00f3n que le pongamos, va a filtrar cada uno de los elementos y de ah\u00ed sacaremos nuestra respuesta.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  &nbsp; &nbsp; const parImpar = (arr = undefined) =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (!arr) return console.log(\"No ingresaste un array de n\u00fameros\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (!(arr instanceof Array))\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"El valor que ingresaste no es un array\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (arr.length === 0) return console.log(\"El array est\u00e1 vac\u00edo\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; for (let num of arr) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (typeof num !== \"number\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(`El valor \"${num}\" no es un n\u00famero`);\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; return console.log({\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pares: arr.filter((num) =&gt; num % 2 === 0),\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; impares: arr.filter((num) =&gt; num % 2 === 1),\n&nbsp; &nbsp; &nbsp; &nbsp; });\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; parImpar([0, 2, 5, 9, 4]);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En este cap\u00edtulo abordaremos el tema de los arrays, para ello vamos a resolver los siguientes ejercicios, creando funciones que [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":720,"menu_order":6,"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-738","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 abordaremos el tema de los arrays, para ello vamos a resolver los siguientes ejercicios, creando funciones que [&hellip;]","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/738","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=738"}],"version-history":[{"count":1,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/738\/revisions"}],"predecessor-version":[{"id":739,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/738\/revisions\/739"}],"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=738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}