I'm trying to convert a string value to number based on an enum.But even when the enum matches.The if block is never hit.This is the code
let item = this.gaparameterlist.find(x => x.key === globalparameter);
if (globalparameter == enums.IASamplingGlobalParameters.PercentageTolerance || globalparameter == enums.IASamplingGlobalParameters.MarkDifferenceMethodThreshold || globalparameter == enums.IASamplingGlobalParameters.TailedThreshold, globalparameter == enums.IASamplingGlobalParameters.AvergaeDiffMethodTolerance || globalparameter == enums.IASamplingGlobalParameters.ModerationTest2Tolerance) {
var numericvalue = +textvalue;
numericvalue = numericvalue / 100;
textvalue = String(numericvalue);
}
This is the output from debugger
Related
I'm trying to make a search engine for my database.
I start with mapping all the elements and then I have a filter for some of the things in each elements like this:
const filteredCars = (carsGet.filter(item => {
const filter = (item.make.toLowerCase().includes(searchfield.toLowerCase()) ||
item.make.toLowerCase().includes(searchfield.toLowerCase()) === '')
&& (item.year > min || min === '')
&& (item.year < max || max === '')
&& (item.color === color || color === '');
console.log(filter);
return filter
}))
The filter works fine as long as it's just one item, but as soon as I send an array to color it returns false, if I set initialState to color: [] and change color === '' to color === [] they return false from start.
I have created a handler for the color:
handleChange (e) {
var options = e.target.options;
var value = [];
for (var i = 0, l = options.length; i < l; i++) {
if (options[i].selected) {
value.push(options[i].value);
}
}
console.log(value)
this.setState({color: value});
}
The console.log returns ["guld", "red"] but the filter returns false for all elements even if i only select one color
You can check if color is an array or a string and return a boolean accordingly.
const filteredCars = (carsGet.filter(item => {
const checkColor = () => {
if(Array.isArray(color)) {
return (color.length === 0) || color.includes(item.color);
} else {
return (color === '') || (color === item.color);
}
}
const filter = (item.make.toLowerCase().includes(searchfield.toLowerCase()) ||
item.make.toLowerCase().includes(searchfield.toLowerCase()) === '')
&& (item.year > min || min === '')
&& (item.year < max || max === '')
&& checkColor();
console.log(filter);
return filter
}))
I have strings that I have to insert in a db but I want to first modify their value if they fall under certain conditions.
For example I have the strings Epatite, Epatite B, EpatiteáB, EpB3 that I want them to be changed to EP B before being inserted into the db.
This is piece of my code:
// vaccines[index] is the string to compare
var vac = makeUniform(vaccines[index]);
const queryInsert = {
text: 'INSERT INTO coverages (vaccine) VALUES ($1) ON CONFLICT DO NOTHING;',
values: [vac]
}
var printText = '[INSERT Italy IN coverages]';
promises.push(postgreSQLlib.query(queryInsert, printText, false));
function makeUniform(val) {
if(val === 'DIF' || val === 'Difterite') {
return 'DIPH'; // diphtheria
}
else if(val === 'Epatite' || val === 'Epatite B' || val === 'EpatiteáB' || val === 'EpB3') {
return 'EP B'; // hepatitis B
}
else if(val === 'HIB' || val === 'Hib3' || val === 'Hib') {
return 'HIB'; // haemophilus influenzae B
}
else {
return val;
}
}
Whene I execute SELECT DISTINCT vaccine FROM coverages ORDER BY vaccine; on psql shell, I get:
DIPH
DT-DTP3
DTP3
EP A
EP B
EpatiteáB
Hib
HIB
M-MPR1
M-MPR1-MPRV ...
There is EpatiteáB which theoretically should have changed in EP B.
Why it doesn't work?
EDIT 1
vaccines[index] comes from an online pdf of which I did web scraping using the textract package of Node.js.
Thanks
Try to clean your development database first with this:
UPDATE coverages set vaccine = 'EP B' WHERE vaccine LIKE 'Epatite%' OR vaccine = 'EpB3';
Do something similar for the others.
Try this added one more condition = (val==="Epatite%E1B%21")
function makeUniform(val) {
if(val === 'DIF' || val === 'Difterite') {
return 'DIPH'; // diphtheria
}
else if(val === 'Epatite' || val === 'Epatite B' || val==="Epatite%E1B%21" || val === 'EpatiteáB' || val === 'EpB3') {
return 'EP B'; // hepatitis B
}
else if(val === 'HIB' || val === 'Hib3' || val === 'Hib') {
return 'HIB'; // haemophilus influenzae B
}
else {
return val;
}
}
Here is some problems with it.
I would like to ask you how to cope with this problem? Actually I'm new to Javascript.
Thank you in advance.
It looks like this on the console:
Javascript_ Error in user.js TypeError: Cannot read property 'value' of undefined
function checkPassword(form) {
var password = checkPassword(password);
var s_letters = "qwertyuiopasdfghjklzxcvbnm";
var b_letters = "QWERTYUIOPLKJHGFDSAZXCVBNM";
var digits = "0123456789";
var specials = "!##$%^&*()_-+=\|/.,:;[]{}";
var is_s = false;
var is_b = false;
var is_d = false;
var is_sp = false;
for (var i = 0; i < password.length; i++) {
if (!is_s && s_letters.indexOf(password[i]) != -1) is_s = true;
else if (!is_b && b_letters.indexOf(password[i]) != -1) is_b = true;
else if (!is_d && digits.indexOf(password[i]) != -1) is_d = true;
else if (!is_sp && specials.indexOf(password[i]) != -1) is_sp = true;
}
var rating = 0;
var text = "";
if (is_s) rating++;
if (is_b) rating++; //
if (is_d) rating++; //
if (is_sp) rating++; //
if (password.length < 6 && rating < 3) text = "Bad";
else if (password.length < 6 && rating >= 3) text = "Good";
else if (password.length >= 8 && rating < 3) text = "Good";
else if (password.length >= 8 && rating >= 3) text = "Excellent";
else if (password.length >= 6 && rating == 1) text = "Bad";
else if (password.length >= 6 && rating > 1 && rating < 4) text = "Good";
else if (password.length >= 6 && rating == 4) text = "Excellent";
console.log(text); // Alert will not work for us here. Console.log allows you to export data to the console for troubleshooting.
return text;
}
var player = GetPlayer(); // Gets the player object.
var myPassword = player.GetVar("SystemDate"); // Gets the value for myPassword
console.log(myPassword);
var newValue = checkPassword(myPassword); // Run the function and return to newValue
player.SetVar("SystemDate",newValue); // Set the value of newValue back to Storyline
I've changed your function in a little, try that one (and show us SetValue function):
// #first error - password input, not the form
function checkPassword (password) {
var expr,
rating = 0,
rules = {
// Lower case
'/[a-z]+/g': false,
// Upper case
'/[A-Z]+/g': false,
// Numbers
'/[0-9]+/g': false,
// Special symbols
'/[^\w\s]/gi': false
};
for (expr in rules) {
if (password.match(expr)) {
// rules[expr] = true; // - can be used to show the proper message of invalidation
rating++;
}
}
return
|| password.length >= 8 && rating >= 3 && "Excellent"
|| password.length < 6 && rating >= 3 && "Good"
|| password.length >= 8 && rating < 3 && "Good"
|| password.length >= 6 && rating > 1 && rating < 4 && "Good"
|| "Bad";
}
// Gets the player object.
var player = GetPlayer();
// Gets the value for myPassword
var myPassword = player.GetVar("SystemDate");
// Run the function and return to newValue
var newValue = checkPassword(myPassword);
// Set the value of newValue back to Storyline
// #second error seems to be in the function SetVar
player.SetVar("SystemDate",newValue);
I'm trying to create a script where I validate a phone number without too much regex in my scripts. So far I have:
var phone = document.PizzaForm.phone.value;
var num = [1,2,3,4,5,6,7,8,9,0];
var delim = ["(" , ")" , "-" , "."];
var incr = 0;
var status = 0;
if (document.PizzaForm.phone.value.substring() = num) {
incr++;
return;
}
if (incr < 10) {
var statustext=1;
alert("Phone data is missing.");
}
if (document.PizzaForm.phone.value.substring[0,4,8] != num || document.PizzaForm.phone.value.substring[0,4,8] != delim) {
(status var statustext=1;
alert("Phone data is incorrect.");)
}
if (statustext == 0) {
return true;
}
else {
return false;
}
}
but it's not working. I'm trying to increment my incr everytime there is a number so if incr < 10, i get a message that there aren't enough numbers. Anyone see where I might be going wrong?
In your below if condition :
if (document.PizzaForm.phone.value.substring[0,4,8] != num || document.PizzaForm.phone.value.substring[0,4,8] != delim) {
(status var statustext=1;
alert("Phone data is incorrect.");)
}
Declare your statustext set globally. Not inside the if condition.
status set to 1 directly.
change your logic as below
var statustext = 0;
if (document.PizzaForm.phone.value.substring[0,4,8] != num || document.PizzaForm.phone.value.substring[0,4,8] != delim) {
status = 1;
statustext = 1;
alert("Phone data is incorrect.");)
}
I'm trying to do some extremely simple form validation, my current problem is that my window.onload function doesn't call in the function I specify.
When I watch the flow of logic with firebug it just skips to the end of the code.
Here is an example of my code:
window.onload = init;
function init() {
var regForm = document.getElementById("registerform");
regForm.onsubmit = validatepostcode();
}
function validatepostcode() {
var postCode = document.getElementById("postcode");
var postCodeStr = postCode.charAt(0);
var state = document.getElementById("state");
var result = true;
if (postCodeStr == 3 || 8 && state == "Vic") {
result = true;
} else if (postCodeStr == (1 || 2) && state == "NSW") {
result = true;
} else if (postCodeStr == (4 || 9) && state == "QLD") {
result = true;
} else if (postCodeStr == 0 && state == "NT" || state == "ACT") {
result = true;
} else if (postCodeStr == 6 && state == "WA") {
result = true;
} else if (postCodeStr == 5 && state == "SA") {
result = true;
} else if (postCodeStr == 7 && state == "TAS") {
result = true;
} else
result = false;
if (result = false) {
alert("Your postcode does not match your state")
}
}
Five problems:
In init, you have this:
regForm.onsubmit = validatepostcode();
That calls validatepostcode and puts its return value in onsubmit. You probably meant to put the function itself it, not its return value in. Remove the parentheses:
regForm.onsubmit = validatepostcode;
In validatepostcode, you're fetching elements like this:
var postCode = document.getElementById("postcode");
…but then try to use them as values, like this:
var postCodeStr = postCode.charAt(0);
But an element and the current value of that element are not the same thing. More likely, you meant to retrieve the value on the first line:
var postCode = document.getElementById("postcode").value;
Same goes for state.
In validatepostcode, you have lines like this:
} else if (postCodeStr == (1 || 2) && state == "NSW") {
Specifically, 1 || 2 won't work like that. It will look at them like booleans and say, “one or two? well, they're both truthy…true it is!” and you'll essentially be doing
} else if (postCodeStr == true && state == "NSW") {
(Actually, it uses 1, not true, since the first operand was truthy, but that's not the important point here.)
Instead of using that abbreviated notation, you'll have to write it out longhand:
} else if ((postCodeStr == 1 || postCodeStr == 2) && state == "NSW") {
You mixed up = and == here:
if(result=false){
= will set result to false and leave the condition always false. Change it to == to test equality:
if(result==false){
You probably meant to return result at the end to prevent the form from being submitted when there is a validation error. With the other changes applied, you'd get an alert if there was a validation error, but it'd go on submitting anyway. As such, add a return result at the end of the validatepostcode function.