<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ainaperez</title>
    <description>The latest articles on DEV Community by ainaperez (@ainaperez).</description>
    <link>https://dev.to/ainaperez</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F607658%2F72d4fa66-cc05-4960-ac1f-c22d6870998e.jpeg</url>
      <title>DEV Community: ainaperez</title>
      <link>https://dev.to/ainaperez</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ainaperez"/>
    <language>en</language>
    <item>
      <title>What is Prototype and how it works in constructor functions?</title>
      <dc:creator>ainaperez</dc:creator>
      <pubDate>Mon, 07 Feb 2022 09:53:12 +0000</pubDate>
      <link>https://dev.to/ainaperez/what-is-prototype-and-how-it-works-in-constructor-functions-43kj</link>
      <guid>https://dev.to/ainaperez/what-is-prototype-and-how-it-works-in-constructor-functions-43kj</guid>
      <description>&lt;p&gt;hey there 👋, I'm &lt;strong&gt;Aina&lt;/strong&gt; a web development student on my way to become a full-time developer.&lt;/p&gt;

&lt;p&gt;In this article I'm offering an explanation of Object.prototype and why it is important when working with constructor functions.&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;

&lt;h2&gt;
  
  
  Context
&lt;/h2&gt;

&lt;p&gt;In the beginning of my studies with Javascript, I was completing an exercise from my professors where I had to build a constructor function with certain properties and create two objects from that constructor function. This exercise was part of a bigger project where we had to create a Formula 1 viewer that displayed the results of races in a table format (you can find the project &lt;a href="https://github.com/ainaperez/formula1-results-viewer"&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;In that exercice there were some build tests that we had to pass in order to complete the project.&lt;/p&gt;

&lt;p&gt;My first version of the constructor function was this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function DriverFn(givenName, familyName, dateOfBirth) {
       this.name = String(givenName + ' ' + familyName);
       this.dateOfBirth = new Date (dateOfBirth);
       this.getYearOfBirth = function(){
         return Number(this.dateOfBirth.getFullYear());
       }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then I created two objects using that function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = new DriverFn('Lewis', 'Hamilton', '1985-01-07');
var y = new DriverFn('Michael', 'Schumacher', '1969-01-03');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When console logging the data, for example with x.getYearOfBirth, I was getting everything correctly, but surprisingly the tests weren’t passing.&lt;/p&gt;

&lt;p&gt;When checking the reason, I saw that the tests were built to expect the use of prototype.&lt;/p&gt;

&lt;p&gt;And at that moment I wondered why would I use prototype if it works perfectly with only this?🤔&lt;/p&gt;

&lt;h2&gt;
  
  
  The use of Constructor functions and how they look when using this
&lt;/h2&gt;

&lt;p&gt;The purpose of using constructor functions is to be able to make many different objects from one function. Sometimes it can be only 10 objects but surely on many occasions there will be many more, perhaps hundreds or thousands. This creates a lot of code and memory usage, so another goal we need to achieve with constructor functions is to minimize code duplication.&lt;/p&gt;

&lt;p&gt;Having said that, first I'll show you a picture of how the objects look when we use this. I am going to use the live tool from &lt;a href="https://pythontutor.com/live.html#mode=edit"&gt;Python Tutor&lt;/a&gt; to show you the data structure:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o6aLsqtc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8xnppmn37urir8t694b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o6aLsqtc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8xnppmn37urir8t694b.png" alt="Code seen on Python Tutor showing how it is structured when we use this" width="880" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the keyword this, what we do is bind the properties directly into the object itself. That means that everytime we create an instance from DriverFn, we will get an object with its own name, its own date of Birth and its own method getYearOfBirth with the code of the function.&lt;/p&gt;

&lt;p&gt;In our case, it's fine that each object has its associated name and date of birth, as they are different in every driver. But, we really don't need to have the function that allows us to know the year of birth duplicated in each driver, as it is the same code for all.&lt;/p&gt;

&lt;p&gt;Now is when Prototype is useful to us.🤫&lt;/p&gt;

&lt;h2&gt;
  
  
  Using prototype in constructor functions
&lt;/h2&gt;

&lt;p&gt;By itself, all objects have an associated protoype. If you went to the console with the previous code, and created an object, you would see the object with all the properties and then a "&lt;em&gt;proto&lt;/em&gt;" property, which in the case of the Driver object is empty.&lt;/p&gt;

&lt;p&gt;In order to include properties in the protoype (&lt;em&gt;proto&lt;/em&gt;), we use the syntax:&lt;/p&gt;

&lt;p&gt;Function.prototype.property = value;&lt;/p&gt;

&lt;p&gt;So our function will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function DriverFn(givenName, familyName, dateOfBirth) {
    this.name = String(givenName + " " + familyName);
    this.dateOfBirth = new Date (dateOfBirth);
    DriverFn.prototype.getYearOfBirth = function(){
          return Number(this.dateOfBirth.getFullYear());
        }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we changed this in the method getYearOfBirth for DriverFn.prototype.&lt;/p&gt;

&lt;p&gt;From now on getYearOfBirth won’t be stored in the object directly but in its prototype.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VHOBpUxo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3828lklmc66zpf2ncij5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VHOBpUxo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3828lklmc66zpf2ncij5.png" alt="Code seen on Python Tutor showing how it is structured when we use prototype" width="880" height="389"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As seen here, a constructor function has been created, with a prototype pointing to the getYearOfBirth function.&lt;/p&gt;

&lt;p&gt;When creating new instances of the object, they will have their own properties of name and dateOfBirth, but then the method will be connected, inside proto, directly with the first getYearOfBirth that we have added to the constructor function, without the need to copy all the code of the function again.&lt;/p&gt;

&lt;p&gt;This saves us a lot of memory space and load time, especially if we have thousands of objects with much more elaborate methods or properties.&lt;/p&gt;

&lt;p&gt;What we have achieved here is based on the basic function of prototypes, which is to allow some objects to access the properties of others without having to define those properties every time we create an object.&lt;/p&gt;

&lt;p&gt;That’s what we call &lt;strong&gt;Prototype Inheritance&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  An introduction to prototype inheritance
&lt;/h2&gt;

&lt;p&gt;As we said, prototype inheritance in Javascript is the ability of objects to access properties of other objects.&lt;/p&gt;

&lt;p&gt;A simple example to explain this prototype inheritance is with Arrays:&lt;/p&gt;

&lt;p&gt;When we create an array, we know that we can access many methods to manipulate it. Some of these methods can be: .length(), .indexof(), .reverse() etc.&lt;/p&gt;

&lt;p&gt;Actually, we have never defined these methods in our code so how can we possibly access them? This is thanks to protoypes.&lt;br&gt;
When we create a new object, array, function etc in Javascript, Javascript automatically creates, without our knowledge, a base object with all these methods that we can't see. In the case of arrays, the object created by Javascript is arr.&lt;em&gt;proto&lt;/em&gt; (This is the first step in the chain, although we can go further until we reach the root of the root, which will not have any properties).&lt;/p&gt;

&lt;p&gt;Therefore, every time we create an array, we can automatically access these methods without having to create all the code with all the methods in our array. This array, with all its ancestors, forms what is called a &lt;strong&gt;“chain of prototypes”&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the case of our DriverFn object, which is a function, all the objects we create will inherit (that is, we will be able to access), the method and properties of the DriverFn constructor function, but also of the function.&lt;em&gt;proto&lt;/em&gt; prototype object, which has its own properties and own methods, without the need to duplicate all the code over and over again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Prototype is the Javascript method that allows objects to access properties of other objects. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Protoype is also very useful to reduce code volume and decrease loading times. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From a child element we can access properties stored in their parent's &lt;em&gt;proto&lt;/em&gt;. Not only their direct parent, but also the grandparent and so on until we reach the end of the prototype chain, that will have a prototype of null.  &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here ends my explanation of Prototype in constructor functions. A bit longer than what I intended initially but I hope it will be of use to whoever needs it!&lt;/p&gt;

&lt;p&gt;At the beginning I didn’t understand Prototype as well and had to do quite a bit of research to get the good meaning of it, so don’t give up if you are in the same situation!.&lt;/p&gt;

&lt;p&gt;As always, let me know in the comments your thought and if you have any further questions :) &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>firstyearincode</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Solving the Cash Register challenge in Free Code Camp</title>
      <dc:creator>ainaperez</dc:creator>
      <pubDate>Wed, 28 Jul 2021 14:38:08 +0000</pubDate>
      <link>https://dev.to/ainaperez/solving-the-cash-register-challenge-in-free-code-camp-12dk</link>
      <guid>https://dev.to/ainaperez/solving-the-cash-register-challenge-in-free-code-camp-12dk</guid>
      <description>&lt;p&gt;Welcome to my first post ever in Dev!&lt;/p&gt;

&lt;p&gt;If you are also taking FreeCodeCamp's Javascript Algorithms and Data Structures certification, this post may be of interest to you. &lt;/p&gt;

&lt;p&gt;I am going to go through the thought process behind &lt;a href="https://github.com/ainaperez/freecodecamp/blob/main/Javascript_algorithms_and_data_structures/cash_register.js"&gt;my solution&lt;/a&gt; for the last challenge of the certification, the Cash Register. I found it quite complex and took around 3 days to complete.&lt;/p&gt;

&lt;p&gt;The goal of this guide is to meet other developers and share the best approaches to this challenge as I am sure I can greatly improve the code. So be sure to leave a comment and connect!&lt;/p&gt;

&lt;h2&gt;
  
  
  So first, the problem
&lt;/h2&gt;

&lt;p&gt;The challenge gives you a function CheckCashResgister with three parameters: price (prices of the purchase), cash(payment received for the purchase), and cid(cash-in-drawer available at the moment).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;checkCashRegister&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;checkCashRegister&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;19.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PENNY&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.01&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;NICKEL&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;2.05&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;DIME&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;3.1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;QUARTER&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;4.25&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ONE&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;FIVE&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;TEN&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;TWENTY&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ONE HUNDRED&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;]]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last parameter, cid consists of a 2D array with the number of different coins and notes in the drawer.  &lt;/p&gt;

