## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
• Create your own community
##### Activity
• Dec 21 2018 17:11
• Jun 05 2018 23:13
• May 25 2018 16:50
Rob Ede
@robjtede
just post the question my dude
Chigozie Igwebuike Christopher
@Gozieh
hi guys, i've been struggling with my algorithm for a couple hours now and still cant get one of the last test cases right. Below is the code i have for the cash register project, pls any feedback on all my wrongs would be appreciated, the code is a bit messy too so pls forgive me in advance
//function checkCashRegister houses the major global variables and conditions to be met
//function findChange searches for exact change to be returned
//function totalCid calculates the total number of change in the drawer
//'cid' stands for cash in drawer
//'theAvailable' refers to total number of available change in the drawer

function totalCid(cid) {
let total = 0;
for (let piece of cid ) {
total += piece[1];
}
};

//exact change

function findChange(moneyOwed, cid) {
var change_arr = [];
var currencyValues = {
'ONE HUNDRED': 100.00,
'TWENTY': 20.00,
'TEN': 10.00,
'FIVE': 5.00,
'ONE': 1.00,
'QUARTER': 0.25,
'DIME': 0.10,
'NICKEL': 0.05,
'PENNY': 0.01
};
for (let i = cid.length - 1; i >= 0; i--) {
const currName = cid[i][0];
const currTotal = cid[i][1];
const currValue = currencyValues[currName];
let currAmount = (currTotal/currValue).toFixed(2);
let currToReturn = 0;
while (moneyOwed >= currValue && currAmount > 0) {
moneyOwed -= currValue;
moneyOwed = moneyOwed.toFixed(2);
currAmount --;
currToReturn ++;
}
if (currToReturn > 0) {
change_arr.push([currName, currToReturn * currValue]);

}

}

return change_arr

};

function checkCashRegister (price, cash, cid) {
var register = {'status' : null, 'change' : []};
const moneyOwed = parseFloat(cash - price).toFixed(2);
const theAvailable = totalCid(cid);

if (Number(moneyOwed) > Number(theAvailable)) {
register.status = 'INSUFFICIENT_FUNDS';
return register;
};

if (Number(moneyOwed) === Number(theAvailable)) {
register.status = 'CLOSED';
register.change = [...cid];
return register;
};
register.change = findChange(moneyOwed, cid);

if (Number(moneyOwed) < Number(theAvailable)) {
register.status = 'OPEN';
return register
}  return {status : "INSUFFICIENT_FUNDS"};

}
here is the test case i cant get right;
checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "INSUFFICIENT_FUNDS", change: []}.
dimtabu
@taburetkin
@Gozieh your test case has cid value of 1.01 and moneyOwed 0.50
so, in checkCashRegister you get third if.
dimtabu
@taburetkin
@Gozieh, also, i have to say
if you are going to calculate money values in js you probably want to use some special library, f.e. decimal.js
in case you dont know, in js: 0.1 + 0.2 != 0.3;
Stephen James
@sjames1958gm
@Gozieh you need to handle the case where after going through the drawer loop that there is still money owed be cause the denominations donâ€™t match.
Chigozie Igwebuike Christopher
@Gozieh
@taburetkin thanks for the feedback buddy, i've heard about js and its complications with floats and i've tried to work around them in my solution: using toFixed as a method to bring the decimal to two places, although now you mention it, i might just look at other options
@sjames1958gm thanks for the feedback chief, i'll take another look at the logic in loop and see how to implement your hint.
i appreciate the feedback guys.
Stephen James
@sjames1958gm
:+1:
dimtabu
@taburetkin
@Gozieh , maybe this will help you somehow
https://codepen.io/dimatabu/pen/zgoQXq?editors=0010
Chigozie Igwebuike Christopher
@Gozieh
thanks for feedback gentlemen, i just handled the test case for that problem right after the function ran and boom.
//function checkCashRegister houses the major global variables and conditions to be met
//function findChange searches for exact change to be returned
//function totalCid calculates the total number of change in the drawer
//'cid' stands for cash in drawer
//'theAvailable' refers to total number of available change in the drawer

function totalCid(cid) {
let total = 0;
for (let piece of cid ) {
total += piece[1];
}
};

function checkCashRegister (price, cash, cid) {
var register = {'status' : null, 'change' : []};
const moneyOwed = parseFloat(cash - price).toFixed(2);
const theAvailable = totalCid(cid);

if (Number(moneyOwed) > Number(theAvailable)) {
register.status = 'INSUFFICIENT_FUNDS';
return register;
};

if (Number(moneyOwed) === Number(theAvailable)) {
register.status = 'CLOSED';
register.change = [...cid];
return register;
};

register.change = findChange(moneyOwed, cid);

if (moneyOwed > totalCid(register.change)) {
register.status = 'INSUFFICIENT_FUNDS';
register.change = []
return register;
};

if (Number(moneyOwed) < Number(theAvailable)) {
register.status = 'OPEN';
return register
};

};

