If/Else statement not working, giving undefined - reactjs

I'm working on a project, and I need the English version of countries' names. I'm trying to have an if/else statement that will give the English spelling when the non-English spelling of a country is given, but it's not working. I would really appreciate any help or advice on why I am having this issue. Thank you!
let countryName;
if (country === 'Shqipëria' || 'hqipëria') {
let countryName ='Albania';
}
else if (country === 'Hayastán' || 'hayastán') {
let countryName ='Armenia';
}
...
else if (country === 'Italia' || 'italia') {
let countryName ='Italy';
}
...
else if (country === 'Türkiye' || 'türkiye') {
let countryName = 'Turkey';
}
else if (country === 'Ukraїna' || 'Україна' || 'ukraїna' || 'yкраїна') {
let countryName = 'Ukraine';
}
else {
let countryName = country;
}
console.log(countryName)
console.log(country)
console.log(country) gives Italia, but console.log(countryName) is undefined.
I have also tried doing this:
const [countryName, setCountryName] = useState('')
if (country === 'Shqipëria' || 'hqipëria') {
setCountryName('Albania');
}
else if (country === 'Hayastán' || 'hayastán') {
setCountryName('Armenia');
}
...
else if (country === 'Italia' || 'italia') {
setCountryName('Italy');
}
...
else if (country === 'Türkiye' || 'türkiye') {
setCountryName('Turkey');
}
else if (country === 'Ukraїna' || 'Україна' || 'ukraїna' || 'yкраїна') {
setCountryName('Ukraine');
}
else {
setCountryName(country);
}
console.log(countryName)
console.log(country)
But this was giving me an error stating Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.

as mentioned in other answers your conditions only contain one equality check, so it wold always return true because a string's value by-itself is truthy, so you never get passed the first condition. for simplifying if statements, you can use the includes method. for example:
if (['Ukraїna','Україна','ukraїna','yкраїна'].includes(country))
and definitely declare country variable once & re-assign it in every condition.
let countryName;
if (...) {
countryName = "x"
}

Cancerning your first code: countryname shouldn't be declared many times
so your code should be like this:
let countryName;
if (country === 'Shqipëria' || country === 'hqipëria') {
countryName ='Albania';
}
else if (country === 'Hayastán' || country === 'hayastán') {
countryName ='Armenia';
}
else if (country === 'Italia' || country === 'italia') {
countryName ='Italy';
}
else if (country === 'Türkiye' || country === 'türkiye') {
countryName = 'Turkey';
}
else if (country === 'Ukraїna' || country === 'Україна' || country === 'ukraїna' || country === 'yкраїна') {
countryName = 'Ukraine';
}
else {
countryName = country;
}
console.log(countryName);
console.log(country);
Also, you should use
if(variable === 'value' || variable === 'value')
instead of
if (variable === 'value' || 'value')

Related

Apex Class to Update Stage of Opportunity by Completeing Field on the record

I've been editing an Apex Class that is supposed to update the Opportunity.StageName when specific fields are completed, however no matter what I do I can't seem to get the field to update to 'Booked'. All stages preceding and proceeding work eg. If I update the fields that meet the 'Closed Won' criteria it updates. It just appears to be that particular Stage.
There doesn't seem to be any issues with the formatting so I'm at a loss as to why it doesn't work.
{
opp.Override_Stage__c = true;
opp.StageName = 'Candidates';
if((opp.Site_Requirements_Sent_To_Client__c == 'Yes' && opp.Client_Confirmed_Requirements__c == 'Yes') || opp.Site_Requirements_Sent_To_Client__c == 'N/A')
{
opp.StageName = 'Quote';
if(opp.Quote_Accepted__c == 'Yes')
{
opp.StageName = 'Awaiting Invoice';
if(opp.Invoice_Created__c == 'Yes')
{
opp.StageName = 'Invoiced';
if(opp.PO_Received__c == 'Yes')
{
opp.StageName = 'Awaiting Payment';
if(opp.Payment_In_Full_Received__c == 'Yes')
{
opp.StageName = 'Paid';
if(opp.Training_Provider__c== 'Online')
{
opp.StageName = 'Booked';
if(opp.Surveys_Printed__c == 'Yes')
{
opp.StageName = 'Closed Won';
}
}
}
}
}
}
else if(opp.Quote_Accepted__c == 'No')
{
opp.StageName = 'Closed Lost';
}
}
}
Constants are Case-sensitive. I think you should check the field value of opp.Training_Provider__c. If it is predefined better to copy the API name of the value in code.

hi i have a problem whit my handleSubmit in react js

