How to clear old data in angular - angular11

I have created timesheet module , here 1st column (3, 3, 3, 3, 3) when ever I entered all it's ok but again when ever I entered 2nd column (5, , , , 5) the three row is not filled me but when ever I submit this it is getting all the rows like this(5,3,3,3,5). i this 1st column data is getting the empty things.
below is payload code:
dayListDetail: [{workdate: "2021-11-29", hoursspent: 5}, {workdate: "2021-11-30", hoursspent: 3},…]
0: {workdate: "2021-11-29", hoursspent: 5}
1: {workdate: "2021-11-30", hoursspent: 3}
2: {workdate: "2021-12-01", hoursspent: 3}
3: {workdate: "2021-12-02", hoursspent: 3}
4: {workdate: "2021-12-03", hoursspent: 5}
here is Angular code:component.ts file
for (var i=0; i<counter; i++){
dayArr = [];
if (this.tform.value.timesheet[i].mon != null && this.tform.value.timesheet[i].mon != ''){
monObj = {
workdate: arr[0].date,
hoursspent: Number(this.tform.value.timesheet[i].mon)
}
console.log('Hi');
console.log(monObj);
}
if (this.tform.value.timesheet[i].tue != null && this.tform.value.timesheet[i].tue != ''){
tueObj = {
workdate: arr[1].date,
hoursspent: Number(this.tform.value.timesheet[i].tue)
}
console.log(tueObj);
}
if (this.tform.value.timesheet[i].wed != null && this.tform.value.timesheet[i].wed != ''){
wedObj = {
workdate: arr[2].date,
hoursspent: Number(this.tform.value.timesheet[i].wed)
}
console.log(wedObj);
}
if (this.tform.value.timesheet[i].thur != null && this.tform.value.timesheet[i].thur != ''){
thursObj = {
workdate: arr[3].date,
hoursspent: Number(this.tform.value.timesheet[i].thur)
}
console.log(thursObj);
}
if (this.tform.value.timesheet[i].fri != null && this.tform.value.timesheet[i].fri != ''){
friObj = {
workdate: arr[4].date,
hoursspent: Number(this.tform.value.timesheet[i].fri)
}
console.log(friObj);
}
dayArr.push(monObj, tueObj, wedObj, thursObj, friObj);
tmpPayloadObj = {
timesheetId: this.tform.value.timesheet[i].timesheetId!=undefined?this.tform.value.timesheet[i].timesheetId:0,
projectid: this.tform.value.timesheet[i].project,
taskid: this.tform.value.timesheet[i].task,
dayListDetail: dayArr,
totalHours: 0
}
finalArr.push(tmpPayloadObj)
}

Related

trying to run a 3axes gantry in sequence mode similar to the sequence of serial data sent

