add a string to a constant in react - reactjs

I have a react code as shown below which prints the following o/p on console.
React Code:
if(type === "/page/chapter") {
console.log(trailFR); // Line A
}
Line A prints the following o/p on console:
/a2-fr
/a1-fr
/world-2020
Problem Statement:
I am wondering what changes I need to make in the react code above so that it prints the following output:
/chapters/a2-fr
/chapters/a1-fr
/chapters/world-2020
This is what I have tried but I am not getting the expecting output:
if(type === "/page/chapter") {
trailFR = {"/chapters"+trailFR}
console.log(trailFR); // Line A
}

You can do it in several ways
Template literal
console.log(`/chapters${trailFR}`)
or using String concatenation
console.log('/chapters'+trailFR)
or using concat method of a string
console.log('/chapters'.concat(trailFR.toString()))

Related

How to access methods from array element?

I've recently taken upon myself to add setter and getter methods to my class.
Since doing this, many parts of my code got broken and I'm unable to access getter methods.
Take the example below:
private loadInputs() : Input[] {
var inputs = <Input[]>this.get('inputs');
inputs.sort((a,b) => a.QuoteRef().localeCompare(b.QuoteRef()))
return( inputs || [] );
}
My input class has 2 variables,
_Project: string
_line: string
Which I access using a method QuoteRef()
public QuoteRef(): string {
return this._Project.concat('-' + this._Line.toString().padStart(3,'0'));
}
Whenever I try to access a method or a getter from my class on an item that is casted as an Input, I can see the variables (though not access them as they are private), but the prototype section doesn't contain any of the methods.
This triggers the following error in the website console:
TypeError: a.QuoteRef is not a function
What am I doing wrong?
Update
I got it to work by updating the code as follows:
inputs.sort((a,b) => {
let first = new Input(a);
let second = new Input(b);
return first.QuoteRef().localeCompare(second.QuoteRef());
});
Without seeing your complete class I can only guess, but I think that a and b in your sort are not of the type you expect. I can't see what this.get('inputs') does, but I suspect it is not returning an array with Input class objects. Hence the function cannot be found (is not a function). You could try:
inputs.sort((a,b) => {
console.log(typeof a);
console.log(typeof b);
a.QuoteRef().localeCompare(b.QuoteRef());
})
and check what the type is. Then check what your this.get actually returns.
Edit: forgot to mention that your IDE probably does not warn you because you cast the output of this.get to <Input[]>.

TypeScript array find() method saying, "No overload matches this call"

Problem Statement
I am using TypeScript's find() method for an array, but when I input a value, it does not work and says, "No overload matches this call".
Code
addOption(event: MatChipInputEvent) {
const input = event.input;
const value = event.value;
// Makes sure that the same element is not inserted in the the options array
if (value !== this.options.find(value)) {
// Add our option
if ((value || '').trim()) {
this.options.push(value.trim());
}
}
// Reset the input value
if (input) {
input.value = '';
}
}
Explanation of Code
As you can see, I am using the find method in the first if condition to check and prevent the same element from coming into the array. The value you see there has a red underline and gives me the error, "No overload matches this call". I seem to be doing everything syntactically correct but the error is still there.
Actual Results
The find method gives me the error, "No overload matches this call" and does not accept the value in the parameters.
Expected Results
The find method to take in the value and find the element in the array.
Instead of find, you should use includes.
if (!this.options.includes(value)) {
In case you really want to use find, it can be done as follows:
if (this.options.find(o => o === value) == undefined) {

How to match the Code of barcode with array codes?

I want a matching array has codes
I've match one code it works without a problem
this code is works well
let code = "code"
if metadataObj.stringValue == code {
println("the code is true")
}else {
println("the code is false")
}
But when I try this code
var codes = ["a","b","c"]
if metadataObj.stringValue == codes {
println("the code is true")
}else {
println("the code is false")
}
This problem appears
cannot invoke == with an argument list of type
It works well with array var codes = ["a","b","c"] But when you put array of analysis JSON local file is not working
A string cannot be equal to an array.
If you want to test if the string is equal to one of the array elements
then use contains():
if contains(codes, metadataObj.stringValue) { ... }

How to use verifyText for comparing two strings?

As we know, assert continues the execution but verify stops the execution the moment the script fails.
e.g suppose two string abc, abd xyz
i want to verify the two strings. How to verify them without using Assert.assertEquals(expected, actual);
Can anyone please guide me for the same?
You can verify the 2 strings by creating a method and using if else to compare them.
In your example instead of assert this can be done
public void compareMethod()
{
string expected = "abc";
string actual = "xyz";
if(expected == actual)
{
//do steps required
}
else
{
}
}
If you have already stored strings and want to verifyText using Selenium WebDriver then the following code will surely help..
if("expected String".equals("actual string"))
{
System.out.println("String matches ");
}
else
{
System.out.println("String does not match ");
}
Try this

in the dark about ajax instructions on arrays inside Autocomplete widget

I have an Autocomplete working with geonames.org cities in the source: option and want to manipulate some of the arrays in the results. I have a "hard coded" test version running, but am having trouble manipulating the array variables to turn it into something useful.
A short statement of the problem is I can't get the alert statements to output readable strings. I get [object, Object] types of output. I need readable strings in the arrays for other code (not shown) to work. But other problems are: the Firebug Console output does not occur, and the Console gives me the following error statement - Error: Permission denied to access property 'item.autocomplete' - from line 2 of jQuery. This does not happen with the hard coded test. This is my first time using .grep and I'm not comfortable with .map, so I'm pretty sure the problems are array manipulations in those 3 sections.
Here's some relevant code. All the variables are declared, but I don't show all the declarations below.
citiesWithBoroughs = //a global array variable, populated in a prior select: option
source: function (request, response){
$.ajax({
success: function ( data ){
var geonamesResponse=$.map(data.geonames, function (item){
return {
label: item.name //and others
}
}
alert(citiesWithBoroughs + "," + citiesWithBoroughs.length + "|cWB2" ); //displays correct info
var noBoroughs=$.grep( geonamesResponse, function ( item, i ) {
for (var i=0; i < citiesWithBoroughs.length; i++ )
if( item.label === citiesWithBoroughs[i] ){ //compare geonamesResponse to each citiesWithBoroughs
console.log(citiesWithBoroughs[i]); //nothing in Console
return false; //drop any matches out of the geonamesResponse array
}
noBoroughs = $.map( data.geonames, function (item){ return item.label; });
console.log(noBoroughs); //nothing appears in Console
return true;
});
alert(noBoroughs.length + "," + citiesWithBoroughs.length + "," + geonamesResponse.length + "|3lengths" ); //correct info
alert(noBoroughs + "|nB" ); //in test, shows correct number of [object,Object] but no data
if( noBoroughs.length != geonamesResponse.length ){
var dropdownsCityWithBoroughs = $.grep( geonamesResponse, function ( item, i ) {
for (var i=0; i<citiesWithBoroughs.length; i++ )
if(item.label === citiesWithBoroughs[i]){return false;}
return true;
}, true )//true inverts grep to return the has-Boroughs city in the dropdown
alert(dropdownsCityWithBoroughs + "|dCWB"); //contains object, Object, but shows no data
}
}
}
}
I'm a novice so please give specific comments and code. I don't follow general instructions well.
The short statement of my problem was I could not read the output of my alerts, which contain arrays. Turns out JSON.stringify() fixes that. Other problems, like the Console.log statements not showing continue, but I can deal with that mysterious problem when the alerts work.

Resources