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 JS function which checks if a user entered string is zero.
if (str.legth = 0) {
alert('Provide at least 1 character to create a folder.');
return;
}
But this seems to let a user pass a zero entered string.
Is there anything missing?
THanks
You've misspelled "length" and you need "==" instead of "=" to check for equailty rather than assigning a value.
If your variable is called str, then length is used to return a boolean value. You need to use either == or === for an actual comparison instead of an assignment. Additionally, a return isn't necessary.
Related
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
So I have a function that will be called with a 1 word string.
Test("ABCD")
my code:
function Test(x)
var str=""
str.push($)
x.toLowerCase()
return str
//So I want the output to change the "ABCD" into "abcd$"
You are accepting the argument x in the function and running toLowerCase on it. The output is not having any value from the input.
Also there is no push method defined for string. It can be String.concat. Even that is not needed, you could make use of arithematic + itsel for concatenation or string leterals.
Just lowercase the input append a $ symbol. Its done!!
Im making use of .toString() method aswell. To ensure code is not breaking for other input types
It should be
function Test(x) {
return x.toString().toLowerCase() + '$'
}
console.log(Test("ABCD"));
Or Simply.
Test = (x) => `${ x.toString().toLowerCase() }$`;
console.log(Test("ABCD"));
This is just an example, please change the text which you want to add at end as you wish.
I passed the string in function through console.log but you can call the function as you wish without console.log.
function capFirst(str) {
return str.toLowerCase().concat('', '$');
}
console.log(capFirst('ABCD'));
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 wanted to compare the value of variable which is getting the URL of current page with the hard coded string. Though the values are same I am getting false in the comparison. Not able to guess what is happening. Below is the code:
var loc = String('"' + window.URL + '"');
if("Here I am specifying entire URL manually exactly same which I am getting in loc"==String(loc))
{ console.log("true") }
else { console.log("false") }
This is pretty simple but I am missing something. Help needed. Thanks in advance.
You should use window.location.href instead . Since loc variable returns "function URL() { [native code] }" Hence the result is false.
window.location.href === "<Your string>"
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'm trying to take a string and check whether or not it contains a lowercase letter or number, and then if so push that letter or number to an array.
for(let i = 0; i < datearg.length; i++)
{
log.info(datearg.charAt(i));
if(/[a-z]/.test(datearg.charAt(i))) letter_num++; letters.push(datearg.charAt(i));
if(/[0-9]/.test(datearg.charAt(i))) number_num++; numbers.push(datearg.charAt(i));
}
However, both if statements always evaluate to true and the arrays end up containing every single character in datearg. Anyone know why?
if(/[a-z]/.test(datearg.charAt(i))) letter_num++; letters.push(datearg.charAt(i));
is equivalent to
if(/[a-z]/.test(datearg.charAt(i))) { letter_num++; }
letters.push(datearg.charAt(i));
i.e. push is not conditional. This is the primary reason why many style guides heavily discourage control structures without braces (which only take a single statement).
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 6 years ago.
Improve this question
Why does 0.toFixed(2) print 0 instead of 0.00?
The correct answer:
Use a variable (noted by Rajesh in the comments)!
var num = 0
var fixedStr = num.toFixed(2);
This just looks better, is easier to understand and also safer, as it will throw errors to you if any occur.
Some Warning
Please note that some interpreters (just like the chrome console) do throw an error if you do 0.toFixed(2), as it is no valid JS to them. If you use a variable or brackets around the 0, it will be okay for them.
Another way for doing it
Also noted in the comments (by 4castle):
You can also use the following:
0..toFixed(2)
As the first dot will be interpreted as a decimal point, this will be okay for the interpreter and be parsed into "0.00".
But please do not use this, use a variable. It just looks horrible and not everyone understands what this should do (or why it magically works).
If you store 0 in a var, or if you use (0), it give 0.00.
(0).toFixed(2)
"0.00"
var x = 0;
x.toFixed(2)
"0.00"
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 years ago.
Improve this question
I want a loop that starts with i=100, and decrements by 5 on each iteration. However, this produces an error:
for(var i=100;i>=1;i-5)
{
document.write(i+"<br />");
}
But 'i=i-5' works:
for(var i=100;i>=1;i=i-5)
{
document.write(i+"<br />");
}
Why?
The question is how we can assign a variable to a variable i=i-5.I go the question answered I taught "i=i-5" was a expression the value of i variable is i-5 and no calculation happen its just a stable variable.
The answer is that it is taking a the i value and subtracting it by 5 and not assigning.
If you just write i - 5, there is no left-hand variable, which means there is nothing which is taking the value of i and subtracting it by 5. If you write i = i - 5 then you will decrement 5 since you now have a left-hand variable.