Javascript add +1 - javascript

Can someone please help me with the simplest question ever.
var rate = (month * "0.004545");
I simply need to get rate to be the above value + 1.
I just need (X * Y) + 1
All values are numbers.
Would truly appreciate this, I have already spent 1 hour.

Just add (+) and remove the "s:
var rate = (month * 0.004545) + 1;
See Arithmetic operators on MDN.
Also ensure that month is defined without "s.
EDIT: As it appears that you aren't being able to get this to work, here's a JSFiddle that demonstrates the code working for me.

var prerate = parseFloat(month) * 0.004545;
var rate = ++prerate;

Related

Trying to create javascript to seperate +1 10 digit global numbers with dashes

I've found code for 7 and 10 digit numbers but I need to separate for 11.
For example I have numbers as +13138373884 and I need them to display as +1-313-837-3884
example for 10 digit code I found...
var number = this.$el.find('#phoneNumberField').val().replace(/-/g, '');
if(number.length > 10) {
e.preventDefault();
}
if(number.length < 3) {
number = number; // just for legibility
} else if(number.length < 7) {
number = number.substring(0,3) +
'-' +
number.substring(3,6)
} else if(number.length > 6) {
number = number.substring(0,3) +
'-' +
number.substring(3,6) +
'-' +
number.substring(6,10);
}
this.$el.find('#phoneNumberField').val(number);
I realise that everyone needs to start somewhere, but if you want to learn, it's a good idea not to expect a web search to turn up code that exactly meets your requirements. Instead, spend some time understanding and adapting the code you do find.
In this case, have a look at those numbers in brackets after "substring". How might they relate to the result? What happens if you change them?
A great resource for learning JS and web programming in general is MDN. Try
If you need to looking up "substring" there.
(If you don't want to learn, and just want someone to do the work for you, pay them.)

Javascript Date/Time Parsing, UTC to Local Time Broke

I have a strange timezone/date formatting issue that recently came up with some new code, and what makes it more strange is that it only affects two months - August and September.
The code takes a date string with UTC time formatted like this:
10-06-2017 09:29:15
And converts it to a new string with the same format but with local time. The zeroPad function ensures that the format remains the same.
We implemented it in March and everything worked fine. It's within Classic ASP on IIS9/Server 2012.
As soon as we got to August, it broke. 08-10-2017 09:33:06 becomes 12-09-2016 20:33:06.
Can anyone see what I've done wrong?
function jsConvert(dateString) {
var patterns = dateString.split(/[\-\s:]/g);
var date = new Date(parseInt(patterns[2]),
parseInt(patterns[0]) - 1,
parseInt(patterns[1]),
parseInt(patterns[3]),
parseInt(patterns[4]),
parseInt(patterns[5]));
date.setTime(date.getTime() - getTimezoneOffset() * 60 * 1000);
var result = zeroPad(date.getMonth() + 1);
result += '-' + zeroPad(date.getDate());
result += '-' + date.getFullYear();
result += ' ' + zeroPad(date.getHours());
result += ':' + zeroPad(date.getMinutes());
result += ':' + zeroPad(date.getSeconds());
return result;
}
function zeroPad(number) {
return (number < 10) ? '0' + number : number;
}
What are the units of time in your getTimezoneOffset() function?
Your code is written as though the getTimezoneOffset() function returns a number of minutes, since you are multiplying by 60 and then 1000, to get millseconds.
But if your getTimezoneOffset is returning seconds, you will be over-doing the multiplication and therefore jumping back too far in time.
I think it would have to be milliseconds, to jump back the distance you are getting. #CBroe above mentions that perhaps you mean the builtin getTimezoneOffset function, which is indeed in minutes. Perhaps you have a separate getTimezoneOffset function defined in your code elsewhere, that returns an answer in milliseconds? In which case CBroe's answer fixes it.
My next suggestion would be to add lines of debugging code
For example, could you add the following?
At the beginning, add console.log("A",dateString).
After var patterns = dateString.split(/[\-\s:]/g); add a line console.log("B",patterns);.
After var date = ...(patterns[5])); add a line console.log("C",date);.
After date.setTime...1000); add a line console.log("D",date); console.log("E",getTimezoneOffset());.
If you show us the output of these lines, we should be able to pinpoint the problem easily. I have included item E because I am just wondering if there is yet another getTimezoneOffset() function in your system, which we are not aware of, or something. Seeing its value will help reassure everyone.
Meanwhile can you confirm the time zone you are running the code in? I am guessing it is in the USA rather than Europe, from your preference for putting month before the day?
So as it turns out this is a known, albeit obscure issue. It has to do with the fact that parseInt assumes that numbers with leading zeros are NOT base 10, but instead radix. It's well documented here: Javascript parseInt() with leading zeros
Once I made the change to:
parseInt(patterns[2]**, 10**);
All was good.
Thanks for the input.

