how to fix nan in javascript? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 days ago.
Improve this question
I can't show result in table and another code same problem result nan and number for result how to remove word nan
plz help me.
totalIron = (total *374.15)
totalIron2 = totalIron.toLocaleString("en-US", {style:"currency", currency:"SAR"})
totalIron22 = totalIron2
document.getElementById('resultForIron').innerHTML = totalIron22;
function toTotal() {
return +totalIron22 + +`ironjs22` + +cement111 + +`blockjs11`;
};
`totalSecaion22` = (totalIron22 + toTotal());
document.getElementById('result2').innerHTML = `totalSecaion22`;

NaN is returned when you try to do something for which a numeric value is not possible. For example, trying to parse a string that doesn't represent a number or taking the square root of a negative number.
If you want to replace NaN with some other value, you could test the result of your operation to check if that result is NaN. The test would look like this:
let isItANumber = !isNaN(x);
console.log(isItANumber); // prints 'false'
But you can't force the result to be a number.

Related

Javascript returns and calling another function question [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 16 days ago.
This post was edited and submitted for review 16 days ago.
Improve this question
I need to write code to
Use a prompt() to gather an initial dollar amount from the user and then pass that value into a calculation function(already created) called calculation(). Then I need to take the return of that function and pass it to the current function. The returned, properly formatted value (the calculation function formats numbers), should appear in an alert() box.
Function format(){
cashAmount = Number(prompt("Enter amount to be formatted"));
}
This is as far as I've gotten. I'm confused at the function returning another function.
Try using these lines.
If the boolean parameter separatedByComma is true we ara going to use the Number method toLocalString("en-US") return the number separated by commas. If the parameter is false it's return a foramted number without commas "$533535".
const ashAmount = Number(prompt("Enter amount to be formatted"));
function format(ashAmount, separatedByComma) {
return separatedByComma
? `$${ashAmount.toLocaleString("en-US")}`
: `$${ashAmount}`;
}
format(ashAmount, true);

JS concatenate numbers with operator sign [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
Maybe tittle is not too descriptive, but how do I get this kind of result
var string = 11+'-'+10
// expected result 11-10
console.log(string);
Every time I try to do the above I get 1, or whatever the result of the subtraction is
I will be a little bit clear about this. What I want to do with this is generate a button with onclick like this:
onClick = method(1,[11-10, 12-10])
method(id,...array){
console.log(array)
//result [1,2]
}
even if inspecting the button actually shows the correct output
In your first example, you use
11+'-'+10
In the second one, you use
11-10
There is a clear difference
Using the first method in the second code will work as expected
method(1,[11+'-'+10, 12+'-'+10])
To make it shorter just use strings
method(1,['11-10', '12-10'])

How can I extract this exact string in regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to get only the value of "vp" from the query string only if it is between 0-4. If it is bigger (for example: 5, 12 etc.) I want to get undefined.
"?vp=324" // ==> undefined
"?vp=1&mn=345" // ==> 1
"?mn=345&vp=2" // ==> 2
"?sf=23&vp=12&fd=343" // ==> undefined
May or may not need to parseInt for this.
function smallVP(queryString) {
let params = new URLSearchParams(queryString);
let vp = parseInt(params.get("vp"), 10);
if (vp >= 0 && vp <= 4) {
return vp;
}
}

How to use regular expressions to say that you need to take the value of Categories = .... & Search = ..... & [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have this line.
let x = "Categories=All&Search=hat&ListPage=15&Page=1";
How to use regular expressions to say what you need to take from the beginning of a line and up to 2 characters &.Categories=All&Search=hat&
Use split and pass the limit argument, then just reconstruct it back into a string (if you really need the & - if not, discard everything after the split):
let x = "Categories=All&Search=hat&ListPage=15&Page=1";
const res = x.split("&", 2).map(e => e + "&").join("");
console.log(res);
You can use /([^&]*&){2}/g regex to match what you want.
let x = "Categories=All&Search=hat&ListPage=15&Page=1";
let y = x.match(/([^&]*&){2}/g);
console.log(y);

Javascript - Show all telephone numbers in a range [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
If I have phone number ranges such as 5555555555-5599 5555550000-0003 5555550005-0007, my mission is to have it return all results without using a server. Is it possible for it to return without a server a result that would look like:
5555555555
5555555556
5555555557
/* etc */
My previous post about javascript has helped me up to this point but I wanted to rehaul the whole site.
Javascript dashes in phone number
If you could point me in the right direction, I would really appreciate it. I'm just having a mind block right now if this is even possible.
Given a single phone range in the form of "xxxxxxyyyy-zzzz", split the whole string on the dash and the first part of the string at the 6th index. This yields three strings "xxxxxx", "yyyy", and "zzzz". Using a for loop, you can create an array of phone numbers by concatenating the prefix "xxxxxx" onto the range "yyyy"-"zzzz":
// Get an array from a given range "xxxxxxyyyy-zzzz"
function fromRange(range) {
var phoneNumArray = [];
var prefix = range.substring(0,5);
var suffixRange = range.split("-");
for (var suffix = suffixRange[0].substring(4, -1);suffix < suffixRange[1];suffix++) {
phoneNumArray.push(prefix + suffix);
}
return phoneNumArray;
}
Try it in JSFiddle.

Categories