javascript adding strings together - javascript

var body = "jsonString=123";
var mybody = body.replace("jsonString=","");
console.log("mybody:"+mybody);
console.log("myhead:"+body.replace("jsonString=",""));
This this should give two equal outputs to console.log file but it doesnt!
Output:
mybody:
myhead:123
Can someone explain why variable mybody is blank?
Shouldnt it be 123?
EDIT
I am such a fool, there were some unrelated problems with my execution environment that led to this. It has nothing to do with javascript.
Thanks for the help SO

It is giving me the correct output per this jsFiddle
Results:
mybody:123
myhead:123

Related

Why is javascript variable overlapping?

My question is why I'm having issues with declaring these variables. It works fine until "priceAdj" and the only thing I can find to explain that, Is that Javascript thinks it's still part of the previous variable. But I have no idea why it would think that because isn't the comma supposed to seperate them? I know I could just declare var again, But my code won't let me pass it up.
CODE:
function myFunction() {
var average = (80),
totalDay = (100),
priceInt = (3000),
adjust = ((totalDay*priceInt)/average),
percent = ((priceInt/adjust)-1),
pLimit = 1+(percent)*(percent)*((percent)/Math.abs(percent)),
priceAdj = (priceInt*(pLimit)),
priceDigits = (Math.ceil(priceAdj*100));
Logger.log(priceDigits);
}
I'm getting a syntax error of "Unexpected token )" when I run it in the console. You have an extraneous "/" in this line: (Math.ceil(priceAdj*100)/);
So I think I sort of know whats wrong.. the code actually does work, but I thought it didn't because what I'm using didn't make it the same color as the other variables for some reason. I don't know why that is, but I guess that isn't a question for here.A screenshot of the code is below:
Screenshot
Your var declaration is correct.
You can test it in codepen with the following sample which returns 288000 for priceDigits:
function myFunction() {
var average = (80),
totalDay = (100),
priceInt = (3000),
adjust = ((totalDay*priceInt)/average),
percent = ((priceInt/adjust)-1),
pLimit = 1+(percent)*(percent)*((percent)/Math.abs(percent)),
priceAdj = (priceInt*(pLimit)),
priceDigits = (Math.ceil(priceAdj*100));
//Logger.log(priceDigits);
return priceDigits;
}
$('body').text(myFunction());
See corresponding codepen here: https://codepen.io/anon/pen/YEPMLb.
Maybe your problem comes from your last instruction.

Looping Object javascript

How can I build a loop in JavaScript?
var input = {0:['S','M','L','XL'],1:['Red','Blue','Black']};
var output =
{
0:['S','Red'],
1:['S','Blue'],
2:['S','Black'],
3:['M','Red'],
4:['M','Blue'],
5:['M','Black'],
6:['L','Red'],
7:['L','Blue'],
8:['L','Black'],
9:['XL','Red'],
10:['XL','Blue'],
11:['XL','Black'],
};
I will render Html from output
I just write this function for him. Please don't justify people from how he write the code. Maybe it's easy to say "Hey! Do you know javascript?".
It's better if you leave some answer or stay quite. His code is valid javascript.
https://gist.github.com/ethaizone/b7d3a833dcdeb80234dde516649ac06d#file-buildmultidimentionarray2-js

How to apply conditional check and execute the code while creating new object?

I'm creating a new object of a class in my code as follows:
$(document).ready(function() {
var mail_list = $('#email_for_contact_us').val();
new Taggle($('.delicious.textarea')[0], {
tags: mail_list.split(',')
});
});
When the variable mail_list contains a comma separated series of strings it's working fine.
The issue raises when the variable mail_list contains nothing(it's blank/empty).
I don't want to execute the statement tags: mail_list.split(',') when the variable mail_list is blank/empty.
How to do this in jQuery in above code? Can someone help me in this regard please?
Thanks in advance.
If you have any queries regarding the issue please do let me know.
You can do something like this to avoid an array containing a single empty string item.
tags: mail_list? mail_list.split(',') : []

Get first value if there is a match

Ok, so I have a simple variable declaration with a match regex query.
var image = decl.val.match(/(url\(.*\))/)[1];
But I found out quickly I cannot do this because sometimes there is no match and instead of returning undefined or something like that an error comes up:
Uncaught TypeError: Cannot read property '1' of null
I would like to fix this as inline as possible as I will have to iterate this many times throughout my code. Thanks for the help!
Rather than trying to find magic 1 line solution you can do:
var m = decl.val.match(/url\(.*\)/);
var image = m ? m[0] : "";
Shortest one-liner you can probably get is
var image = decl.val.match(/(url\(.*\))/)); image = image ? image[1] : ""
(Or whatever your preferred alternative to "" is.)
Oh well, come to think of it, you can still go a little further with
var image = (image = decl.val.match(/(url\(.*\))/)) ? image[1] : "";
if that looks better to you …
This works due to the fact that variables are “hoisted” in JavaScript – that means first the code is searched for all variable declarations (var image in this case) and those variables are created, and the actual code is executed only after that.

converting JS variable to string & display it in html

Im newbie in JS so please bear with me.
I have a JS variable that i got from web service (JSON type). I tried to alert this variable and its working, returning 1 (integer) :
alert(result[0].totalMall);
I want to add a string to this variable so it will become like (for example) : 1 mall(s)
This is my code :
var result = result[0].totalMall.toString() + "Mall(s)";
alert(result);
$('.totalMall', this).html(result);
But the alert above always returning undefined. What should i do? I have been working for hours to just fix this.
And the result always displaying just "mall(s)" without the integer (result[0].totalMall.toString()) with it.
Any help is appreciated. Thanks :D
try following code:
var results = [
{
totalMall :3
},
{
totalMall :5
}
];
var result = results[0].totalMall.toString() + " Mall(s)";
alert(result);
$("#div").html(result);
Working example: http://jsfiddle.net/WaydY/
Thanks for your help, all :D
yes, it appears that "something" was wrong last night. I tried the code this morning, and its working fine now :D

Categories

Resources