&lt;p&gt;The main goal of the function is to return an object with two properties: status and change. These properties’ values will change depending on the cid available and the change we need to give back. There can be three cases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Our cash-in-drawer is less than the change, meaning we don’t have enough money to return the change, OR we cannot return the exact change. This means if we need to return $0.5 but only have a $1 note, we won’t return the change.&lt;br&gt;
In that case the values to return will be: {status: "INSUFFICIENT_FUNDS", change: []} &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Our cash-in-drawer and change are equal. In that case, our cid will be the value for the key change: {status: "CLOSED", change: [cid]}.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The cash-in-drawer is higher than the change. In that case, the value of change will be an array containing the different notes and coins to return sorted from highest to lowest.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Breaking up the solution
&lt;/h2&gt;

&lt;p&gt;The first step is to define what we will need to give back, creating the variable difference to calculate de margin between the cash given and the price to pay.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;checkCashRegister&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;difference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cash&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;originalDiff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;difference&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;objectReturn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;change&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arrCurrency&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ONE HUNDRED&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; 
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;TWENTY&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; 
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;TEN&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; 
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;FIVE&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; 
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ONE&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; 
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;QUARTER&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.25&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;DIME&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;NICKEL&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.05&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PENNY&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.01&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This variable will change further in the code. That’s why we create a Const orginalDiff to store the original difference. We will need this variable further in the code to define the three possible outcomes.&lt;/p&gt;

