{"id":732,"date":"2024-07-30T20:35:20","date_gmt":"2024-07-30T18:35:20","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=732"},"modified":"2024-07-30T20:35:21","modified_gmt":"2024-07-30T18:35:21","slug":"005-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\/005-ejercicios-de-logica-de-programacion\/","title":{"rendered":"005. Ejercicios de l\u00f3gica de programaci\u00f3n"},"content":{"rendered":"\n<p>En este cap\u00edtulo estamos trabajando con n\u00fameros y fechas, los ejercicios que vamos a hacer son los siguientes.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">1. Conversi\u00f3n de n\u00fameros de base binaria a decimal<\/h2>\n\n\n\n<p>Vamos a crear una funci\u00f3n que convierta n\u00fameros de base binaria a decimal y viceversa.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; const convertirBinarioDecimal = (\n&nbsp; &nbsp; &nbsp; &nbsp; numero = undefined,\n&nbsp; &nbsp; &nbsp; &nbsp; base = undefined\n&nbsp; &nbsp; &nbsp; ) =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (numero === undefined)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"No ingresaste el n\u00famero a convertir\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (typeof numero !== \"number\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"El par\u00e1metro n\u00famero tiene que ser un n\u00famero\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (base === undefined) return console.log(\"No ingresaste una base\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (typeof base !== \"number\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"La base ingresada no es un n\u00famero\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (base === 2) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `${numero} base ${base} = ${parseInt(numero, base)} base 10`\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );\n&nbsp; &nbsp; &nbsp; &nbsp; } else if (base === 10) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `${numero} base ${base} = ${numero.toString(2)} base 2`\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );\n&nbsp; &nbsp; &nbsp; &nbsp; } else {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"El tipo de base a convertir no es v\u00e1lido\");\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; convertirBinarioDecimal();\n&nbsp; &nbsp; &nbsp; convertirBinarioDecimal(100, 2);\n&nbsp; &nbsp; &nbsp; convertirBinarioDecimal(4, 10);\n&nbsp; &nbsp; &nbsp; convertirBinarioDecimal(4, 100);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. Monto final<\/h2>\n\n\n\n<p>Vamos a programar una funci\u00f3n que devuelva el monto final despu\u00e9s de aplicar descuento a una cantidad dada.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  &nbsp; &nbsp; const aplicarDescuento = (monto = undefined, descuento = 0) =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (monto === undefined) return console.log(\"No ingresaste el monto\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (typeof monto !== \"number\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"No ingresaste un n\u00famero\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (monto === 0) return console.log(\"El monto no puede ser cero\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (Math.sign(monto) === -1)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"El monto no puede ser negativo\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (typeof descuento !== \"number\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"El descuento no es un n\u00famero\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (Math.sign(descuento) === -1)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"El descuento no puede ser negativo\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; return console.log(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `${monto} - ${descuento}% = ${monto - (monto * descuento) \/ 100}`\n&nbsp; &nbsp; &nbsp; &nbsp; );\n&nbsp; &nbsp; &nbsp; };\n,\n&nbsp; &nbsp; &nbsp; aplicarDescuento(100, 10);\n&nbsp; &nbsp; &nbsp; aplicarDescuento(100, 100);\n&nbsp; &nbsp; &nbsp; aplicarDescuento(100, 20);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. Cuantos a\u00f1os han pasado<\/h2>\n\n\n\n<p>Vamos a programar una funci\u00f3n que, dada una fecha v\u00e1lida,determine cuantos a\u00f1os han pasado hasta el d\u00eda de hoy. Para este ejercicio vamos a utilizar el formato timestamp, que significa cuantos milisegundos han pasado del 1 de enero de 1970 hasta ahora. Lo que interesar\u00eda ser\u00eda convertir ambas fechas a valores timestamp, hacer la operaci\u00f3n aritm\u00e9tica que tengamos que hacer, y esto, nuevamente volverlo a convertir a la unidad de tiempo que necesitemos nuevamente (segundos, horas&#8230;).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  &nbsp; &nbsp; const calcularAnios = (fecha = undefined) =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (fecha === undefined) return console.log(\"No mandaste la fecha\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (!(fecha instanceof Date))\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return console.log(\"El valor que ingresaste no es una fecha v\u00e1lida\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Variable expresada en milisegundos\n&nbsp; &nbsp; &nbsp; &nbsp; let hoyMenosFecha = new Date().getTime() - fecha.getTime();\n\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Transformaci\u00f3n de milisegundos a una fecha entendible\n&nbsp; &nbsp; &nbsp; &nbsp; let aniosEnMilisegundos = 1000 * 60 * 60 * 24 * 365;\n\n&nbsp; &nbsp; &nbsp; &nbsp; let aniosHumanos = Math.floor(hoyMenosFecha \/ aniosEnMilisegundos);\n\n&nbsp; &nbsp; &nbsp; &nbsp; return Math.sign(aniosHumanos) === -1\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? console.log(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `Faltan ${Math.abs(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aniosHumanos\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )} a\u00f1os para el ${fecha.getFullYear()}`\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : Math.sign(aniosHumanos) === 1\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? console.log(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `Han pasado ${aniosHumanos} a\u00f1os desde ${fecha.getFullYear()}`\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : console.log(`Estamos en el a\u00f1o actual: ${fecha.getFullYear()}`);\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; calcularAnios(new Date());\n&nbsp; &nbsp; &nbsp; calcularAnios(new Date(1972, 2, 14));\n&nbsp; &nbsp; &nbsp; calcularAnios(new Date(1872, 2, 14));\n&nbsp; &nbsp; &nbsp; calcularAnios(new Date(2050, 2, 14));<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En este cap\u00edtulo estamos trabajando con n\u00fameros y fechas, los ejercicios que vamos a hacer son los siguientes.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":720,"menu_order":4,"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-732","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 estamos trabajando con n\u00fameros y fechas, los ejercicios que vamos a hacer son los siguientes.","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/732","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=732"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/732\/revisions"}],"predecessor-version":[{"id":734,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/732\/revisions\/734"}],"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=732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}