I am new to web components, and am using LIT framework. I have been breaking my head trying to figure this out.
This is my function.
createObj() {
this.financingObj = [];
if (this.dropDownValue === common.ONCE) {
if (this.ammountEntered >= 2000 && this.ammountEntered <= 999999) {
this.financingObj.push(this.createFinancingStructure('BusinessLoan'));
}
if (this.ammountEntered >= 10000 && this.ammountEntered <= common.MAX_ALLOWED_VALUE) {
this.financingObj.push(this.createFinancingStructure('Leasing'));
}
if (this.ammountEntered >= 250000 && this.ammountEntered <= common.MAX_ALLOWED_VALUE) {
this.financingObj.push(this.createFinancingStructure('InvestmentLoan'));
}
if (this.ammountEntered >= 2500000 && this.ammountEntered <= common.MAX_ALLOWED_VALUE) {
this.financingObj = this.financingObj.filter(
item => item !== this.createFinancingStructure('BusinessLoan'),
);
this.financingObj.push(this.createFinancingStructure('RollOverTermLoan'));
}
} else if (this.dropDownValue === common.MULTIPLE_TIMES) {
if (this.ammountEntered >= 250000 && this.ammountEntered <= common.MAX_ALLOWED_VALUE) {
this.financingObj.push(this.createFinancingStructure('InvestmentLoan'));
}
if (this.ammountEntered >= 2500000 && this.ammountEntered <= common.MAX_ALLOWED_VALUE) {
this.financingObj.push(
this.createFinancingStructure('RollOverTermLoan'),
this.createFinancingStructure('RollOverRevolving'),
);
}
}
}
And this is my test case
it('should call createObjForInvest when invest product is selected', () => {
const spy = sinon.spy(element, 'createObj');
spy();
sinon.assert.called(spy);
});
I see the function gets called, but I am getting the following error.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Below is some code to check if somebody has won on a tic tac toe board. (board[0] - board[8] represent the tic tac toe board from top to bottom, left to right):
func checkWin(board: [Int]) -> Bool{
if board[0] != 0 {
if board[0] == board[1] && board[1] == board[2] {
return true
} else if board[0] == board[3] && board[3] == board[6] {
return true
}
} else if board[4] != 0 {
if board[1] == board[4] && board[4] == board[7] {
return true
} else if board[3] == board[4] && board[4] == board[5] {
return true
} else if board[2] == board[4] && board[4] == board[6] {
return true
} else if board[0] == board[4] && board[4] == board[8] { //
return true
}
} else if board[8] != 0 {
if board[2] == board[5] && board[5] == board[8] { //
return true
} else if board[6] == board[7] && board[7] == board[8] { //
return true
}
}
return false
}
However, the three lines with // at the end will not return true even if the conditions are met. I have noticed that they all share board[8], however I know this is not a problem with the storyboard, as if I hard code a win that satisfies one of faulty conditions it still doesn't work. Can anyone see what's going wrong?
Your logic is flawed. Once you check with the outer ifs, you have committed to just checking a few of the possible cases.
For instance, if space 0 is not empty, then you are only checking cases 0-1-2 and 0-3-6, but you aren't checking 0-4-8 so you'll miss that possibility. The 0-4-8 case is handled inside the first else if case, but you'll never get there if board[0] != 0.
You can fix this by using 3 ifs instead of the else ifs.
func checkWin(board: [Int]) -> Bool{
print(board)
if board[0] != 0 {
if board[0] == board[1] && board[1] == board[2] {
return true
} else if board[0] == board[3] && board[3] == board[6] {
return true
}
}
if board[4] != 0 {
if board[1] == board[4] && board[4] == board[7] {
return true
} else if board[3] == board[4] && board[4] == board[5] {
return true
} else if board[2] == board[4] && board[4] == board[6] {
return true
} else if board[0] == board[4] && board[4] == board[8] { //
return true
}
}
if board[8] != 0 {
if board[2] == board[5] && board[5] == board[8] { //
return true
} else if board[6] == board[7] && board[7] == board[8] { //
return true
}
}
return false
}
a little bit more compact:
func checkWin(board: [Int]) -> Bool{
let checks = [
//rows
[0,1,2],
[3,4,5],
[6,7,8],
//columns
[0,3,6],
[1,4,7],
[2,5,8],
//cross
[0,4,8],
[2,4,6]
]
for check in checks{
if board[check[0]] != 0
&& board[check[0]] == board[check[1]]
&& board[check[1]] == board[check[2]]
{
return true
}
}
return false
}
better to get also the winner-id or 0 for no winner:
func getWinner(board: [Int]) -> Int{
let checks = [
//rows
[0,1,2],
[3,4,5],
[6,7,8],
//columns
[0,3,6],
[1,4,7],
[2,5,8],
//cross
[0,4,8],
[2,4,6]
]
for check in checks{
if board[check[0]] != 0
&& board[check[0]] == board[check[1]]
&& board[check[1]] == board[check[2]]
{
return board[check[0]]
}
}
return 0
}
or if you like to put it in an enum:
enum Winner {
case none
case player(id: Int)
}
func checkWin(board: [Int]) -> Winner{
let checks = [
//rows
[0,1,2],
[3,4,5],
[6,7,8],
//columns
[0,3,6],
[1,4,7],
[2,5,8],
//cross
[0,4,8],
[2,4,6]
]
for check in checks{
if board[check[0]] != 0
&& board[check[0]] == board[check[1]]
&& board[check[1]] == board[check[2]]
{
return .player(id: board[check[0]])
}
}
return .none
}
i got stuck on this query that calculate the new column.
i cannot explain briefly just see the snippet code below.
from user in context.Table
select new
{
Total = user.Total,
Paid = user.Paid,
Balance = //should be Total - Paid to assign result
}
i have tried this query
var result = from a in context.EnrollmentRequests
where a.SchoolYear == SchoolYear
select new
{
a.StudentID,
Name = a.Student.FirstName + " " + a.Student.MiddleName + " " + a.Student.LastName,
Tuition = context.Miscs.Where(m => m.YearLevel == a.YearLevel && m.SchoolYear == SchoolYear && m.Term == a.Term && m.CourseID == a.CourseID)
.Select(ms => new { Amount = ms.Amount })
.Union(context.StudentCharges
.Where(s => s.YearLevel == a.YearLevel && s.SchoolYear == SchoolYear && s.Term == a.Term && s.CourseID == a.CourseID && s.StudentID == a.StudentID)
.Select(ss => new { Amount = ss.Amount }))
.Union(context.StudentSubjectTakes
.Where(st => st.StudentID == a.StudentID && st.SchoolYear == a.SchoolYear && st.Term == a.Term && st.YearLevel == a.YearLevel && st.EducationalLevel == a.Student.StudentAdvanceEducations.FirstOrDefault().EducationLevel)
.Select(st => new
{
Amount = context.SubjectOfferedFees
.Where(f => f.SubjectsOfferedID == st.SubjectsOfferedID).Sum(w => (decimal?)w.Cost ?? 0)
}))
.Select(f => f.Amount).Sum(),
PaymentMade = context.Payments.Where(p => p.SchoolYear == SchoolYear && p.Term == a.Term && p.StudentID == a.StudentID && p.PaymentDes == "Tuition Fee").Sum(sm => (decimal?)sm.Amount),
Balance = Tuition - PaymentMade //Does not exist on current context
};
but doesn't work it says that does not exist on current context.
how could this possible.
thanks. this will be helpful to anyone.
Balance = user.Total - user.Paid
I just updated to the non beta version of Xcode 6 (finally) and, coming from beta 5, got a few errors that I didn't get before, one being "could not find overload for '&&' that accepts the supplied arguments"
I am following a tutorial here and from another question, I know that this error is because "the expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions."
I'm a beginner; how do I break up an expression into sub-expressions?
My code:
func checkForWin(){
//first row across
var youWin = 1
var theyWin = 0
var whoWon = ["Lost":0,"Won":1]
for (key,value) in whoWon {
if ((plays[6] == value && plays[7] == value && plays[8] == value) || //across the bottom
(plays[3] == value && plays[4] == value && plays[5] == value) || //across the middle
(plays[0] == value && plays[1] == value && plays[2] == value) || //across the top
(plays[6] == value && plays[3] == value && plays[0] == value) || //down the left side
(plays[7] == value && plays[4] == value && plays[1] == value) || //down the middle
(plays[8] == value && plays[5] == value && plays[2] == value) || //down the right side
(plays[6] == value && plays[4] == value && plays[2] == value) || //diagonal
(plays[8] == value && plays[4] == value && plays[0] == value)){//diagonal
userMessage.hidden = false
youLabel.hidden = false
userMessage.text = "\(key)!"
done = true;
}
}
}
To break it up, you just need to add more ()s. Like this:
if (((plays[6] == value) && (plays[7] == value) && (plays[8] == value)) ||
((plays[3] == value) && (plays[4] == value) && (plays[5] == value)) ||
((plays[0] == value) && (plays[1] == value) && (plays[2] == value)) ||
((plays[8] == value) && (plays[4] == value) && (plays[0] == value)))
I had the code below that provide search, 4 field, the
lambda will increase huge lot for each field included in, and it is time consuming and not efficient to have such a combination search, any better way to write a proper search?
var result = db.ValuePairs
.Where(c=>
(isStatus && isText && isCode && isGroupCd)
?
c.Status == status && c.Text == text && c.Code == code && c.GroupCode == groupCd
:
(isStatus && isText && isCode)
?
c.Status == status && c.Text == text && c.Code == code
:
(isStatus && isText && isGroupCd)
?
c.Status == status && c.Text == text && c.GroupCode == groupCd
:
(isStatus && isCode && isGroupCd)
?
c.Status == status && c.Code == code && c.GroupCode == groupCd
:
(isText && isCode && isGroupCd)
?
c.Text == text && c.Code == code && c.GroupCode == groupCd
:
(isStatus && isText)
?
c.Status == status && c.Text == text
:
(isStatus && isCode)
?
c.Status == status && c.Code == code
:
(isStatus && isGroupCd)
?
c.Status == status && c.GroupCode == groupCd
:
(isText && isCode)
?
c.Text == text && c.Code == code
:
(isText && isGroupCd)
?
c.Text == text && c.GroupCode == groupCd
:
(isCode && isGroupCd)
?
c.Code == code && c.GroupCode == groupCd
:
(isStatus)
?
c.Status == status
:
(isText)
?
c.Text == text
:
(isCode)
?
c.Code == code
:
(isGroupCd)
?
c.GroupCode == groupCd
:
groupCd != ""
);
Pretty simple boolean logic there...
.Where(c =>
(!isStatus || c.Status == status)
&& (!isCode || c.Code == code)
&& (!isGroupCd || c.GroupCode == groupCd)
);