I've never used javascript before and just need a bit of help. I have this:
if (vars.devUrl != '')
{
vars.smsmessage = vars.smsmessage + "\n\n" + vars.devUrl + "\n\n" + vars.optOut;
}
else
{
vars.smsmessage = vars.smsmessage + "\n\n" + vars.optOut;
}
delivery.smsParameters.smsContent = vars.smsmessage;
But I need to amend it to say if the vars.smsmessage includes the string "http:" then suppress the vars.devUrl
I've come up with the below, would an if in an if work?
if (vars.devUrl != '')
{
if (vars.smsmessage includes("http:"))
{
vars.smsmessage = vars.smsmessage + "\n\n" + vars.optOut;
}
else
{
vars.smsmessage = vars.smsmessage + "\n\n" + vars.devUrl + "\n\n" + vars.optOut;
}
else
{
vars.smsmessage = vars.smsmessage + "\n\n" + vars.optOut;
}
}
delivery.smsParameters.smsContent = vars.smsmessage;
Also should add that I can't test this before I go live very easily at all, hence the request
Very close, but the else comes outside the if block, and you need to use indexOf in Javascript to check if one string contains another, like so:
if (vars.devUrl) {
if (vars.smsmessage.indexOf('http:') > -1) {
vars.smsmessage = vars.smsmessage + '\n\n' + vars.optOut;
}
else {
vars.smsmessage = vars.smsmessage + '\n\n' + vars.devUrl + '\n\n' + vars.optOut;
}
}
else {
vars.smsmessage = vars.smsmessage + '\n\n' + vars.optOut;
}
delivery.smsParameters.smsContent = vars.smsmessage;
However, you can make this a little easier on the eye using a ternary conditional like so:
var url = vars.smsmessage.indexOf('http:') > -1 ? vars.devUrl + '\n\n' : '';
vars.smsmessage += '\n\n' + url + vars.optout;
delivery.smsParameters.smsContent = vars.smsmessage;
If I'm not mistaken, for this specific request your intial code don't need and extra if, simply an extra condition :
if (vars.devUrl != '' && vars.devUrl.indexOf('http:') < 0)
{
vars.smsmessage = vars.smsmessage + "\n\n" + vars.devUrl + "\n\n" + vars.optOut;
}
else
{
vars.smsmessage = vars.smsmessage + "\n\n" + vars.optOut;
}
delivery.smsParameters.smsContent = vars.smsmessage;
So; basically, you use vars.devUrl only if it's not empty AND if it doesn't contain "http:"
Related
The line in question is...
if(listContent[Source].properties === ""
I need it to check if the "Source" key has any value. It's currently not outputting anything. What is proper syntax for this?
Here is the full code:
if (visibleFeatures) {
var uniqueFeatures = getUniqueFeatures(visibleFeatures, "arrayIndex");
for (var i = 0; i < uniqueFeatures.length; i++) {
if (listContent[Source].properties === "") {
listContent += '<div class="dealer"><h3>' + uniqueFeatures[i].properties.Name + '</h3><p class="address">' + uniqueFeatures[i].properties.Address + '<br>' + uniqueFeatures[i].properties.City + ', ' + uniqueFeatures[i].properties.State + ' ' + uniqueFeatures[i].properties.Zip + '</p><p class="phone">' + uniqueFeatures[i].properties.Phone + '</p></div>';
} else {
listContent += '<div class="dealer"><h3>' + uniqueFeatures[i].properties.Name + '</h3><p class="address">' + uniqueFeatures[i].properties.Address + '<br>' + uniqueFeatures[i].properties.City + ', ' + uniqueFeatures[i].properties.State + ' ' + uniqueFeatures[i].properties.Zip + '</p><p class="phone">' + uniqueFeatures[i].properties.Phone + '</p><p class="bl-map-link">' + uniqueFeatures[i].properties.Source + '</p></div>';
}
}
Thank you guys so much for all the help. I was able to figure out my error.
!!uniqueFeatures[i].properties.Source
The !! is what made the difference. I'm still researching why this works and why this doesn't if (strValue === "").
The function is replicating each item that I place in. It runs through all of the panels, and places the additional items each time it runs. I have tried using the if statement that is placed but it is not working. I am placing the entire code block, but the issue starts at the let x = -1 variable. The full code block is strictly for reference. Please let me know if there is anything additional that I can provide. Additionally, .first() will not work for this instance.
function getRecordUI( alias, type, id, viewtype, version ) {
// Get record model from Data Model Registry
if ( type ) {
getJSONUIModel( type,
function ( data ) {
displayRecordSections( alias, type, id, version, viewtype, data );
},
function (error) {
console.log("Error getting UI model for type " + type);
console.log(error);
displayRecordSections( alias, type, id, version, viewtype, null );
});
} else {
displayRecordSections( alias, type, id, version, viewtype, null );
}
let x = -1;
lastName = "";
$("#panels-region .panel-heading").each(function () {
x++
let curName = $(this).attr("name")
if(!$('<a id="panels-sidebar-' + curName.replace(/ |\//g, "_") + '" class="w3-bar-item" href="#panel-' + x + '-header" title="' + curName.replace(/ |\//g, "_") + '"> ' + curName + '</a>')) {
$('<a id="panels-sidebar-' + curName.replace(/ |\//g, "_") + '" class="w3-bar-item" href="#panel-' + x + '-header" title="' + curName.replace(/ |\//g, "_") + '"> ' + curName + '</a>').insertAfter("#panels-sidebar" + lastName.replace(/ |\//g, "_"))
break;
}
lastName = "-" + curName;
});
$("#graph-region .panel-heading h4").each(function () {
x++
let curName = $(this).text().replace("Collapse panelExpand panel", "")
if(!$('<a id="tree_viewers-sidebar-' + curName.replace(/ |\//g, "_") + '" class="w3-bar-item" href="#panel-' + x + '-header" title="' + curName.replace(/ |\//g, "_") + '"> ' + curName + '</a>')) {
$('<a id="tree_viewers-sidebar-' + curName.replace(/ |\//g, "_") + '" class="w3-bar-item" href="#panel-' + x + '-header" title="' + curName.replace(/ |\//g, "_") + '"> ' + curName + '</a>').insertAfter("#tree_viewers-sidebar" + lastName.replace(/ |\//g, "_"))
break;
}
lastName = "-" + curName
});
$(".menu-links a").css("padding", "0px 8px")
$(".panel-body").css("background-color", "white")
$("#panels-region .panel-wrapper .panel-default").css("background-color", "white")
}
So I have this command that's supposed to display a player's stats if it's found and says player not found otherwise. After I search a player and go to the previous screen, I get "player not found". I thought it was because my loop continues running after the player is found and my boolean becomes false, so I added a break statement. Won't work
function Search(Table, Stat1, Stat2, Stat3, Stat4, Stat5) {
onEvent("Search2.btn", "click", function() {
readRecords(Table, {}, function(records) {
var SearchPlayer = getText("text_input1");
var found = false;
for (var i = 0; i < records.length; i++) {
if ((records[i]).Player == SearchPlayer) {
setScreen("DisplaySearch");
setText("label3", records[i].Player + " Stats" + "\n" + records[i][Stat1] + " " + Stat1 + "\n" + records[i][Stat2] + " " + Stat2 + "\n" + records[i][Stat3] + " " + Stat3 + "\n" + records[i][Stat4] + " " + Stat4 + "\n" + records[i][Stat5] + " " + Stat5 + "\n");
setText("text_input1", "");
setText("label5", "");
found = true;
break;
} else if ((found == false)) {
setText("label5", "Player Not Found");
}
}
});
});
I need to get rid of the comma right after the input firstAuthorInitials.value.slice(0,1) only when it is empty.
In case when in the input firstAuthorInitials.value.slice(0,1) is something written the code works fine.
The code looks like:
if (edition.value == "none") {
div.innerHTML +=
firstAuthorSurname.value + ", " +
firstAuthorName.value.slice(0, 1) + "." +
firstAuthorInitials.value.slice(0,1) + "." + ", " +
year.value +
". <i>" + title.value + "</i>. " +
placeOfPublication.value + ": " +
publisher.value + ".";
}
Create an array, push things into it, then join. Use logic applied to each element to decide if you want to push it or not.
if (edition.value == "none") {
var stringComponents = [];
stringComponents.push(
firstAuthorSurname.value,
", ",
firstAuthorName.value.slice(0, 1),
"."
);
if (/* some kind of logic */) {
stringComponents.push(firstAuthorInitials.value.slice(0,1), "., ");
}
stringComponents.push(
year.value +
". <i>",
title.value,
"</i>. ",
placeOfPublication.value,
": ",
publisher.value,
"."
);
div.innerHTML += stringComponents.join("");
}
I was wondering if there is any way to put this in a string.
function format(e) {
if (!e.id) return e.text;
if (e.avatar === "defaultAvatar.jpg") {
return "<div class='select2TemplateImg'><span class='helper'></span> <img class='flag' src='" + baseURL + "uploads/users/d/defaultAvatar.jpg'/></div><div class='select2TemplateText'><p> " + e.text + "</p><p>" + e.username + "</p><p>" + e.cnic + "</p></div>"
} else if (e.avatar != "defaultAvatar.jpg") {
return "<div class='select2TemplateImg'><span class='helper'></span> <img class='flag' src='" + baseURL + "uploads/users/" + e.userid + "/" + e.avatar + "'/></div><div class='select2TemplateText'><p> " + e.text + "</p><p>" + e.username + "</p><p>" + e.cnic + "</p></div>"
}
}
like
var variableName = "FUNCTION-HERE";
Problem is in that function i have used both double quotes and single quotes and if i try to put it inside the string, then there is no support of triple quotes in javascript and i can not close the above function in simple javascript quotes (single or double) everything gets messed up.
Any idea how to achieve it?
Like this?
var variableName = 'function format(e) {\
if (!e.id) return e.text;\
if (e.avatar === "defaultAvatar.jpg") {\
return "<div class=\'select2TemplateImg\'><span class=\'helper\'></span> <img class=\'flag\' src=\'" + baseURL + "uploads/users/d/defaultAvatar.jpg\'/></div><div class=\'select2TemplateText\'><p> " + e.text + "</p><p>" + e.username + "</p><p>" + e.cnic + "</p></div>"\
} else if (e.avatar != "defaultAvatar.jpg") {\
return "<div class=\'select2TemplateImg\'><span class=\'helper\'></span> <img class=\'flag\' src=\'" + baseURL + "uploads/users/" + e.userid + "/" + e.avatar + "\'/></div><div class=\'select2TemplateText\'><p> " + e.text + "</p><p>" + e.username + "</p><p>" + e.cnic + "</p></div>"\
}\
}';
Use toString()
var myFunc = function(){
console.log("here are double quotes", 'and here single');
}
console.log(myFunc.toString());
Or String (as an operator)
var myFunc = function(){
console.log("here are double quotes", 'and here single');
}
console.log(String(myFunc));
It also takes 2 characters less than the previous version. It is a better method, IMHO.
A hackish way would be as following:
var myFunc = function(){
console.log("here are double quotes", 'and here single');
}
console.log('' + myFunc);
//or
console.log(myFunc + ''); //both are same
It works because of Javascript's automatic type casting. We are basically appending an empty string to the function which causes the function to be automatically cast to a string. It then adds the empty string but since the string is empty, no actual change is made to the string returned by casting the function.
Use this ONLY when golfing or in overly simple cases. It is not considered good practice.
Further, there are better ways to store functions, like wrapping them in an object. Or if you wanna pass them as arguments, you can do that directly by passing the function itself (without the parenthesis) since functions in javascipt are first class citizens.
you can do it by escaping using '\'
var v = "function format(e) { if (!e.id) return e.text;if (e.avatar === \"defaultAvatar.jpg\") {return \"<div class='select2TemplateImg'><span class='helper'></span> <img class='flag' src='\" + baseURL +\"uploads/users/d/defaultAvatar.jpg'/></div><div class='select2TemplateText'><p> \" + e.text + \"</p><p>\" + e.username + \"</p><p>\" + e.cnic + \"</p></div>\"} else if (e.avatar != \"defaultAvatar.jpg\") {return \"<div class='select2TemplateImg'><span class='helper'></span> <img class='flag' src='\" + baseURL + \"uploads/users/\" + e.userid + \"/\" + e.avatar + \"'/></div><div class='select2TemplateText'><p> \"+ e.text + \"</p><p>\" + e.username + \"</p><p>\" + e.cnic + \"</p></div>\"}}";
While this may not be what you directly asked about, you can also directly convert code into string:
function format(e) {
if (!e.id) return e.text;
if (e.avatar === "defaultAvatar.jpg") {
return "<div class='select2TemplateImg'><span class='helper'></span> <img class='flag' src='" + baseURL + "uploads/users/d/defaultAvatar.jpg'/></div><div class='select2TemplateText'><p> " + e.text + "</p><p>" + e.username + "</p><p>" + e.cnic + "</p></div>"
} else if (e.avatar != "defaultAvatar.jpg") {
return "<div class='select2TemplateImg'><span class='helper'></span> <img class='flag' src='" + baseURL + "uploads/users/" + e.userid + "/" + e.avatar + "'/></div><div class='select2TemplateText'><p> " + e.text + "</p><p>" + e.username + "</p><p>" + e.cnic + "</p></div>"
}
}
var someVariable = format.toString();
You can use + on the end of each sting if you want it formatted nicely in your file, then escape your double quotes with \
var variableName = 'function format(e) {'+
'if (!e.id) return e.text;'+
'if (e.avatar === "defaultAvatar.jpg") {'+
'return "<div class=\'select2TemplateImg\'><span class=\'helper\'></span> <img class=\'flag\' src=\'" + baseURL + "uploads/users/d/defaultAvatar.jpg\'/></div><div class=\'select2TemplateText\'><p>"+ e.text +"</p><p>" + e.username + "</p><p>" + e.cnic + "</p></div>"'+
'} else if (e.avatar != "defaultAvatar.jpg") {'+
'return "<div class=\'select2TemplateImg\'><span class=\'helper\'></span> <img class=\'flag\' src=\'" + baseURL + "uploads/users/" + e.userid + "/" + e.avatar + "\'/></div><div class=\'select2TemplateText\'><p> " + e.text + "</p><p>" + e.username + "</p><p>" + e.cnic + "</p></div>"'+
'}';
Personally I feel dirty doing it like that but it gets the job done.