How can I use an exponent in javascript

I am trying to build a simple loan calculator using jQuery for practice. I have most of the code down, but am having trouble inserting the exponent for the formula. I am aware of the Math.pow, but not sure if I am implementing it properly. Here is the loan formula:
P ( r / 12 ) / (1 - ( 1 + r / 12 ) ^-m )
P = principal
r = interestRate
-m = loan term in months
Here is my code:
var months = ("#loanTerm" * -1);
var calc = Math.pow(1 + (interestRate / 12), months);
Here is the HTML:
Loan Term(Months): <input class="userInput" id="loanTerm" type='number'>
Not sure if I am doing something wrong with the Math.pow, or if there is a way I am able to simply set the months to an integer, I'm relatively new to jQuery so any help would be much appreciated. Thanks in advance.
After the exchange of comments, I'm fairly sure the underlying question is how to access the value of a form input with jQuery.
In jQuery you can get the value of an <input> with
$("#loanTerm").val()
In vanilla JavaScript you can use
document.getElementById("loanTerm").value
You can then explicitly convert it to an integer and make it negative like so (in jQuery):
var months = parseInt($("#loanTerm").val(), 10) * (-1);
To test if months is NaN (the user didn't enter anything, or entered non-numeric data), you can use isNaN().
Finally, here is a testbed you can experiment with to confirm the equation is working as expected. I hope this helps.

Math.flooring a scientific notation at a certain decimal

I am trying to Math.floor a scientific notation, but at one point the number gets too big and my current method doesn't work anymore. This is what I am using atm
var nr = (number+"").length - 4;
if( nr > 1 ) {
nr = Math.pow( 10, nr );
number= Math.floor(number/nr)*nr;
number= number.toExponential(3);
}
When it becomes a scientific notation by default, I think that's e20+, than my .length method doesn't work anymore since the length it returns isn't accurate. I can think of a work around, and that's to find out the number after e, and update my nr to Math.floor it properly, but it seems like so much work to do something so simple. Here's an example number 8.420960987929105e+79 I want to turn this into 8.420e+79, is there a way I can Math.floor the third decimal point always, no matter what the number is? As it stands when I use toExponential(3) it always rounds the number. My numbers can get as high as e+200 easily, so I need an easier way of doing what I'm currently doing.
Edit: managed to find a work around that works besides Connor Peet's answer for anyone who wants extra options
var nr = 8.420960987929105e+79+"";
var nr1 = nr.substr(0,4);
var nr2 = nr.substr(4, nr.length);
var finalNr = Number(nr1 + 0 + nr2).toExponential(3);
This way is more of a hack, it adds a 0 after the 4th number so when toExponential rounds it up, it gets 'floored' pretty much.
I wrote a little snippet to round a number to a certain number of significant figures some time ago. You might find it useful
function sigFigs(num, figures) {
var delta = Math.pow(10, Math.ceil(Math.log(num) / Math.log(10)) - figures);
return Math.round(num / delta) * delta;
}
sigFigs(number, 3); // => 8.420e+79

Round a variable up to the next closest multiple of X

I'm looking for a way to round up a number to the next closest multiple of 250. So for example if I had the following JS:
var containerHeight = $("#container").height();
...And we imagine the value of "containerHeight" was 680px, I would want a way to round up to 750px (if the value was 1007, it should round up to 1250). I suspect this requires a solution that is more complex than I anticipate. Or perhaps jQuery has a built in function that will make this feasible?
I suppose this is more of a math question than it is a jQuery question (but my jQuery syntax knowledge is also a bit limited :)
Any ideas / bits of help are greatly appreciated,
Thanks!
containerHeight = Math.ceil(containerHeight / 250.0) * 250;
function NearestMultiple(i, j) {
alert(Math.ceil(i/ j) * j);
}
NearestMultiple(1007, 250); //returns 1250
See example at http://jsfiddle.net/SUya9/1/
Or what James said too!
EDIT: I see you wanted to round up all the time...Updated fiddle, but James got her in 1.
simple
var rounded = Math.ceil(value / round) * round;
For those that are working with integers, and want a solution that avoids intermediate floating point, use this:
int roundedUp = alignment * ((value + alignment - 1) / alignment);
or without the division:
int mod = value % alignment;
if (mod > 0)
value += (alignment - mod);

Categories