Getting to know if '*' is included in a string? [closed] - javascript

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 1 year ago.
Improve this question
I have a string variable and I want to know if the multiplication symbol is in the string. This is what I did:
const str = "45*33"
const arr = str.split('')
if(arr.includes("*"){
console.log("Symbol found!")
}
else{
console.log("Error")
})
This does not work, is there a way I can make this work?

You don't need to split at all; you can directly use String#includes.
const str = "45*33";
if (str.includes("*")) {
console.log("Symbol found!")
} else {
console.log("Error")
}

Use the console of your browser to check for errors.
It is a simple syntax error at:
if(arr.includes("*"){
should be:
if(arr.includes("*")){

Working perfectly now, it was a typo in the code that my eye just did not see. There is a reason why I had to split the string to an array. I could have just used the str.includes().
const str = "45*33"
const arr = str.split('')
if(arr.includes("*")){
console.log("Symbol found!")
}
else{
console.log("Error")
})
Thank you.

Related

undefine keep on appearing [closed]

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 7 months ago.
Improve this question
I have been battling with this code since couple of days and its giving me issue. the other functions are working perfectly except this EQUAL function.
Below is the code
equal.addEventListener("click", function(e){
if (screen.value = "") {
screen.value = "";
} else{
console.log(screen.value);
let answer = eval(screen.value);
screen.value = answer;
}
});
The first thing jumping out to me is that you're using the assignment operator rather than comparison in the if statement. Maybe this resolves your issue:
if (screen.value == "") {
screen.value = "";
} else{

? Incorrect regex expression [closed]

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 2 years ago.
Improve this question
I’ve been trying to do find if a variable contains a digit inside of it. And for some reason, I put (exp.) ‘Majorpassword09’ and it just prints a false?
// vars
var regex = new RegExp(‘.*\d.*’)
var str = ‘Scorpio08’
// main
function reg1() {
if (regex.test(str)){
console.log(‘true’);
}else{
console.log(‘false’);
}
}
reg1();
EDIT: I’ve put the title as Incorrect regex expression because that's the only thing that I think is wrong.
You're most likely on a mac.
Your code shows that you're using fancy quotes. Like these ones: ‘’. You need to use normal quotes, like ', and ".
Here's a solution to your problem.
// vars
var regex = new RegExp(".*\\d.*")
var str = "Scorpio08"
// main
function reg1() {
if (regex.test(str))
{
console.log("true");
}else{
console.log("false");
}
}
reg1();
Whatever text editor you're using, it has fancy quotes on. Consider looking in the settings, or on Google, to find out how to disable them.
as you are using string to create a RegExp Object thus have to use two backslashes to properly escape
var regex = new RegExp(‘.*\\d.*’)
or
var regex = /.*\d.*/

Why does comparing objects values return false? [closed]

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 3 years ago.
Improve this question
I am creating a email checker to see if the email inputed on the front end is excactly the same email in the backend. I have managed to get both the front and back end emails but everytime i check them it return false, even though they are the same values. I am using Object.is to checck if the vaues are the same,I've also tried the typical (data.email === user.email ) approach but it stills returns false even though, the values are the same.
router.get('/checkemail',(req,res)=>{
let data = req.query;
console.log(data)
User.findOne({
username: data.username,
},function(error,user){
if(error){
res.status(401).send(error)
} else if(Object.is(user.email, data.email)) {
res.status(200).send(true)
} else {
res.status(401).send(false)
}
})
})
Can you try use trim() function on each param of the object.is ?
Object.is(user.email.trim(), data.email.trim())

How to Change or replace a part of URL string in javascript? [closed]

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 4 years ago.
Improve this question
I have one url which contains special character like
#1# or #2#
http://10.10.10.10:8000/admin/taskinstance/?flt1_dag_id_equals=#1#&flt2_state_equals=#2#
I want to change with some other value like in place of #1# I need "hello" and In place of #2# I need "there", I tried with some code, but it's not working
var str = response.data;
var mapObj = {
#1#:dagId,
#2#:state
};
str = str.replace(/#1#|#2#/gi, function(matched){
return mapObj[matched];
});
In response.data I am getting same URL which I have mention above,
I am getting:
SyntaxError: Invalid or unexpected token for line no 3.
Is something is wrong in syntax?
I am fine with any syntax angularjs or javascript.
You have to change the regex a bit by wrapping strings with parenthesis. Your object is not valid as well:
var str = 'http://10.10.10.10:8000/admin/taskinstance/?flt1_dag_id_equals=#1#&flt2_state_equals=#2#';
var mapObj = {
"#1#":"dagId",
"#2#":"state"
};
str = str.replace(/(#1#)|(#2#)/gi, function(matched){
return mapObj[matched];
});
console.log(str)

If/Else only works for first if [closed]

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 8 years ago.
Improve this question
I'm working with this, but the issue is that only the first if ever functions correctly. If I change the order, each if statement works on its own, but the logic where if the first is false, then check the second, and so on isn't working. What am I missing here?
$("#search_button").click(function(){
var table = $('#main_index').DataTable();
var search_term = $("#second_select2 option:selected").text();
var first_s = $("#first_select2 option:selected").text();
if (first_s = 'District'){
table.columns(1).search(search_term).draw();
}
else if (first_s = 'Territory'){
table.columns(2).search(search_term).draw();
}
else if (first_s = 'Region'){
table.columns(0).search(search_term).draw();
}
else {
console.log('error');
}
});
If I console.log the search_term and first_s variables, I can see them changing and correctly working. And, as I said, without the if/else statements each of these works on its own.
In all your comparisons, you need to replace = by ===
You are using the assignment operator (=) in your comparisons. you should be using the identity operator: ===.
if (first_s = 'District'){
needs to be
if (first_s === 'District'){
as do the rest of the checks.

Categories

Resources