{"id":729,"date":"2024-07-30T20:34:14","date_gmt":"2024-07-30T18:34:14","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=729"},"modified":"2024-07-30T20:34:14","modified_gmt":"2024-07-30T18:34:14","slug":"004-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\/004-ejercicios-de-logica-de-programacion\/","title":{"rendered":"004. Ejercicios de l\u00f3gica de programaci\u00f3n"},"content":{"rendered":"\n<p>En este cap\u00edtulo abordaremos los siguientes ejercicios, relacionados con n\u00fameros.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">1. N\u00famero primo<\/h2>\n\n\n\n<p>Crearemos una funci\u00f3n expresada, utilizando Arrow Function, que determine si un n\u00famero es o no primo.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; const numeroPrimo = (numero = undefined) =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (!numero) return console.log(\"No ingresaste un n\u00famero\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (typeof numero !== \"number\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"El n\u00famero no es un n\u00famero\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (numero === 0) return console.log(\"El n\u00famero no puede ser cero\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (Math.sign(numero) === -1)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"El n\u00famero no puede ser negativo\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (numero === 1)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"El n\u00famero 1 no es un n\u00famero primo\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; let divisible = false;\n\n&nbsp; &nbsp; &nbsp; &nbsp; for (let i = 2; i &lt; numero; i++) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (numero % i === 0) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; divisible = true;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; return divisible\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? console.log(`El n\u00famero ${numero} NO es primo`)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : console.log(`El n\u00famero ${numero} S\u00cd es primo`);\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; numeroPrimo(3);\n&nbsp; &nbsp; &nbsp; numeroPrimo(4);\n&nbsp; &nbsp; &nbsp; numeroPrimo(8);\n&nbsp; &nbsp; &nbsp; numeroPrimo(13);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. N\u00famero par o impar<\/h2>\n\n\n\n<p>Vamos a crear una funci\u00f3n expresada que determine si un n\u00famero es par o impar. Utilizaremos funciones flecha o Arrow Function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; const numeroPar = (numero = undefined) =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (numero === undefined)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"No ingresaste ning\u00fan n\u00famero\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (typeof numero !== \"number\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(`El valor \"${numero}\" NO es un n\u00famero`);\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (numero === 0) return console.log(\"El n\u00famero 0 no es un n\u00famero par\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; return numero % 2 === 0\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? console.log(`El n\u00famero \"${numero}\" es un n\u00famero par`)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : console.log(`El n\u00famero \"${numero}\" es un n\u00famero impar`);\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; numeroPar(2);\n&nbsp; &nbsp; &nbsp; numeroPar(-100);\n&nbsp; &nbsp; &nbsp; numeroPar(0);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. Celsius a Fahrenheit<\/h2>\n\n\n\n<p>En este \u00faltimo ejercicio crearemos una funci\u00f3n que convierta grados Celsius a Fahrenheit y viceversa.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; const convertirGrados = (grados = undefined, unidad = undefined) =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (grados === undefined)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"No ingresaste grados a convertir\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (typeof grados !== \"number\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"No ingresaste un n\u00famero\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (!unidad) return console.log(\"No ingresaste unidad para convertir\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (typeof unidad !== \"string\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"La unidad NO es una cadena de texto\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (unidad.length !== 1 || !\/C|F\/.test(unidad))\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"Valor de unidad no reconocido\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (unidad === \"C\") {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `${grados}\u00baC es igual a ${Math.round((grados * 9) \/ 5 + 32)}\u00baF`\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (unidad === \"F\") {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `${grados}\u00baF es igual a ${Math.round((grados - 32) * (5 \/ 9))}\u00baF`\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; convertirGrados(100, \"C\");\n&nbsp; &nbsp; &nbsp; convertirGrados(0, \"C\");\n&nbsp; &nbsp; &nbsp; convertirGrados(32, \"F\");\n&nbsp; &nbsp; &nbsp; convertirGrados(100, \"F\");<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En este cap\u00edtulo abordaremos los siguientes ejercicios, relacionados con n\u00fameros.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":720,"menu_order":3,"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-729","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 los siguientes ejercicios, relacionados con n\u00fameros.","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/729","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=729"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/729\/revisions"}],"predecessor-version":[{"id":731,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/729\/revisions\/731"}],"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=729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}