Angular2, TypeError: el.toLowerCase is not a function in Autocomplete Process - autocomplete

When I am try to integrate autocomplete in angular2, I got TypeError i.e
el.toLowerCase is not a function.
filter() {
if (this.query !== ""){
this.filteredList = this.names.filter(function(el){
return el.toLowerCase().indexOf(this.query.toLowerCase()) > -1;
}.bind(this));
}else{
this.filteredList = [];
}
}

Try return el.toString().toLowerCase().indexOf(this.query.toLowerCase()) > -1;. If the argument el is not a string, the toLowerCase() function won't work on it.

try this.. Its working for me..
filter(event: any) {
if (this.query != "") {
this.FilteredApplicationNames = this.ApplicationNames.filter(function (el) {
return el.toString().toLowerCase().indexOf(this.query.toLowerCase()) > -1;
}.bind(this));
} else {
this.FilteredApplicationNames = [];
}
}

You can use like
return !input || (el ? ('' + el).toLowerCase().indexOf(input) !== -1 : false);
This works because the object el is cast to a string using '' + el. Since it's now a string object, we can use the toLowerCase() function.

Try this.el.toLowerCase(). If the argument el being passed in is a property of the class, it might not recognize it without this. in front of it. Let me know if this doesn't work.

Related

dart null safty issue when try to add value to map element

I am using Dart but i am facing null safty issue with following code
RxMap<Product,int>cartItems=Map<Product,int>().obs;
void updateCart(Product product,String type){
if(type=="plus") {
cartItems.value[product]++;
}
else {
cartItems.value[product]--;
}
}
i got the following error message
the method '+' can't be unconditionally invoked because the receiver can be 'null'
i tried to add null check to the target as following
cartItems.value![product]++;
You can give a default value if null.
cartItems.value[product]??0 +1
Or force assert to non null value like this.It may throw exception if element not present in HashMap
cartItems.value[product]!+1
In your question you are asserting not null for HashMap not the value of the HashMap.
The problem is that cartItems.value is a Map and it's possible that cartItems.value[product] is null. In this case you can't add or remove 1 to null.
So you should do like the following:
if (type == "plus") {
cartItems.value[product] = (cartItems.value[product] ?? 0) + 1;
} else {
cartItems.value[product] = (cartItems.value[product] ?? 0) - 1;
}
Using (cartItems.value[product] ?? 0) you're saying that if cartItems.value[product] is null 0 is used instead.
Also note that in the else clause, when cartItems.value[product] == null, you're trying to remove 1 to something that doesn't exist, so in that case it may be best to throw an exception:
int? currentValue = cartItems.value[product];
if (currentValue == null) {
throw Exception('Trying to remove on a null object');
}
cartItems.value[product] = currentValue - 1;

isEnabled() function not working in Protractor

I want to select a date in calendar picker using protractor. Below is the snippet of code, that I have written in my script:
if (dat <= 24) {
var us_dat = element(by.xpath("(//a[text()='" + dat + "'])[1]"));
} else {
var us_dat = element(by.xpath("(//a[text()='" + dat + "'])[1]"));
if (us_dat.isEnabled() == false) {
var us_dat = element(by.xpath("(//a[text()='" + dat + "'])[2]"));
}
}
us_dat.click();
browser.sleep(1000);
where dat is a variable, where I have stored the date that needs to be selected in the calendar picker.
During debugging, I found that, when the date is greater than 24, the code is checking the "if" block present under the "else" block. However, it returns a value of undefined and hence it is skipping the action written inside the "if" block. May I know why it is returning a value of "undefined", instead of true or false please?
Please let me know if any further details are required.
Because all Protractor's API is Async and return promise. So isEnabled() is not return Boolean value directly, you can't directly compare it with a boolean value. You should do the comparing in then() or use await.
Another thing, you no need to put xpath into (), like (//a[text()='click me']).
var us_dat = element(by.xpath("//a[text()='" + dat + "'][1]"));
us_dat.isEnabled().then(function(enabled){
if (dat > 24 && enabled=== false) {
return element(by.xpath("//a[text()='" + dat + "'][2]"));
} else {
return us_dat;
}
})
.then(function(ele){
ele.click();
browser.sleep(1000);
});
try to use this method will work
isEnable: function (element) {
return element.isEnabled();
},

Javascript create a function that returns a boolean value based on certain parameters

Thanks for taking the time to look at my problem. What I'm trying to do is create a javascript function that tests whether a sting is a particular length and also whether each element of that string can be found in another string. The function then needs to return a boolean value of either true or false depending on whether the string is valid.
Here's what I have:
N_ALPHA = 6;
N_CHOICES = 4;
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var alphabet = ALPHABET.substring(0, N_ALPHA);
function isValidGuess(inStr)
{ var valid;
var Str = inStr;
for (i=0; i<Str.length; i++)
{ if (Str.charAt(i) === alphabet.charAt(i) && Str.length == N_CHOICES.length)
{ valid = true;
}
else
{ valid = false;
}
}
return valid;
}
This code is not working at all. It only returns false every time. Any help you could provide would be greatly appreciated. Thank you.
N_CHOICES.length return undefined, because variable N_CHOICES is number.
you have to change your condition to
if (Str.charAt(i) === alphabet.charAt(i) && Str.length == N_CHOICES)

Can someone improve my code? what's wrong?

I have a problem in compiling, it's a "unexpected type required variable; found: value" and
"incopatible types" my code is here:
`
String sCL;
BufferedReader br = null;
br = new BufferedReader(new FileReader("banned.txt"));
providerSocket = new ServerSocket(2004, 10);
connection = providerSocket.accept();
`
while((sCL = br.readLine()) != null) {
if ((br.readLine()) = (connection.getInetAddress().getHostName())) {
sendMessage("baned");
}
else {
//some code here.
}
}
Errors are in .readLine() and .getHostName()
Your code has three bugs on the same line:
Using = instead of ==
Doing a readLine() twice
Comparing Strings with ==
if (sCL.equals(connection.getInetAddress().getHostName())) {
And I'm hoping that getInetAddress() isn't NULL now! :)
Did you need a double-equals here?:
if ((br.readLine()) == (...

Checking multiple conditions for a string in C#

I want to check multiple conditions in a string in C# but it throws error saying Cannot use && for string or boolean
if ((duStart.Trim() != "" && duStart.Trim() != null) &&(duEnd.Trim() != "" && duEnd.Trim() != null))
{
//do this
}
else
//do that
The code you've given compiles fine. Here's a short but complete program - I've changed the whitespace of your line of code, but that's all:
using System;
class Test
{
static void Main()
{
string duStart = "X";
string duEnd = "X";
if ((duStart.Trim() != "" && duStart.Trim() != null) &&
(duEnd.Trim() != "" && duEnd.Trim() != null))
{
Console.WriteLine("Yes");
}
}
}
Having said that:
If you're going to use the same value (the trimmed version of duStart, for example) multiple times, there seems little point in computing it twice. I'd have used extra local variables (trimmedStart, trimmedEnd) here
Trim never returns null, so those tests are pointless.
Using string.IsNullOrWhitespace is probably a better idea here. Why bother creating strings that you're never going to use?
you can simplify the condition by writing:
if( !string.IsNullOrEmpty(duStart.Trim()) && !string.isNullOrEmpty(duEnd.Trim()) )
{
}
Check for the Null first for duStart and duEnd. Then try Trim the string. Trim cannot be applied on a null value. So, below code block should work for you.
if ((duStart != null && duStart.Trim() != "") && (duEnd != null && duEnd.Trim() != ""))
{
//do this
}
else
{
//do that
}