This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 7 years ago.
<script language="Javascript">
function monthlyPayment (form) {
var down = form.dPayment.value;
var trade = form.tradeIn.value;
var totalDown = down + trade;
alert ("Total down is " + totalDown);
}
</script>
This is a beginner question as I'm new to Javascript... but I am just trying to make a mock up of a feature I'm trying to implement on my website.
I have a form with two user definable variables for 'Down Payment' and 'Trade-In'. Everything in the code works, except for when it "add's" the numbers (such as $100 + $200), it doesn't output $300, but instead $100200. When I change the sign to multiplication it outputs a correct value.
What am I missing? Is there some .sum or .math code I need to implement? Or is my entire script screwed?
Thank you all for your time and help.
This is a very common mistake people new with javascript make.
the + sign is used to concatenate in javascript, which explains your result is 100200 when you try. What you give him is string, so he just concatenates the two.
use the Number() function to make sure their types become "Number" so your addition will work correctly.
var totalDown = Number(down) + Number(trade);
Related
This question already has answers here:
Does a JavaScript bookmarklet need to be a valid URL?
(2 answers)
How to create a URL from a string that replaces special characters?
(1 answer)
Closed last month.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
I wrote a relatively simple program designed to calculate annual compound interest, and while it worked in google chrome's console, when I converted it into a bookmark it would still give me the prompts, yet not the final result. I was wondering if I am supposed to format it differently, or if this is impossible.
Here is my code:
var pv = prompt('principal');
var rate = prompt('rate');
rate = rate/100;
var time = prompt('time');
var fv = pv*(1 + rate)**time
alert(fv);
As a bookmarklet it will all be on a single line, so you need to have a semicolon after this line:
var fv = pv*(1 + rate)**time;
Then it will work.
This question already has answers here:
Remove first character from a string if it is a comma
(4 answers)
Closed 3 years ago.
My website uses product references in this style: "R202020"
I want them to be shown like this for the users of my website: "BA2202020"
So basically I'm looking for a script, which formats the style of my reference numbers (should affect a ".reference" class I've created) by:
Removing the "R" in the original reference - replacing it with a "BA2" in stead - leaving the rest as it is (the "202020" part).
How can I do this?
Find 1st character of your string using string[0] and replace that with your desire value like below.
var string=$('.YourClass').text();
var result = string.replace(string[0],'BA2');
$('.YourClass').text(result);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class='YourClass'>R202020</span>
Try replace method. https://www.w3schools.com/jsref/jsref_replace.asp
'R202020'.replace('R2','BA2') // BA202020
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 4 years ago.
Having a bad day. This one has stumped me all morning. All the solutions I've found have stopped one step short of where I need to go.
I have a legacy PHP/JS app that I'm working on. Rather than trying to explain it, I'll just show what I need to do.
<?php
$phpDate_1 = new Date($someDate);
$phpDate_2 = new Date($someOtherDate);
//...There are a bunch of these
$phpDate_n = new Date($endOfTime);
<script language="javascript">
function myFunction() {
var line = aUserSelection; //an int from user which tells me what date to use
//Next line is the problem. I'm trying to pull the month from the appropriate PHP date into the JS variable.
var theMonth = "<?php echo $phpDate_" + line + "->getMonth();?>";
}
</script>
?>
I must have tried 20-30 combinations of single and double quotes, escapes, dots, pluses, and so on, but I keep getting errors over the "line" part. Unexpected character, encapsed strings, etc.
Hoping someone can point me in the right direction because my brain is fried at this point. Answers in pure JS and PHP only please because that's how the app is built. Thanks.
You need to close your php (?>) before outputting the javascript to fix the syntax error that you got.
However, with that said, you are trying to incorporate the javascript line variable into the variable name for $phpDate, to generate something like $phpDate_1.
If you don't want to go with an AJAX solution, your best bet would be to output each line's date into a javascript array. This is strongly discouraged, but if this is a legacy application that you cannot make many changes to, this might be your only option.
This question already has answers here:
convert string to a number in javascript
(4 answers)
Closed 8 years ago.
I'm currently making a simple calculator for use in an intro to Javascript lesson. I would like to know if there's a simpler way of taking the value from a HTML input textbox as an integer than having to use the Javascript parse() method?
HTML:
<input type = "Text" id="num1">
JS:
var num1 = parseInt(document.getElementById("num1").value);
You can force a string to be a 32-bit integer like this:
var num1 = ~~document.getElementById("num1").value;
You can, alternatively, accept a number with a possible fractional part with
var num1 = +document.getElementById("num1").value;
In both cases, you can check to see if the original string really did look like a number by using isNaN():
if (isNaN(num1)) {
// bad input
}
This question already has answers here:
Replace function not replacing [duplicate]
(2 answers)
Closed 8 years ago.
I have written a simple code in a .js file (in Zend framework, but I dont think it matters)
var location = "localhost:8080/mymodule/id/1#";
location.replace(/\#/g, "");
alert(location.valueOf());
return;
but I dont know why I can not see the result I want.
I get my page url and want to omit all number signs appears in it. but the code above does nothing.
please help
location is a bad name to use for a variable since it collides with the window.location variable used for the actual browser page location.
If you change location to loc in your above code, and then also add loc = in front of the loc.replace() call (since replace() doesn't modify the input, but instead returns the new version), your code works.
replace will not change the value of the original string, you need to assign the result to a new variable -
var newString = location.replace(/#/g, "");
alert(newString);
Demo - http://jsfiddle.net/5H5uZ/
It can be done in one line. This is the result you look for?
alert("localhost:8080/mymodule/id/1#".replace(/#/g,''));
//=> alerts 'localhost:8080/mymodule/id/1'