I want to submit some data but if the textarea (name) is empty or null i want to use the value of the select to search, but for some reason my if/else doesn't work and always search using the name even if its empty and this gives an error beaacause it's empty
e.preventDefault();
if (name != null || name !== '') {
try {
setNameIntroduced(true);
let cardFound = completeArrayCards.filter((e) => e.name.toLowerCase() === name.toLowerCase());
setCardFoundIndividual(cardFound);
let arrayImages = Object.keys(cardFound[0].image)
.map(function (key) {
return cardFound[0].image[key];
});
if (cardFound[0].name !== null) {
setFound(true);
setName(cardFound[0].name);
setType(cardFound[0].type);
setImage(arrayImages[1]);
setAttribute(cardFound[0].attribute || cardFound[0].type);
setDescription(cardFound[0].desc);
setLevel(cardFound[0].level);
setRace(cardFound[0].race);
setAtk(cardFound[0].atk);
setDef(cardFound[0].def);
setCardsets(cardFound[0].card_sets);
setCardPrices(Object.keys(cardFound[0].card_prices)
.map(function (key) {
return cardFound[0].card_prices[key];
}));
if (type === 'Spell Card' || type === 'Trap Card') setIsMagicTrap(true)
}
} catch (err) {
console.log(err);
setFound(false);
}
} else if (name == null && type != null) {
try {
setMultipleResults(true);
setFound(true);
setNameIntroduced(false);
let cardsFound = completeArrayCards.filter((e) => e.type === type);
//console.log(cardsFound);
console.log('Multiple found: ' + cardsFound[0].name + '\n' + cardsFound[0].type + '\n' + cardsFound[0].image.image_url);
} catch (err) {
console.log(err);
setFound(false);
}
}
}```

ReactJS - Validate Nested-IF/ELSE Statement

In my react app I'm using IF statement inside another IF statement to validate a condition that I want to get data filtered only from a specific column not from all.
Basically I'm using AntD table in which I'm displaying students data. In a table I do have few columns namely Age, Marks and Attendance. On each column(Age, Marks and Attendance) I'm applying a range filter with aim to fetch student(s) when applied search on a specific column i.e. either Age, Marks or on Attendance.
The problem I'm getting that my Nested-IF condition is not validating to true. Rest of code is working fine. I also tried to run code without Nested-IF(I mean with the simple IF/ELSE statement). That is also working fine.
I don't know what did I wrong or what I'm missing/skipping in my attempt.
Help me to validate my Nested-IF/ELSE statement(s).
What I'm trying is:
const handleSearch = (firstInput, secondInput, dataIndex) => {
setSearchStu(
student.filter((obj) => {
if (dataIndex === "age") {
if (obj.age >= firstInput && obj.age <= secondInput) {
return true;
} else {
return false;
}
} else {
if (dataIndex === "marks") {
if (obj.marks >= firstInput && obj.marks <= secondInput) {
return true;
} else {
return false;
}
} else {
if (dataIndex === "attendance") {
if (obj.attendance >= firstInput && obj.attendance <= secondInput) {
return true;
} else {
return false;
}
} else {
return false;
}
}
}
})
);
};
Where
firstInput is the 1st input field in a range filter
secondInput is the 2nd input field in a range filter
dataIndex is like a key to validate my 1st IF statement in Nested-IF case
Values "age", "marks" and "attendance" are the defined values which are uniquely correspond to the each columns(Age, Marks and Attendance) in the table
and so on..
Thank you in advance for your assistance.
to simplfy your code, you can try this :
const handleSearch = (firstInput, secondInput, dataIndex) => {
setSearchStu(
student.filter((obj) => {
if (dataIndex === "age") {
return (obj.age >= firstInput && obj.age <= secondInput)
} else {
if (dataIndex === "marks") {
return (obj.marks >= firstInput && obj.marks <= secondInput)
} else {
if (dataIndex === "attendance") {
return (obj.attendance >= firstInput && obj.attendance <= secondInput)
} else {
return false;
}
}
}
})
);
};
or like this :
switch(dataIndex){
case "age" :
return (obj.age >= firstInput && obj.age <= secondInput)
break;
case "marks" :
return (obj.marks >= firstInput && obj.marks <= secondInput)
break;
case "attendance" :
return (obj.attendance >= firstInput && obj.attendance <= secondInput)
break;
default :
return false
}
The function does work given the right inputs and data - I made a sandbox on replit to verify. Possibly there is an issue with the student data (consider renaming to students) or with the inputs to the function. Also, the function can be simplified to the following (see replit sandbox):
const handleSearch = (firstInput, secondInput, dataIndex) => {
return (
student.filter((obj) => {
if (["age", "marks", "attendance"].includes(dataIndex)) {
return obj[dataIndex] >= firstInput && obj[dataIndex] <= secondInput
}
return false
})
);
};

typescript filter array on 2 parameters

In order to filter an array on possibly 2 parameters I have written the following code:
filterStudies(searchString?: string) {
if (searchString && !this.selectedModalityType) {
this.studies = this.fullStudyList.filter(function (study) {
return (study.Code.toUpperCase().includes(searchString.toUpperCase())) ||
(study.Description.toUpperCase().includes(searchString.toUpperCase()));
})
} else if (!searchString && this.selectedModalityType) {
console.log(this.selectedModalityType)
this.studies = this.fullStudyList.filter(function (study) {
return (study.ModalityType.Code.toUpperCase() === this.selectedModalityType.toUpperCase())
})
} else if (searchString && this.selectedModalityType) {
this.studies = this.fullStudyList.filter(function (study) {
return (study.Code.toUpperCase().includes(searchString.toUpperCase())) ||
(study.Description.toUpperCase().includes(searchString.toUpperCase())) &&
(study.ModalityType.Code.toUpperCase() === this.selectedModalityType.toUpperCase())
})
}
}
filterStudies(searchString?: string) is called when typing in a textbox that.
The other way of filtering could be by selecting a value from a dropdown box. Achieved by this code:
handleSelection(value:any){
this.selectedModalityType = value;
console.log(value)
this.filterStudies()
}
All works fine until this code is hit:
this.studies = this.fullStudyList.filter(function (study) {
return (study.ModalityType.Code.toUpperCase() === this.selectedModalityType.toUpperCase())
})
Error message : ERROR TypeError: Cannot read property 'selectedModalityType' of undefined, I see it is actually logged in the line before.
What am I missing??
Thanks,
In your funtcion, this is not the same this as the line before.
This will work:
let self = this;
this.studies = this.fullStudyList.filter(function (study) {
return (study.ModalityType.Code.toUpperCase() === self.selectedModalityType.toUpperCase())
})
You can read this to learn more: https://github.com/Microsoft/TypeScript/wiki/%27this%27-in-TypeScript
The this keyword in JavaScript (and thus TypeScript) behaves differently than it does in many other languages. This can be very surprising, especially for users of other languages that have certain intuitions about how this should work.
(...)
Typical symptoms of a lost this context include:
A class field (this.foo) is undefined when some other value was expected

Ag-Grid Filter Comma

im using ag-Grid, but there is a issue when it filters my data, when i filter my data in the price column, it only works with numbers dot and not with commas.
Link: https://plnkr.co/edit/LDdrRbANSalvb4Iwh5mp?p=preview
Practical Example:
In the Price column select box equal and above insert "1.5" and than try inserting "1,5"
This is because this filter is a native one.
If you want to handle custom behaviour, define your own filter.
Documentation : https://www.ag-grid.com/angular-grid-filtering/index.php
A quick and dirty solution would be to monkey patch the NumberFilter like this :
NumberFilter.prototype.doesFilterPass = function (node) {
if (this.filterNumber === null) {
return true;
}
var value = this.valueGetter(node);
if (!value && value !== 0) {
return false;
}
var valueAsNumber;
if (typeof value === 'number') {
valueAsNumber = value;
}
else {
valueAsNumber = parseFloat(value.replace(',','.'));
}
switch (this.filterType) {
case EQUALS:
return valueAsNumber === this.filterNumber;
case LESS_THAN:
return valueAsNumber < this.filterNumber;
case GREATER_THAN:
return valueAsNumber > this.filterNumber;
default:
// should never happen
console.warn('invalid filter type ' + this.filterType);
return false;
}
};
Then changed line is here :
valueAsNumber = parseFloat(value.replace(',','.'));
So i found the problem, first i had to convert the value has a string than i needed to replace the dot by the comma, the problem with the answer above was first because of the data type and than the order of the properties of the replace function, but the problem now is that is not filtering correctly, if i search using equal option if gives me 2 values, instead a fixed one, code looks something like this:
Code:
NumberFilter.prototype.doesFilterPass = function (node) {
if (this.filterNumber === null) {
return true;
}
var value = this.valueGetter(node);
if (!value && value !== 0) {
return false;
}
var valueAsNumber;
if (typeof value === 'number') {
value = value.toString()
valueAsNumber = parseFloat(value.replace('.',','));
}
else {
valueAsNumber = parseFloat(value.replace('.',','));
}
switch (this.filterType) {
case EQUALS:
return valueAsNumber === this.filterNumber;
case LESS_THAN:
return valueAsNumber < this.filterNumber;
case GREATER_THAN:
return valueAsNumber > this.filterNumber;
default:
// should never happen
console.warn('invalid filter type ' + this.filterType);
return false;
}
};
Possible Solution:
NumberFilter.prototype.onFilterChanged = function () {
var filterText = utils_1.default.makeNull(this.eFilterTextField.value);
if (filterText && filterText.trim() === '') {
filterText = null;
}
var newFilter;
if (filterText !== null && filterText !== undefined) {
console.log(filterText);
// replace comma by dot
newFilter = parseFloat(filterText.replace(/,/g, '.'));
console.log(newFilter);
}
else {
newFilter = null;
}
if (this.filterNumber !== newFilter) {
this.filterNumber = newFilter;
this.filterChanged();
}
};

Resources