&lt;p&gt;We also create the object to return called objectReturn with the two properties. For now, the status will be set to an empty string, and the change will be set to an empty array.&lt;/p&gt;

&lt;p&gt;We will also create the variable arrCurrency copying the different currency values from the statement in an array.  We are going to use this array later to calculate the availability of each currency.&lt;/p&gt;

&lt;p&gt;After that, our first action is to reverse the original cid array. The cid is given in ascendent order but we reverse it to be able to compare each sub-array with the correspondent one in the arrCurrency array.&lt;/p&gt;

&lt;p&gt;Then, we create a variable cidSum and use a for loop to sum all the money in our drawer. Comparing the cidSum to originalDiff, we will be able to create the three possible returns.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;cid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;cidSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;cid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;cidSum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;cid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it gets to the complicated part. We need to find a way to return the change with the appropriate currency.&lt;br&gt;
To do that we will use a for loop that will iterate through the different currencies of our array arrCurrency.&lt;br&gt;
To begin with, we create a new array with the name result where we copy all the exact values of arrCurrency with the spread operator. In each iteration, we will change the value of each currency in the result array to the number needed for the change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;arrCurrency&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;arrCurrency&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;returnMoney&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bill&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;arrCurrency&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="nx"&gt;bill&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;difference&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;=&lt;/span&gt;&lt;span class="nx"&gt;arrCurrency&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;bill&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;difference&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nx"&gt;arrCurrency&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nx"&gt;returnMoney&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;arrCurrency&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nx"&gt;bill&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;returnMoney&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
          &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;returnMoney&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;returnMoney&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;returnMoney&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;parseFloat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;returnMoney&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;returnMoney&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the for loop:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We create a variable returnMoney where we will store the value of each coin calculated for the change.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We will also need to know if we have money available from each currency. For that, we create a variable bill and divide each currency of our cash-in-drawer for its counterpart in the arrCurrency, and we round the number to two decimals. If bill is greater or equal than 1, it will mean we will have currency available.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We create a while loop. As long as the difference is greater than the currency we are iterating at at the moment, and bill is greater or equal than 1, the loop will keep subtracting the currency value to our difference and adding that same currency to our variable returnMoney.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once the difference goes under the currency value or we run out of money, we go on to the if statement. If the returnMoney is greater than 0, meaning we will return change in that note or coin, we will have to evaluate if the number needs rounding or not.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One of the problems I encountered was JavaScript’s floating point number precision. The second if statement looks if a number is decimal or not by substracting its largest integer with the Math.floor method. (Ex = 5.05 – Math.floor(5.05) = 0.05). If the result is greater than 0 it will round the number to two decimals. As the method toFixed returns a string, we will need to convert it to a number again to pass the tests.&lt;/p&gt;

