{"id":726,"date":"2024-07-30T20:33:05","date_gmt":"2024-07-30T18:33:05","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=726"},"modified":"2024-07-30T20:33:06","modified_gmt":"2024-07-30T18:33:06","slug":"003-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\/003-ejercicios-de-logica-de-programacion\/","title":{"rendered":"003. Ejercicios de l\u00f3gica de programaci\u00f3n"},"content":{"rendered":"\n<p>En este cap\u00edtulo veremos los siguientes ejercicios de <strong>l\u00f3gica de programaci\u00f3n<\/strong>, aplicados en este caso a n\u00fameros.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">1. N\u00famero aleatorio<\/h2>\n\n\n\n<p>Vamos a programar una funci\u00f3n que obtenga un n\u00famero aleatorio entre 501 y 600.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; const aleatorio = () =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(Math.round(Math.random() * 100 + 500));\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; aleatorio();<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. N\u00famero capic\u00faa<\/h2>\n\n\n\n<p>Vamos a programar una funci\u00f3n que reciba un n\u00famero y eval\u00fae si es o no capic\u00faa.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; const capicua = (numero = 0) =&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 valor \"${numero}\" NO es un n\u00famero`);\n\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Convertimos el n\u00famero a cadena de texto\n&nbsp; &nbsp; &nbsp; &nbsp; numero = numero.toString();\n\n&nbsp; &nbsp; &nbsp; &nbsp; let alReves = numero.split(\"\").reverse().join(\"\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; return numero === alReves\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? console.log(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `S\u00ed es cap\u00edcua. N\u00famero original: ${numero} - N\u00famero 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 cap\u00edcua. N\u00famero original: ${numero} - N\u00famero al rev\u00e9s: ${alReves}`\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; capicua(505);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. Factorial de un n\u00famero<\/h2>\n\n\n\n<p>En este \u00faltimo ejercicio programamos una funci\u00f3n que calcule el factorial de un n\u00famero. El factorial de un n\u00famero entero positivo se define como el producto de todos los n\u00fameros enteros positivos desde 1 hasta n.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  &nbsp; &nbsp; const factorial = (numero = undefined) =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (numero === undefined) 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 valor introducido no es un n\u00famero\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (numero === 0)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"No se puede enviar cero como n\u00famero\");\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; let factorial = 1;\n\n&nbsp; &nbsp; &nbsp; &nbsp; for (i = numero; i &gt; 1; i--) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factorial *= i;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; return console.log(`El factorial de un n\u00famero es ${factorial}`);\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; factorial(4);\n&nbsp; &nbsp; &nbsp; factorial(-3);\n&nbsp; &nbsp; &nbsp; factorial(0);\n&nbsp; &nbsp; &nbsp; factorial([]);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En este cap\u00edtulo veremos los siguientes ejercicios de l\u00f3gica de programaci\u00f3n, aplicados en este caso a n\u00fameros.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":720,"menu_order":2,"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-726","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 veremos los siguientes ejercicios de l\u00f3gica de programaci\u00f3n, aplicados en este caso a n\u00fameros.","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/726","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=726"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/726\/revisions"}],"predecessor-version":[{"id":728,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/726\/revisions\/728"}],"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=726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}