i want to run my 3 axes gantry(stm32) in sequence similar to the sequence of serial data sent from the pc but this is not happening. no data is lost as all the axes perform their task but in wrong sequence.
this is the recieve complete callback loop
`void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(r != ',')
{
Rx[i++] = r;
}
else
{
strncpy(s1,Rx,i);
Home = strrchr(s1, 'H');
StringX = strrchr(s1, 'X');
StringY = strrchr(s1, 'Y');
StringZ = strrchr(s1, 'Z');
StringW = strrchr(s1, 'W');
if(Home[0] == 'H' && Home[1] == 'O' && Home[2] == 'M' && Home[3] == 'E')
{
homeValue = 1;
}
if(StringX[0] == 'X')
{
MoveX= substr(StringX,1,8);
MoveXvalue = (atol(MoveX))/100;
SpeedX = substr(StringX,8,12);
SpeedXvalue = atol(SpeedX);
insertintoQueue_X(MoveXvalue, SpeedXvalue);
insertintoSequenceQueue (1);
}
if(StringY[0] == 'Y')
{
MoveY= substr(StringY,1,8);
MoveYvalue = (atol(MoveY))/100;
SpeedY = substr(StringY,8,12);
SpeedYvalue = atol(SpeedY);
insertintoQueue_Y(MoveYvalue, SpeedYvalue);
insertintoSequenceQueue (2);
///goes on for other axes
/
i = 0;
}
}`
i tried sequencing these received cmds with below loop(posting only for one axis)
`void insertintoQueue_X (int j, int l)
{
uint8_t queuefull[] = "XFULL";
if(countXmove == 256 || countXspeed == 256 )
{
HAL_UART_Transmit_IT (&huart1, queuefull, sizeof (queuefull));
}
Xmovequeue[countXmove] = j ;
countXmove++;
Xspeedqueue[countXspeed] = l;
countXspeed++;
}
void removefromQueue_X()
{
//uint8_t queueEmpty[] = "XEMPTY";
/*if(countXmove == 0 || countXspeed == 0)
{
HAL_UART_Transmit_IT (&huart2, queueEmpty, sizeof (queueEmpty));
}*/
currentXmoveelement = Xmovequeue[0];
currentXspeedelement = Xspeedqueue[0];
for(int i = 0; i < countXmove - 1; i++)
{
Xmovequeue[i] = Xmovequeue[i+1];
Xspeedqueue[i] = Xspeedqueue[i+1];
}
}`
while loop is as below
` removefromSequenceQueue();//only called once in while loop
for(int p = 0; p < countXmove ; p++)
{
if(currentSequence == 1)
{
removefromQueue_X();
if(currentXmoveelement >0)
{
//uint8_t Xmovvestatus[] = "XMMM";
if(CurrentXposition != currentXmoveelement)
{
if(CurrentXposition > currentXmoveelement )
{
currentmoveX = CurrentXposition - currentXmoveelement;
motorMoveTo(currentmoveX,0,currentXspeedelement,20,SMSPR_X,SMS_X,SMDPR_X,SMD_X);
}
else if(CurrentXposition < currentXmoveelement )
{
// HAL_UART_Transmit_IT (&huart1, Xmovvestatus, sizeof (Xmovvestatus));
currentmoveX = currentXmoveelement - CurrentXposition ;
motorMoveTo(currentmoveX,1,currentXspeedelement,20,SMSPR_X,SMS_X,SMDPR_X,SMD_X);
}
CurrentXposition = currentXmoveelement;
currentXmoveelement = 0;
currentmoveX = 0;
MoveXvalue=0;
}
}
}
}

Can someone explain the mistake in this code? Leetcode 44 Wildcard Matching

Can Someone explain what is wrong in this code ?
It is failing on testcase s = "aa" and p = "*".
I have followed recursion and dynamic programming code here
Leetcode 44 Wildcard Matching
class Solution {
public boolean isMatch(String s, String p) {
int n = s.length();
int m = p.length();
int[][] dp = new int[n][m];
for(int[] it : dp)
Arrays.fill(it, -1);
return solve(n-1 , m-1, s ,p , dp);
}
public boolean solve(int i, int j, String s, String p, int[][] dp){
if(i < 0 && j < 0) return true;
if(i < 0 && j >=0){
while(j>=0){
if(p.charAt(j) + "" == "*") j--;
else return false;
}
return true;
}
if(j < 0 && i >=0) return false;
if(dp[i][j] != -1){
if(dp[i][j]==1) return true;
return false;
}
if(s.charAt(i) == p.charAt(j) || p.charAt(j) + "" == "?"){
boolean temp = solve(i-1,j-1,s,p,dp);
if(temp == false) dp[i][j] = 0;
else
dp[i][j] = 1;
return temp;
}
if(p.charAt(j) + "" == "*"){
boolean temp = solve(i-1,j,s,p,dp) || solve(i,j-1,s,p,dp);
if(temp == false)
dp[i][j] = 0;
else
dp[i][j] = 1;
return temp;
}
dp[i][j] = 0;
return false;
}
}

Dart sort / compare nullable datetime

I'm trying to compare list of music with releaseDate. But I can retrieve music without releaseDate and when I want to sort them, I got an error.
How can I sort / compare nullable datetime and put null releaseDate to the end?
_followedMusic.sort((a, b) {
if (a.releaseDate != null && b.releaseDate != null)
return a.releaseDate.compareTo(b.releaseDate);
else
// return ??
});
Thank you
If you take a look at the documentation for compareTo:
Returns a value like a Comparator when comparing this to other. That is, it returns a negative integer if this is ordered before other, a positive integer if this is ordered after other, and zero if this and other are ordered together.
https://api.dart.dev/stable/2.10.0/dart-core/Comparable/compareTo.html
So your compareTo should just result in returning the values -1, 0 or 1 according to if the compared object should be before, the same position or after the current object.
So in your case if you want your null entries to be at the start of the sorted list, you can do something like this:
void main() {
final list = ['b', null, 'c', 'a', null];
list.sort((s1, s2) {
if (s1 == null && s2 == null) {
return 0;
} else if (s1 == null) {
return -1;
} else if (s2 == null) {
return 1;
} else {
return s1.compareTo(s2);
}
});
print(list); // [null, null, a, b, c]
}
Or if you want the null at the end:
void main() {
final list = ['b', null, 'c', 'a', null];
list.sort((s1, s2) {
if (s1 == null && s2 == null) {
return 0;
} else if (s1 == null) {
return 1;
} else if (s2 == null) {
return -1;
} else {
return s1.compareTo(s2);
}
});
print(list); // [a, b, c, null, null]
}
Or, as #lrn suggests, make the last example in a more short and efficient way (but maybe not as readable :) ):
void main() {
final list = ['b', null, 'c', 'a', null];
list.sort((s1, s2) => s1 == null
? s2 == null
? 0
: 1
: s2 == null
? -1
: s1.compareTo(s2));
print(list); // [a, b, c, null, null]
}
what about _followdMusic.map((date) => return date ?? 1900.01.01).toList().sort(...)
the date is pseudo code, not sure how to write it. This way you put all unknown dates at one of the ends of the list.
The answer of #julemand101 also can be used with the extension function.
extension DateEx on DateTime? {
int compareToWithNull(DateTime? date2) {
if (this == null && date2 == null) {
return 0;
} else if (this == null) {
return -1;
} else if (date2 == null) {
return 1;
} else {
return this!.compareTo(date2);
}
}
}

compare Binary Tree in Golang. my answer is wrong

I am going to compare Binary Tree in Golang.But my answer is wrong. Need the third eye to help.
Thanks.
package main
import(
"fmt"
)
type TreeNode struct {
val int
left *TreeNode
right *TreeNode
}
func isSameTree(p *TreeNode , q *TreeNode ) (bool){
if p == nil && q == nil {
return true
}
if p != nil && q == nil{
return false;
}
if p ==nil && q != nil {
return false;
}
if (p.val == q.val) && (isSameTree(p.left,q.left)) && (isSameTree(p.right ,q.left)){
return true;
} else {
return false;
}
}
func main(){
p := &TreeNode{val: 1}
p.left = &TreeNode{val: 2}
p.right = &TreeNode{val: 3}
q := &TreeNode{val: 1}
q.left = &TreeNode{val: 2}
q.right = &TreeNode{val: 3}
isSame := isSameTree(p,q)
fmt.Println("is same?: ", isSame)
}
Go playground link for this code:
https://play.golang.org/p/mTX3aBxh6_
This line has a small mistake;
if (p.val == q.val) && (isSameTree(p.left,q.left)) && (isSameTree(p.right ,q.left)){
It should be;
if (p.val == q.val) && (isSameTree(p.left,q.left)) && (isSameTree(p.right ,q.right)){
If you don't see the difference in the second call to isSameTree you're passing q.left when it is supposed to be q.right.
Updated go play; https://play.golang.org/p/ul9ijG9HLc

Core Plot: only works ok with three plots

I am adding a scatter plot to my app (iGear) so when the user selects one, two or three chainrings combined with a cogset on a bike, lines will show the gears meters.
The problem is that Core Plot only shows the plots when three chainrings are selected. I need your help, this is my first try at Core Plot and I'm lost.
My code is the following:
iGearMainViewController.m
- (IBAction)showScatterIpad:(id)sender {
cogsetToPass = [NSMutableArray new];
arrayForChainringOne = [NSMutableArray new];
arrayForChainringTwo = [NSMutableArray new];
arrayForChainringThree = [NSMutableArray new];
//behavior according to number of chainrings
switch (self.segmentedControl.selectedSegmentIndex) {
case 0: // one chainring selected
for (int i = 1; i<= [cassette.numCogs intValue]; i++) {
if (i <10) {
corona = [NSString stringWithFormat:#"cog0%d",i];
}else {
corona = [NSString stringWithFormat:#"cog%d",i];
}
float one = (wheelSize*[_oneChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
float teeth = [[cassette valueForKey:corona] floatValue];
[cogsetToPass addObject:[NSNumber numberWithFloat:teeth]];
[arrayForChainringOne addObject:[NSNumber numberWithFloat:one]];
}
break;
case 1: // two chainrings selected
for (int i = 1; i<= [cassette.numCogs intValue]; i++) {
if (i <10) {
corona = [NSString stringWithFormat:#"cog0%d",i];
}else {
corona = [NSString stringWithFormat:#"cog%d",i];
}
float one = (wheelSize*[_oneChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
//NSLog(#" gearsForOneChainring = %#",[NSNumber numberWithFloat:one]);
float two = (wheelSize*[_twoChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
[cogsetToPass addObject:[NSNumber numberWithFloat:[[cassette valueForKey:corona]floatValue]]];
[arrayForChainringOne addObject:[NSNumber numberWithFloat:one]];
[arrayForChainringTwo addObject:[NSNumber numberWithFloat:two]];
}
break;
case 2: // three chainrings selected
for (int i = 1; i<= [cassette.numCogs intValue]; i++) {
if (i <10) {
corona = [NSString stringWithFormat:#"cog0%d",i];
}else {
corona = [NSString stringWithFormat:#"cog%d",i];
}
float one = (wheelSize*[_oneChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
float two = (wheelSize*[_twoChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
float three = (wheelSize*[_threeChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
[cogsetToPass addObject:[cassette valueForKey:corona]];
[arrayForChainringOne addObject:[NSNumber numberWithFloat:one]];
[arrayForChainringTwo addObject:[NSNumber numberWithFloat:two]];
[arrayForChainringThree addObject:[NSNumber numberWithFloat:three]];
}
default:
break;
}
ScatterIpadViewController *sivc = [[ScatterIpadViewController alloc]initWithNibName: #"ScatterIpadViewController" bundle:nil];
[sivc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
sivc.records = [cassetteNumCogs integerValue];
sivc.cogsetSelected = self.cogsetToPass;
sivc.chainringOne = self.arrayForChainringOne;
sivc.chainringThree = self.arrayForChainringThree;
sivc.chainringTwo = self.arrayForChainringTwo;
[self presentViewController:sivc animated:YES completion:nil];
}
And the child view with the code to draw the plots:
ScatterIpadViewController.m
#pragma mark - CPTPlotDataSource methods
- (NSUInteger)numberOfRecordsForPlot: (CPTPlot *)plot {
return records;
}
- (NSNumber *)numberForPlot: (CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
switch (fieldEnum)
{
case CPTScatterPlotFieldX:
return [NSNumber numberWithInt:index];
break;
case CPTScatterPlotFieldY:{
if ([plot.identifier isEqual:#"one"]==YES) {
//NSLog(#"chainringOne objectAtIndex:index = %#", [chainringOne objectAtIndex:index]);
return [chainringOne objectAtIndex:index];
}else if ([plot.identifier isEqual:#"two"] == YES ){
//NSLog(#"chainringTwo objectAtIndex:index = %#", [chainringTwo objectAtIndex:index]);
return [chainringTwo objectAtIndex:index];
}else if ([plot.identifier isEqual:#"three"] == YES){
//NSLog(#"chainringThree objectAtIndex:index = %#", [chainringThree objectAtIndex:index]);
return [chainringThree objectAtIndex:index];
}
default:
break;
}
}
return nil;
}
The error returned is an exception on trying to access an empty array.
2012-11-15 11:02:42.962 iGearScatter[3283:11603] Terminating app due to uncaught exception 'NSRangeException', reason: ' -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
First throw call stack:
(0x1989012 0x1696e7e 0x192b0b4 0x166cd 0x183f4 0x1bd39 0x179c0 0x194fb 0x199e1 0x43250 0x14b66 0x13ef0 0x13e89 0x3b5753 0x3b5b2f 0x3b5d54 0x3c35c9 0x5c0814 0x392594 0x39221c 0x394563 0x3103b6 0x310554 0x1e87d8 0x27b3014 0x27a37d5 0x192faf5 0x192ef44 0x192ee1b 0x29ea7e3 0x29ea668 0x2d265c 0x22dd 0x2205 0x1)*
libc++abi.dylib: terminate called throwing an exception
Thank you!
Solved! I saw the light while riding on bicycle.
- (NSNumber *)numberForPlot: (CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
if (([chainringOne count]!=0) && ([chainringTwo count]==0) && ([chainringThree count]==0)) {
switch (fieldEnum) {
case CPTScatterPlotFieldX:
return [NSNumber numberWithInt:(index+1)];
break;
case CPTScatterPlotFieldY:{
if ([plot.identifier isEqual:#"one"]) {
return [chainringOne objectAtIndex:index];
}
}
default:
break;
}
}else if (([chainringOne count]!=0) && ([chainringTwo count]!=0) && ([chainringThree count]==0)){
switch (fieldEnum) {
case CPTScatterPlotFieldX:
return [NSNumber numberWithInt:(index+1)];
break;
case CPTScatterPlotFieldY:{
if ([plot.identifier isEqual:#"one"]) {
return [chainringOne objectAtIndex:index];
}else if ([plot.identifier isEqual:#"two"]){
return [chainringTwo objectAtIndex:index];
}
}
default:
break;
}
}else if (([chainringOne count]!=0) && ([chainringTwo count]!=0) && ([chainringThree count]!= 0)){
switch (fieldEnum) {
case CPTScatterPlotFieldX:
return [NSNumber numberWithInt:(index+1)];
break;
case CPTScatterPlotFieldY:{
if (([chainringOne count] !=0) && ([plot.identifier isEqual:#"one"])) {
return [chainringOne objectAtIndex:index];
}else if ((chainringTwo || chainringTwo.count) &&([plot.identifier isEqual:#"two"] == YES )){
return [chainringTwo objectAtIndex:index];
}else if ((chainringThree || chainringTwo.count)&& ([plot.identifier isEqual:#"three"] == YES)){
return [chainringThree objectAtIndex:index];
}
default:
break;
}
}
}
return nil;
}

Resources