//exact change
function findChange(moneyOwed, cid) {
var change_arr = [];
var currencyValues = {
'ONE HUNDRED': 100.00,
'TWENTY': 20.00,
'TEN': 10.00,
'FIVE': 5.00,
'ONE': 1.00,
'QUARTER': 0.25,
'DIME': 0.10,
'NICKEL': 0.05,
'PENNY': 0.01
};
//looping through the drawer for cash
for (let i = cid.length - 1; i >= 0; i--) {
const currName = cid[i][0];
const currTotal = cid[i][1];
const currValue = currencyValues[currName];
let currAmount = (currTotal/currValue).toFixed(2);
let currToReturn = 0;
//selecting the right bill value to return
while (moneyOwed >= currValue && currAmount > 0) {
moneyOwed -= currValue;
moneyOwed = moneyOwed.toFixed(2);
currAmount --;
currToReturn ++;
}

if (currToReturn > 0) {
change_arr.push([currName, currToReturn * currValue]);

}

}

return change_arr
};
Khalifathul meeran
@khalifathul
@khalifathul
Hi
I've an array like this
var ab = [
{weight: 1, answer: "3"},
{weight: 2, answer: "5"},
{weight: 1, answer: "2"},
{weight: 2, answer: "3"},
{weight: 1, answer: "4"},
];
and I want to multiply this value like this (1 x 3) x (2 x 5) x (1 x 2) x (2 x 3) x (1 x 4) = 1440 using javascript
Can anyone help me
?
dimtabu
@taburetkin
let score = ab.reduce((memo, item) => memo += item.weight * item.answer, 0)
ah, there is multiply
let score = ab.reduce((memo, item) => memo *= item.weight * item.answer, 1);
Khalifathul meeran
@khalifathul
Thanks..
josecgomezvazquez
@josecgomezvazquez
Does anyone know how to use the Webster Dictionary API? I could use some pointers. I read the documentation, but I am still confused as heck.
I want to create a Writing Prompt Generator, and I think the API can help instead of me making a database of words
Abhinav Sharma
@tweaktastic

Hi. I needed your thoughts on the use of constants as in the code below.

function createUrl(urlPath) {
const endpoint = url.parse(env.config.get('myEndpoint'));
endpoint.pathname = path.posix.join(endpoint.pathname, urlPath);
return endpoint;
}

The above snippet won't throw an error but is it okay to declare some object as a const even if I know that I would be making some change to it?

Abhi
@abhigk
I am mapping array of objects. and also displaying obj.quantity on browser
I am also passing obj in addQuantity(obj) and subQuantity(obj)
However increment and decrement of quantity is not reflected on browser that obj.quantity
React question
dimtabu
@taburetkin
react answer: you make some mistakes
syedsimanta03
@syedsimanta03


<!--Gallery Item-->
<div class="gallery-item mix branding vue col-md-4 col-sm-6 col-xs-12">
<div class="inner-box">
<figure class="image-box">
<img src="images/gallery/25b.jpg" alt="css">
<!--Overlay Box-->
<div class="overlay-box">
<div class="overlay-inner">
<div class="content">
<a href="#
" target="_blank" rel="noopener noreferrer" class="link"><span class="icon flaticon-link"></span></a>
<a href="images/gallery/25b.jpg" data-fancybox="images" data-caption="branding"  class="link"><span class="icon flaticon-magnifying-glass"></span></a>
</div>

</div>
</div>
</figure>
</div>
</div>
How can I generate same code block above 5times but with different images number like this one has 25b.jpg. So I am unable to generate this same block dynamically 26b.jpg, 27b.jpg ... and so on. I tried with js
Stephen James
@sjames1958gm
If your version of js supports template strings then replace the fixed values with
${value} then loop over the various values Shekhar Ramola @shekharramola Hi, can someone please give me a piece of advice. I need to built a very heavy, complicated web application with multiple web pages(almost 60 pages).I already have a theme with me. I want to use a framework like angular which could make life a bit easier but that website need very good SEO. using angular universal, it might be possible but it is gonna use jquery and a lot of query selector which will not work in angular universal. so if I go with the plain javascript approach, how am I gonna handle routing and all. dimtabu @taburetkin take a look at backbone.marionette Abdul Basit @Abdul___Says_twitter Hi, is there any way to hide my application's assets in DOM ? let say i have some images on canvas but i dont want to show those images in browser's image section. nicsachin @nicsachin <img src="image.jpg" style="display: none"/> i think this will help Alecell @Alecell Hey guys, any ideal how I improve the readability of this object?  var routes = { get main() { return Route('', { customer: Route('/customer/:id', { get cart() { return Route('/cart')() }, }), edit_realty: Route('/realty/:id', { config: Route('/config/:setting', { get look() { return Route('/look')() } }) }) })()} } Im building an route manager to my team but this config obj is unredable and I dont know how to improve this this is the route function:  function Route(path, children) { return function(param) { url += path.replace(/:id|:setting/g, param); return children; }; } Shekhar Ramola @shekharramola can I return common jquery http response to some other function I want to write common post method will pass the url and json data Stephen James @sjames1958gm Since http is a asynchronous you could wrap it in a promise and then use async/await and pass the response back in the promise resolve. The browser fetch function might be an alternative. Shekhar Ramola @shekharramola browser fetch..yes but it do not work in IE? Stephen James @sjames1958gm Possibly not dimtabu @taburetkin @shekharramola var xhr =$.ajax({...});
mySuperFunction(xhr);

function mySuperFunction(xhr) {
xhr.then(() => .... );
}

what wrong with this approach?

Shekhar Ramola
@shekharramola
@taburetkin , this is cool but I try to follow DRY(Do not repeat) wherever possible.
dimtabu
@taburetkin
Its not clear what do you want to achieve
Shekhar Ramola
@shekharramola
am I doing anything wrong?
 addDynamicHTML('particularId', data.data[i].title);
function addDynamicHTML(selectedElementId, template) {
console.log(selectedElementId);
console.log(template);
if (document) {
console.log(document.getElementById(selectedElementId));
document.getElementById(
JSON.stringify(selectedElementId)
).innerHTML = template;
}
}
 console.log(document.getElementById(selectedElementId));
above statement will say, it is null though it shouldn't be. just a plain synchronous function
dimtabu
@taburetkin
you can work with dom by js only after dominteractive event
and for some browsers after dom loaded event
Shekhar Ramola
@shekharramola
I am creating dom element dynamically.
like this