Javascript working properly in console, but not as bookmarklet [duplicate] - javascript

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.

Related

Validate time format hh:mm:ss.ms with regEx JavaScript? [duplicate]

This question already has an answer here:
Why this javascript regex doesn't work?
(1 answer)
Closed 2 years ago.
I created function that should validate user input. The value should only be accepted as valid if format is matching this hh:mm:ss.s. Here is the function:
function time_format(time_val) {
let regEx = new RegExp('/^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(:|\.)\d{2}?$/');
console.log(time_val);
console.log(regEx.test(time_val));
};
console.log(time_format('00:00:00.0'));
console.log(time_format('05:35:23.7'));
console.log(time_format('25:17:07.0'));
All three values failed the test above. First and second format should pass the regex. The third should fail since the hours are not valid. If anyone knows how this can be fixed please let me know.
Try this…
function time_format(time_val) {
let regEx = /^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(:|\.)\d{1,2}?$/;
console.log(time_val);
console.log(regEx.test(time_val));
};
console.log(time_format('00:00:00.0'));
console.log(time_format('05:35:23.7'));
console.log(time_format('25:17:07.0'));

How to set formula to get value from specific sheet [duplicate]

This question already has answers here:
Javascript - String concatenation [duplicate]
(2 answers)
How can I do string interpolation in JavaScript?
(21 answers)
Closed 2 years ago.
I set formula in sheet1 cell (1,9) to get the value from other sheet through google apps script. Where "sheet" is a variable, I need to get value from specific sheets. The code below shows "=sheet!B1" rather than "=6!B1".
var ss1 = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxx/edit#gid=0");
var sheet1 = ss1.getSheetByName("Sheet1");
var sheet;
sheet = 6;
var sheet = sheet.toString();
sheet1.getRange(1,9).setFormula('sheet!B1')
}
Google Apps Script uses JavaScript as programming language
setFormula requires a string as parameter.
Considering the above you could use any string manipulation technique available using the JavaScript version supported by the runtime that your project is using.
Among other techniques you coul use:
String concatenation operator i.e. sheet + '!B1' (if you are using the old (Mozilla Rhino) or the new runtime (Chrome v8) )
String templates i.e. `${sheet}!B1` (only if you are using the new runtime (Chrome V8) )

JS variable inside of PHP inside of JS inside of PHP [duplicate]

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.

javascript sum user defined numbers [duplicate]

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);

omiting number sign using regex in java script not working [duplicate]

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'

Categories