# CoDEVIANT #4 (3/24/19)

`parameter (positive int, guaranteed) startPriceOld (Old car price)parameter (positive int, guaranteed) startPriceNew (New car price)parameter (positive int, guaranteed) savingperMonthparameter (positive float or int, guaranteed) percentLossByMonthnbMonths(2000, 8000, 1000, 1.5) should return [6, 766] or (6, 766)where 6 is the number of months at the end of which he can buy the new car and 766 is the nearest integer to 766.158 (rounding 766.158 gives 766).`
`nbMonths(12000, 8000, 1000, 1.5) should return [0, 4000]nbMonths(8000, 8000, 1000, 1.5) should return [0, 0]`
`function nbMonths(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth){   //your code here}`
`Test.assertSimilar(nbMonths(2000, 8000, 1000, 1.5), [6, 766])Test.assertSimilar(nbMonths(12000, 8000, 1000, 1.5) ,[0, 4000])`
`function nbMonths(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth){   console.log(startPriceOld, startPriceNew, savingperMonth,     percentLossByMonth);//So we know this cat can save a grand a month. I know who to rob now.//Let’s make a variable starting at 0 that represents this cat’s dough.var dough = 0;//We will need a counter for months…each 1 month savingsPerMonth increases by 1000 & the cost of the new car decreases by 1.5%//& each 2 months the price of the new car decreases 0.5 %let month;//We don’t give a shit about the price of the old car…that seems like a red herring to me//We know that every two months, the value of the variable startPriceNew is going to decrease by 1.5%//& each 2 months the price of the new car decreases 0.5 %//I’m going to be gutsy and directly manipulate the argument instead of making a variable with the initial//new car price to manipulate.//So while dough is less than the price of the new car, we will increase the months by 1,//when we do this, we will augment the dough by 1000 and make the new car price equal itself minus 1.5% of itself.//and whenever//months is divisible by 2 without any remainder (the even months) we will again increase the dough by 1000//& add 0.5 to the argument percentLossByMonth//Once dough is not less than the price of the new car, we’ll stop and divide the price of the new car by our dough.//we’ll round the remainder and put both the result of the division and the remainder in an array, and call it day…we hope.}`
`function nbMonths(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth){console.log(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth);//So we know this cat can save a grand a month. I know who to rob now.//Let’s make a variable starting at 0 that represents this cat’s dough.//I also forgot that we need to mention the value of the old car because he is going//to get credit for it…var dough = 0 + startPriceOld;//so really dough should include the value of startPriceOld and how that argument will change overtime too.//We will need a counter for months…each 1 month savingsPerMonth increases by 1000 & the cost of the new car decreases by 1.5%//& each 2 months the price of the new car decreases 0.5 %let month = 0;//We know that every two months, the value of the variable startPriceNew is going to decrease by 1.5%//& each 2 months the price of the new car decreases 0.5 %//I’m going to be gutsy and directly manipulate the argument instead of making a variable with the initial//new car price to manipulate.//So while dough is less than the price of the new car, we will increase the months by 1,//when we do this, we will augment the dough by 1000 and make the new car price equal itself minus 1.5% of itself.//and whenever//months is divisible by 2 without any remainder (the even months) we will again increase the dough by 1000//& add 0.5 to the argument percentLossByMonthwhile(dough < startPriceNew) {console.log(‘dough is smaller than the price of the new car’);month++;dough += 1000;//no matter what happens, our guy gets another grand each monthswitch(month%2 == 0) {case true://the stuff we do every two monthsconsole.log(‘even month’);console.log(‘month is: ‘ + month);percentLossByMonth += 0.5;startPriceNew = startPriceNew — (startPriceNew * percentLossByMonth/100);dough -= (startPriceOld * percentLossByMonth/100);startPriceOld -= (startPriceOld * percentLossByMonth/100);console.log(‘2nd months percent:’ + percentLossByMonth);break;default://the stuff we do every regular monthconsole.log(‘month is: ‘ + month);startPriceNew = startPriceNew — (startPriceNew * percentLossByMonth/100);dough -= (startPriceOld * percentLossByMonth/100);startPriceOld -= (startPriceOld * percentLossByMonth/100);console.log(‘1st months percent:’ + percentLossByMonth);break;}console.log(‘Dough on hand: ‘ + dough);console.log(‘Price of the New Car at month ‘ + month + ‘: ‘ + startPriceNew);}if(dough >= startPriceNew){console.log(‘saved up enough’);console.log(dough — startPriceNew);return [month, Math.round(dough-startPriceNew)];}//Once dough is not less than the price of the new car, we’ll stop and divide the price of the new car by our dough.//we’ll round the remainder and put both the result of the division and the remainder in an array, and call it day…we hope.}`
`dough += 1000;`
`dough += savingperMonth;`
`function nbMonths(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth){var dough = 0 + startPriceOld;let month = 0;while(dough < startPriceNew) {   month++;   dough += savingperMonth;   switch(month%2 == 0) {      case true:         percentLossByMonth += 0.5;         startPriceNew = startPriceNew — (startPriceNew *     percentLossByMonth/100);         dough -= (startPriceOld * percentLossByMonth/100);         startPriceOld -= (startPriceOld * percentLossByMonth/100);    break;    default:       startPriceNew = startPriceNew — (startPriceNew *   percentLossByMonth/100);      dough -= (startPriceOld * percentLossByMonth/100);      startPriceOld -= (startPriceOld * percentLossByMonth/100);    break;  }}   if(dough >= startPriceNew){    return [month, Math.round(dough-startPriceNew)];   }}`
• dough represents the money the man has on hand PLUS the value of his old car.
• month represents the amount of months that have passed since homeboy began saving up for his new whip.
• While dough is less than the price of the new car:
• month increases by one
• dough increases by the value represented by the savingperMonth argument that is passed into the function.
• We use a modulus operator to figure out if we are dealing with every second month or just another regular month, because each second month has different effects on the money flow of our prospective car buyer.
`console.log( 7 % 3 ); // we get a remainder of 1…so you’ll just get 1console.log( 11 % 3); //we get a remainder of 2…you will then get 2.`
• We increase the amount of percentage lost per month by 0.5
• The new car’s price [startPriceNew] equals itself minus (itself * the percentage loss each month)
• dough equals itself minus the (old car’s value * the percentage loss each month)
• The old car’s value also becomes itself minus its value multiplied by the percentage loss each month.
• If we are in a regular month:
• The exact same stuff happens EXCEPT that the percentage lost per month value remains the same.
• Once dough is finally more than or equal to whatever the price of the new car happens to be:
• we return an array with the month value at the first index location, and the rounded difference of the dough subtracted from the price of the new car at the second index location.
`function nbMonths(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth) {var months = 0, moneySaved = 0;while (startPriceNew > startPriceOld + moneySaved){   moneySaved += savingperMonth;   startPriceOld -= (startPriceOld * (percentLossByMonth / 100));   startPriceNew -= (startPriceNew * (percentLossByMonth / 100));   months++;   if (months % 2 == 1) {      percentLossByMonth += .5;   }}return [months, Math.round(startPriceOld + moneySaved — startPriceNew)]; }`
• Our dough variable and the others’ moneySaved variable serve the exact same purpose.
• Great minds do think alike.
• We use a while loop, although the direction of the compared values was reversed in mine.
• The others’ also used similar lines to mine to change the value of dough/moneySaved, startPriceOld, & startPriceNew
• months get augmented LATER in the process, which is why the others’ solution seeks for a remainder of 1 when using the modulus operator between the months value and 2.
• And like ours it is at that point that the percentLossByMonth value augments by 0.5.
• Then similarly to ours, we return an array with months as the first value, and the various values of the man’s wealth with the new car price subtracted; the product of which is then rounded.
`…switch(month%2 == 0) {case true:percentLossByMonth += 0.5;startPriceNew = startPriceNew — (startPriceNew * percentLossByMonth/100);dough -= (startPriceOld * percentLossByMonth/100);startPriceOld -= (startPriceOld * percentLossByMonth/100);break;default:startPriceNew = startPriceNew — (startPriceNew * percentLossByMonth/100);dough -= (startPriceOld * percentLossByMonth/100);startPriceOld -= (startPriceOld * percentLossByMonth/100);break;}…`
`function nbMonths(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth){var dough = 0 + startPriceOld;let month = 0;   while(dough < startPriceNew) {   month++;   dough += savingperMonth;   switch(month%2 == 0) {     case true:      percentLossByMonth += 0.5;     break;     default:     break;  }startPriceNew = startPriceNew — (startPriceNew *      percentLossByMonth/100);dough -= (startPriceOld * percentLossByMonth/100);startPriceOld -= (startPriceOld * percentLossByMonth/100); }   if(dough >= startPriceNew){   return [month, Math.round(dough-startPriceNew)];   }}`
• Other than that…scary math problems can be okay. 🙂
• Sometimes directions are wrong, we were told to expect a \$1000 increase each month but our actual function gave us something different. Be sure to be very skeptical and thorough about what you’re handed to work with. Sometimes people will cut corners and leave just enough out of a description to put you on a wild goose chase. It’s like being given a video game without a controller…

--

--

--

is a web developer, opera singer, actor, and lover of cats. (adrian-rosales.tech)

Love podcasts or audiobooks? Learn on the go with our new app.