Simple Jquery Problems - javascript

$(function(){
var lr-lineheight = $('#sub').height();
$("#lefty,#righty").css({"line-height":lr-lineheight+'px'});
)}
So I'm not really sure what im doing wrong here, but I'm on a deadline and would like a bit of help. Please give some input.

Variables cannot be declared with a dash in them.
You can use underscores to split variables:
var my_variable = 1;
or you can use camel case:
var myVariable = 1;

Related

How to add a variable to a regex checking for a URL?

I have this part of a function which is running perfectly:
if(/https?:\/\/[a-z]{2}w?\.mywebsite\./.test(href)){
if(!firstSerp){
firstSerp = this;
add_prerender(this, href);
}
}
As you can see mywebsite is hard-coded. What I want is to put a variable there instead.
So it would look like this:
var mylink = 'mywebsite';
if(/https?:\/\/[a-z]{2}w?\.+= mylink\./.test(href)){}
One of the users suggested I look at How do you use a variable in a regular expression?
var replace = "regex";
var re = new RegExp(replace,"g");
But I have difficulties understanding how that would apply to my example.
Could you please help me solve this?
Regular expressions are intended to be used to check if an existing string matches a pattern or to find a pattern in an existing string. You cannot use them to build a string.
Instead, you should use string concatenation:
const url = 'http://www.' + mywebsite + '.com';
or a string template:
const url = `http://www.${mywebsite}.com`;

Solve Linear equation with variables in JavaScript

I am trying to use JavaScript to solve the linear equation with variables.
So my try is this:
var CE = parseFloat(document.getElementById("CE").value)
var CF = parseFloat(document.getElementById("CF").value)
var EF = parseFloat(document.getElementById("EF").value)
var x1=algebra.parse("CE^2+2*EF*x-EF^2");
var x2=algebra.parse("CF^2");
var eq= new Equation(x1,x2);
var h=eq.solveFor("x");
I know I should not put the valuable in "" mark, but I do not know where I should put them.
Please help me. Thank you!
You can use template String in ES-6 to simplify writing these complicated string.
var x1=algebra.parse(`${CE}^2+2*${EF}*x-${EF}^2`);
var x2=algebra.parse(`${CF}^2`);
var eq= new Equation(x1,x2);
var h=eq.solveFor("x");

Make Variable Text Uppercase

I'm trying to make the text stored in the variable sendSpecialChat uppercase, but I can't find out why it doesn't work. This is not actually what I'm trying to do, but I simplified the code:
var sendSpecialChat = hi;
document.writeIn(sendSpecialChat.toUpperCase());
document.getElementById('print').innerHTML = "sendSpecialChat";
<p id="print"></p>
Although, it doesn't work. What's wrong with my code?
sendSpecialChat.toUpperCase() will give you an uppercase version of the string. You have a few problems with your code, you need to use quotes(") around strings, but not around variable names.
var sendSpecialChat = "hi";
document.getElementById('print').innerHTML = sendSpecialChat.toUpperCase();
<p id="print"></p>

What is a better way to remove element from a string and join it?

Is there a better way to do this using chaining?
var url = "www.mycompany.com/sites/demo/t1"
var x = url.split('/');
console.log(x);
var y = x.pop();
console.log(y,x);
var z = x.join("/");
console.log(z);
I tried something like but wouldn't work since pop just returns the last value and not the rest:
parentUrl = self.attr('Url').split("/").pop().join("/");
You could certainly do this with a regex replacement:
var url = "www.mycompany.com/sites/demo/t1";
var z = url.replace(/\/[^\/]+$/, '');
console.log(z);
This Regex should do the trick:
var url = "www.mycompany.com/sites/demo/t1"
console.log(url.match(/^(.+)\/(.+)/));
I solved it using lodash. The _.initial() does exactly what I'm looking for.
var url = "www.mycompany.com/sites/demo/t1";
var x = _.initial(url.split("/")).join('/');
console.log(x);
The regex solutions worked as well. However, I don't know how to read regex yet so I didn't feel comfortable using something I didn't really understand. But they work so thanks.
EDIT: My project already has the lodash library so I'm not adding a library just for this :)

Can I pass multiple args (patterns) for Javascript's replace()? If not, any good alternative?

I'm taking the following ex URL https://support.dev.mysite.com/batch/ and removing everything, but the environment (eg dev). The below code works fine.
var env = endPoint.replace("https://support.", "\n");
var envClean = env.replace(".mysite.com/batch/", "\n");
I don't like repeating myself. I would like to look for both patterns in the string and remove them all at once. MDN has a good breakdown of replace() here - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/replace but it doesn't mention anything about multiple arguments.
I've tried this:
var env = endPoint.replace("https://support." && ".mysite.com/batch/", "\n");
but it just parses the second arg and disregards the first.
Does anyone have a cleaner way of doing this? I'm assuming I can search for multiple patterns via REGEX, any REGEX masters out there care to help?
Cheers.
You can use regular expressions for this:
var environment = 'https://support.dev.mysite.com/batch/'
.replace(/^https:\/\/support\.|\.mysite\.com\/batch\/$/g, '');
You could chain your method:
var envClean = endPoint.replace("https://support.", "\n").replace(".mysite.com/batch/", "\n");
Or you could use regex:
var envClean = endPoint.replace(/https:\/\/support\.|\.mysite\.com\/batch\//, "\n");
And there is another solution to get dev:
var envClean = endPoint.match(/^https:\/\/support\.([^.]*)\.mysite\.com\/batch\/$/)[1];
For this specific URL pattern, why not make it really simple and use .split():
var url = 'https://support.dev.mysite.com/batch/';
var name = url.split('.')[1];
If I were using a regular expression, I would probably do it this way:
var match = url.match( /support\.(.*)\.mysite.com/ );
var name = match && match[1];
Note that you don't have to worry about the entire URL this way, only enough to do the match.
If you know that the URL will match, you can simplify that to:
var name = url.match( /support\.(.*)\.mysite.com/ )[1];

Categories