I have the following recursive function to count the number of ways change can be returned given various coin denominations:
function makeChange(amount, coins) {
// Note: using Floats here will not work
console.log(`Amount: ${amount}, Coins: ${coins}`);
return (amount === 0) ? 1 :
(amount < 0) ? 0 :
(!coins.length) ? 0 :
makeChange(amount-coins[0], coins)
+ makeChange(amount, coins.slice(1));
}
console.log(
makeChange(11, [7,3,1])
);
However, I'd like to improve the visual-aspect of the debugging to really see what might be going on behind the scenes of the recursive function -- almost like each level of the stack and how to show that. I've improved it to pass a level parameter so I can do indentation and I then have:
function makeChange(amount, coins, level=0) {
console.log(`${' '.repeat(level)}Amount: ${amount}, Coins: ${coins}`);
return (amount === 0) ? 1 :
(amount < 0) ? 0 :
(!coins.length) ? 0 :
makeChange(amount-coins[0], coins, level+1)
+ makeChange(amount, coins.slice(1), level+1);
}
console.log(
makeChange(10, [5,1])
);
But even this is a bit difficult to understand as it has so much superfluous input. What might be a better way to add in various debug helpers to better visualize this?
One possibility is just to also track the returns and use a bit more formatting to group together the events at a single level.
You can run this here, but you will need to open the browser console to see the results, as there are too many lines for StackOverflow's one:
function makeChange(amount, coins, level=0) {
console.log(`${'| '.repeat(level)}makeChange(${amount}, [${coins.join (', ')}])`);
const result = (amount === 0) ? 1 :
(amount < 0) ? 0 :
(!coins.length) ? 0 :
makeChange(amount-coins[0], coins, level+1)
+ makeChange(amount, coins.slice(1), level+1);
console.log(`${'| '.repeat(Math .max(level - 1, 0))}${level > 0 ? '| ' : ''}+--> ${result}`);
return result;
}
console .log (
makeChange(11, [7, 3, 1])
);
.as-console-wrapper {max-height: 100% !important; top: 0}
It will give you output like this:
makeChange(11, [7, 3, 1])
| makeChange(4, [7, 3, 1])
| | makeChange(-3, [7, 3, 1])
| | +--> 0
| | makeChange(4, [3, 1])
| | | makeChange(1, [3, 1])
| | | | makeChange(-2, [3, 1])
| | | | +--> 0
| | | | makeChange(1, [1])
| | | | | makeChange(0, [1])
| | | | | +--> 1
| | | | | makeChange(1, [])
| | | | | +--> 0
| | | | +--> 1
| | | +--> 1
| | | makeChange(4, [1])
| | | | makeChange(3, [1])
| | | | | makeChange(2, [1])
| | | | | | makeChange(1, [1])
| | | | | | | makeChange(0, [1])
| | | | | | | +--> 1
| | | | | | | makeChange(1, [])
| | | | | | | +--> 0
| | | | | | +--> 1
| | | | | | makeChange(2, [])
| | | | | | +--> 0
| | | | | +--> 1
| | | | | makeChange(3, [])
| | | | | +--> 0
| | | | +--> 1
| | | | makeChange(4, [])
| | | | +--> 0
| | | +--> 1
| | +--> 2
| +--> 2
| makeChange(11, [3, 1])
| | makeChange(8, [3, 1])
| | | makeChange(5, [3, 1])
| | | | makeChange(2, [3, 1])
| | | | | makeChange(-1, [3, 1])
| | | | | +--> 0
| | | | | makeChange(2, [1])
| | | | | | makeChange(1, [1])
| | | | | | | makeChange(0, [1])
| | | | | | | +--> 1
| | | | | | | makeChange(1, [])
| | | | | | | +--> 0
| | | | | | +--> 1
| | | | | | makeChange(2, [])
| | | | | | +--> 0
| | | | | +--> 1
| | | | +--> 1
| | | | makeChange(5, [1])
| | | | | makeChange(4, [1])
| | | | | | makeChange(3, [1])
| | | | | | | makeChange(2, [1])
| | | | | | | | makeChange(1, [1])
| | | | | | | | | makeChange(0, [1])
| | | | | | | | | +--> 1
| | | | | | | | | makeChange(1, [])
| | | | | | | | | +--> 0
| | | | | | | | +--> 1
| | | | | | | | makeChange(2, [])
| | | | | | | | +--> 0
| | | | | | | +--> 1
| | | | | | | makeChange(3, [])
| | | | | | | +--> 0
| | | | | | +--> 1
| | | | | | makeChange(4, [])
| | | | | | +--> 0
| | | | | +--> 1
| | | | | makeChange(5, [])
| | | | | +--> 0
| | | | +--> 1
| | | +--> 2
| | | makeChange(8, [1])
| | | | makeChange(7, [1])
| | | | | makeChange(6, [1])
| | | | | | makeChange(5, [1])
| | | | | | | makeChange(4, [1])
| | | | | | | | makeChange(3, [1])
| | | | | | | | | makeChange(2, [1])
| | | | | | | | | | makeChange(1, [1])
| | | | | | | | | | | makeChange(0, [1])
| | | | | | | | | | | +--> 1
| | | | | | | | | | | makeChange(1, [])
| | | | | | | | | | | +--> 0
| | | | | | | | | | +--> 1
| | | | | | | | | | makeChange(2, [])
| | | | | | | | | | +--> 0
| | | | | | | | | +--> 1
| | | | | | | | | makeChange(3, [])
| | | | | | | | | +--> 0
| | | | | | | | +--> 1
| | | | | | | | makeChange(4, [])
| | | | | | | | +--> 0
| | | | | | | +--> 1
| | | | | | | makeChange(5, [])
| | | | | | | +--> 0
| | | | | | +--> 1
| | | | | | makeChange(6, [])
| | | | | | +--> 0
| | | | | +--> 1
| | | | | makeChange(7, [])
| | | | | +--> 0
| | | | +--> 1
| | | | makeChange(8, [])
| | | | +--> 0
| | | +--> 1
| | +--> 3
| | makeChange(11, [1])
| | | makeChange(10, [1])
| | | | makeChange(9, [1])
| | | | | makeChange(8, [1])
| | | | | | makeChange(7, [1])
| | | | | | | makeChange(6, [1])
| | | | | | | | makeChange(5, [1])
| | | | | | | | | makeChange(4, [1])
| | | | | | | | | | makeChange(3, [1])
| | | | | | | | | | | makeChange(2, [1])
| | | | | | | | | | | | makeChange(1, [1])
| | | | | | | | | | | | | makeChange(0, [1])
| | | | | | | | | | | | | +--> 1
| | | | | | | | | | | | | makeChange(1, [])
| | | | | | | | | | | | | +--> 0
| | | | | | | | | | | | +--> 1
| | | | | | | | | | | | makeChange(2, [])
| | | | | | | | | | | | +--> 0
| | | | | | | | | | | +--> 1
| | | | | | | | | | | makeChange(3, [])
| | | | | | | | | | | +--> 0
| | | | | | | | | | +--> 1
| | | | | | | | | | makeChange(4, [])
| | | | | | | | | | +--> 0
| | | | | | | | | +--> 1
| | | | | | | | | makeChange(5, [])
| | | | | | | | | +--> 0
| | | | | | | | +--> 1
| | | | | | | | makeChange(6, [])
| | | | | | | | +--> 0
| | | | | | | +--> 1
| | | | | | | makeChange(7, [])
| | | | | | | +--> 0
| | | | | | +--> 1
| | | | | | makeChange(8, [])
| | | | | | +--> 0
| | | | | +--> 1
| | | | | makeChange(9, [])
| | | | | +--> 0
| | | | +--> 1
| | | | makeChange(10, [])
| | | | +--> 0
| | | +--> 1
| | | makeChange(11, [])
| | | +--> 0
| | +--> 1
| +--> 4
+--> 6
6
It's not beautiful, but it's not too bad.
It seems like the following would be useful to potentially print depending on the verbosity level of the script:
The stack level. Of this can be done using tabs or something similar rather than just "Level=1".
Whether the base case is reached or not. For example, the above function might only reach the base case three times out of 1000 recursive function calls.
The variables that the function is applied with.
With the above, we can improve the function to something like the following:
function makeChange(amount, coins, verbose) {
console.log(`Changing ${amount}...`);
return (function _makeChange(amount, coins, verbose, debugLevel=0, debugCoinsUsed=[]) {
if (verbose || amount===0)
console.log(`${' '.repeat(debugLevel)}Amount: ${amount}, Coins: ${coins}${ amount===0? ' ==> ['+ debugCoinsUsed + ']' :''}`);
return (amount === 0) ? 1 :
(amount < 0) ? 0 :
(!coins.length) ? 0 :
_makeChange(amount-coins[0], coins, verbose, debugLevel+1, [...debugCoinsUsed, coins[0]])
+ _makeChange(amount, coins.slice(1), verbose, debugLevel+1, [...debugCoinsUsed]);
})(amount, coins, verbose);
}
console.log(
makeChange(10, [5,1]),
makeChange(10, [5,1], true)
);
And the non-verbose call would log:
makeChange(10, [5,1], false)
Changing 10...
Amount: 0, AvailableCoins: 5,1 ==> [5,5]
Amount: 0, AvailableCoins: 1 ==> [5,1,1,1,1,1]
Amount: 0, AvailableCoins: 1 ==> [1,1,1,1,1,1,1,1,1,1]
3
I need to group by month or year in the Date column of the table. and I currently used the group function also. I need to add an additional group condition. Is it possible having two grouping conditions and grouping monthly, yearly in Sequelize? Please if someone is using it, share it with me.
Table
-----------------------------------------------------------
| bulk_id | date | grade_GL | gross_weight | lot_id |
-----------------------------------------------------------
| 100 | 2020-10-02 | A | 100 | 1000 |
| 100 | 2020-10-02 | B | 120 | 1002 |
| 100 | 2020-10-02 | C | 140 | 1004 |
| 101 | 2020-10-22 | A | 210 | 1006 |
| 101 | 2020-10-22 | B | 230 | 1008 |
| 101 | 2020-10-22 | C | 250 | 1010 |
| 102 | 2020-11-03 | A | 110 | 1013 |
| 102 | 2020-11-03 | B | 130 | 1015 |
| 102 | 2020-11-03 | C | 200 | 1017 |
| 103 | 2020-11-23 | A | 220 | 1019 |
| 103 | 2020-11-23 | B | 240 | 1021 |
| 103 | 2020-11-23 | C | 150 | 1023 |
Expected Result
------------------------------------------------
| grade_GL | month | sum_of_gross_weight |
------------------------------------------------
| A | October | 310 |
| B | October | 350 |
| C | October | 390 |
| A | November | 330 |
| B | November | 370 |
| C | November | 350 |
** When bulk_Id 100,101,102,103 is given expected result should be above table.
Here is my current Sequeilize:
const gradeWiseTotalLots = await Lot.findAll({
attributes: ['grade_GL', [sequelize.fn('sum', sequelize.col('gross_weight')), 'total_Gross_weight'],],
where: {BulkBulkId: bulk_id_ele.dataValues.bulk_id,},
group: ['grade_GL']
});
Select statement I would expect is:
SELECT `grade_GL`, sum(`gross_weight`) AS `total_Gross_weight` FROM `Lots` AS `Lot` WHERE `Lot`.`BulkBulkId` = 103 GROUP BY `grade_GL`,`month`;
I get the following object from a php file:
[
{"val1":"A","val2":"TA","val3":"5"},
{"val1":"A","val2":"TB","val3":"3"},
{"val1":"A","val2":"TC","val3":"2"}
]
I tried doing things like:
{label: {data:'val1'} }
{label: {data:'val1'[0]} }
{label: {data:['val1'][0]} }
But so far no luck to obtain the value I want.
This is the part of my code that deals with the data from the object:
var containerD1 = document.getElementById('tabD1');
var hotD1 = new Handsontable(containerD1, {
rowHeaders: true,
colHeaders: true,
columns: [
{type:'text', data: 'val2', readOnly:true},
{type:'text', data: 'val3', readOnly:true},
],
nestedHeaders: [
[{label: {data:'val1'}, colspan: 2}],
['Val2','Val3']
]
});
Since val1 value is dynamic, I can't set it as I have done for val2 and val3
So what I want to achieve is obtaining the value from val1 to be able to use it in the nestedHeaders part as the title for my table, since this value is repeated trough my object, it doesn't matter from which one I take it.
So far what I obtain is a table like this
+------+------+
| object |
+------+------+
| val2 | val3 |
+------+------+
| TA | 5 |
| TB | 3 |
| TC | 1 |
+------+------+
In the object part, in my table it says this [object Object].
So I'm guessing it's actually being read as an object instead of the value I want.
And I'm trying to get something like this:
+------+------+
| A |
+------+------+
| val2 | val3 |
+------+------+
| TA | 5 |
| TB | 3 |
| TC | 1 |
+------+------+
Code snippet to get the value of val1
{ label: { data: data[0].val1 } }
I'm not sure if I understand you correctly. If you want to obtain the value of val1 property in your objects, I would do this (since you say it doesn't matter from which element of the array you take it):
const data = [
{"val1":"A","val2":"TA","val3":"5"},
{"val1":"A","val2":"TB","val3":"3"},
{"val1":"A","val2":"TC","val3":"2"}
]
console.log(data[0]["val1"])
Alternatively, you could write:
console.log(data[0].val1) instead of the last line of code.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
var arr = [{id: 1, name: 'John'}, {id: 2, name: 'Mike'}];
var member = arr[0];
arr.splice(0, 1);
console.log(member);
You can keep accessing it because you still have a reference to it. It's no longer in the array, but it still exists. It will continue to exist until all references to it are dropped, and when they are, it becomes eligible for garbage collection.
After this:
var arr = [{id: 1, name: 'John'}, {id: 2, name: 'Mike'}];
you have something like this in memory (several details omitted):
+−−−−−−−−−−−−+
arr:Ref4875−−−−−−>| (array) |
+−−−−−−−−−−−−+ +−−−−−−−−−−−−−−+
| 0: Ref8612 |−−−−−>| (object) |
| 1: Ref4631 |−−+ +−−−−−−−−−−−−−−+
+−−−−−−−−−−−−+ | | id: 1 |
| | name: "John" |
| +−−−−−−−−−−−−−−+
|
| +−−−−−−−−−−−−−−+
+−−>| (object) |
+−−−−−−−−−−−−−−+
| id: 1 |
| name: "John" |
+−−−−−−−−−−−−−−+
(The "Ref1234" values are called object references. You never see their actual values, but you can think of them as a number the JavaScript engine uses to look up where the object is in memory.)
Then after this:
var member = arr[0];
both member and the array refer to the object, something like this:
member:Ref8612−−−−−−−−−−−−−−−−−−−−−+
+−−−−−−−−−−−−+ |
arr:Ref4875−−−−−−>| (array) | |
+−−−−−−−−−−−−+ | +−−−−−−−−−−−−−−+
| 0: Ref8612 |−−−+−>| (object) |
| 1: Ref4631 |−−+ +−−−−−−−−−−−−−−+
+−−−−−−−−−−−−+ | | id: 1 |
| | name: "John" |
| +−−−−−−−−−−−−−−+
|
| +−−−−−−−−−−−−−−+
+−−>| (object) |
+−−−−−−−−−−−−−−+
| id: 1 |
| name: "John" |
+−−−−−−−−−−−−−−+
Then after:
arr.splice(0, 1);
the array no longer has a reference to it, but member still does:
+−−−−−−−−−−−−−−+
member:Ref8612−−−−−−−−−−−−−−−−−−−−−−−>| (object) |
+−−−−−−−−−−−−+ +−−−−−−−−−−−−−−+
arr:Ref4875−−−−−−>| (array) | | id: 1 |
+−−−−−−−−−−−−+ | name: "John" |
| 0: Ref4631 |−−+ +−−−−−−−−−−−−−−+
+−−−−−−−−−−−−+ |
|
|
|
|
| +−−−−−−−−−−−−−−+
+−−>| (object) |
+−−−−−−−−−−−−−−+
| id: 1 |
| name: "John" |
+−−−−−−−−−−−−−−+
I have these data in mysql.
| code | date | value |
| A | 2016-04-04 00:00:00 | 0.1 |
| B | 2016-04-04 00:00:02 | 0.5 |
| C | 2016-04-04 00:00:05 | 1 |
| A | 2016-04-04 00:11:00 | 0.2 |
| B | 2016-04-04 00:12:25 | 0.6 |
| C | 2016-04-04 00:15:30 | 0.4 |
and i want adjust and send a data to json using nodejs/expressjs
with daily, monthly, yearly sum.
First i try this query.
SELECT date, value FROM 'table'
WHERE code = 'A'
AND date >= '2016-04-05 00:00:00'
GROUP BY DATE_FORMAT(date, "%y-%m-%d-%H") // or "%y-%m-%d", "%y-%m"
ORDER BY date;
this query runs well. but this is wasteful i think.
because repeatedly querying for number of codes.
Output:
+---------------------+-------+
| date | value |
+---------------------+-------+
| 2016-04-05 00:01:56 | 0 |
| 2016-04-05 01:01:56 | 0 |
| 2016-04-05 02:01:58 | 0 |
| 2016-04-05 03:01:57 | 0 |
| 2016-04-05 04:01:58 | 0 |
| 2016-04-05 05:01:58 | 0 |
| 2016-04-05 06:01:59 | 0 |
| 2016-04-05 07:01:58 | 0 |
| 2016-04-05 08:01:58 | 0 |
| 2016-04-05 09:01:59 | 0 |
| 2016-04-05 10:01:59 | 0.009 |
| 2016-04-05 11:02:00 | 0.007 |
+---------------------+-------+
Can i get all data with one query?
Finally, i want send this form of data with expressjs.
[
A: [{ date: '2016-04-04 00:00:00', sum: 4},
{ date: '2016-04-04 00:01:00', sum: 6}],
B: [{ date: '2016-04-04 00:00:00', sum: 4},
{ date: '2016-04-04 00:01:00', sum: 6}]
]
or this form
[
{ date: '2016-04-04 00:00:00', A: 4, B: 4 },
{ date: '2016-04-04 00:01:00', A: 4, B: 4 }
]
i've try mysql query, sequelize, lodash.
but i can't find right way.
You can use a subquery to format your date so you can group on it. Example with daily sums:
SELECT code, date, sum(value) as value
FROM (select code, DATE_FORMAT(date, "%y-%m-%d") as date, value from t1) as t2
GROUP BY code, date
ORDER BY date
The result:
"A";"16-04-04";"0.30000000447034836"
"B";"16-04-04";"1.100000023841858"
"C";"16-04-04";"1.4000000059604645"