&lt;p&gt;In all cases, we will end up changing the original value of the currency in our result array with that of returnMoney.&lt;/p&gt;

&lt;p&gt;Before going to the last part of the challenge, we will need to calculate if we will be able to return the exact change.&lt;br&gt;
For that, we create another for loop that will sum all the values stored in our array result to the variable sumResult. As we have calculated in the previous for loop, our change to give back according to the currencies available will mean that if the sum of all these values is less than our original difference, we won’t be able to give back the exact change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sumResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;cid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;sumResult&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;sumResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sumResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our last if statement defines the three outcomes possible:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cidSum&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;originalDiff&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;sumResult&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;originalDiff&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;objectReturn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;INSUFFICIENT_FUNDS&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cidSum&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;originalDiff&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="nx"&gt;objectReturn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CLOSED&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;objectReturn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;change&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cid&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;resultFiltered&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;
      &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;!==&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
          &lt;span class="nx"&gt;resultFiltered&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;  
        &lt;span class="p"&gt;}&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt;
     &lt;span class="nx"&gt;objectReturn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;OPEN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nx"&gt;objectReturn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;change&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;resultFiltered&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;objectReturn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If the sum of our cash-in drawer, or the sum of the currency available, is less than the difference to give back  it will change the property status of our object to ‘INSUFFICIENT_FUNDS’.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the sum of our cash-in drawer is the same as our difference it will change both properties of the object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, if the sum of our cash-in drawer is greater than the difference, it will create a filtered array of our result array, without all the coins with a value of 0, and change the properties to their correspondent values.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the end, we will return the object.&lt;/p&gt;

&lt;p&gt;I am sure you are happy-crying now as you realize this long-ass article has finally come to an end. Hopefully, you will have been able to understand it, as concision and storytelling have never been my strongest skills.&lt;/p&gt;

&lt;p&gt;Either way, let me know in the comments what do you think about the code and please do give suggestions for improvements, they will be greatly appreciated!&lt;/p&gt;

&lt;p&gt;You can also check out the full solution in my &lt;a href="https://github.com/ainaperez/freecodecamp/blob/main/Javascript_algorithms_and_data_structures/cash_register.js"&gt;Github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>freecodecamp</category>
      <category>challenge</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
