Variable not printing in console.log [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have large chunk of js code.
I am not able to display the output in console.log.
Can you tell me what I am missing?
I am providing my code below:
var multCoffees = false;
if (Coffees.length > 1) {
multCoffees = true;
}
if (apptTimeCell) {
apptTimeHTML = MyDay.dish(allData, multCoffees);
apptTimeCell.innerHTML = apptTimeHTML;
} else {
apptTimeCell = Util.cep("span", {
className: "appt-time"
});
patientRowTD.insertBefore(apptTimeCell, patCell);
}
dish: function (allData, multCoffees) {
if (multCoffees) {
var htmlArr = [];
htmlArr.push(allData.APPT_TIME_DISPLAY, "<br/><span class='sub-detail'>", allData.MNEMONIC, "</span>");
console.log("multiCoffee" + allData.PROVIDER_MNEMONIC);
return htmlArr.join("");
} else {
return allData.APPT_TIME_DISPLAY;
}
},

Do you mean to have the ** around your console statement? That is probably causing the error.
**console.log("dateRaj" + date);**

I know this behaviour. This happens when combining values with strings ( especially with objects). Try to output your data variable stand alone without combining it with the string. This way:
console.log("dateRaj");
console.log(date);
update
Like my commentators said the console seems to take also multiple arguments which may be the most elegant way to go.

Just guessing...
With unescapeJSON() you mean JSON.parse() ?
Show us your syntax error message?

Related

Is it best practice to have an if - else block inside the else block [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have a Map = {'key - string', value as object} and a List[] and a String 'TEST',
Here I need to check two checking based on the String in the map and the Array,
For Example,
if(Map.has('TEST')) {
Need to do some logic
} else {
if(Array.includes('TEST') {
Need to do some manipulation
} else {
Need to do some manipulation
}
}
I have tried like above to achieve my need. Is it a best practice? Advice me.
if(Map.has('TEST')) {
Need to do some logic
} else if(Array.includes('TEST')) {
Need to do some manipulation
} else {
Need to do some manipulation
}
Use Else If
More Examples
EDIT1: Since you said you Need to check 2 Things Maybe this suits even more but im not sure your Question is very vague.
// || = OR
if(Map.has('TEST') || Array.includes('TEST')) {
Need to do some logic
} else {
Need to do some manipulation
}

Javascript form validation pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to validate that two characters and a number were correctly input.
var studentValid = /^[MTWTF][AL][1-9]$/i;
if (studentValid.test(studentTemp.value))
{
alert("true");
}
else
{
alert("false");
}
Yet everything I enter turns out false?
The problem is with your regexp (/^[MTWTF][AL][1-9]$/i). What this tells you is that you first want one of the characters M,T,W,T or F, and after that either A or L and finaly a number (and nothing before or after this).
So for example
ML4, WA5, FL9
will give you true
while
AM9, ML0, MMA5, MA99
will give you false.
Is this the pattern you are trying to match? There is nothing else wrong with your code and a valid value will give you true, for example:
var studentValid = /^[MTWTF][AL][1-9]$/i;
var value = 'MA9';
if (studentValid.test(value))
{
alert("true");
}
else
{
alert("false");
}
When working with regexp, it can be very usefull to use a tool to help you build it, check out https://regex101.com/r/A5FOIh/3 where you can try your different studentTemp.value to see if they match.

How to use `this` like an object and get its variables/functions by a string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've got an object.
function Obj()
{
}
Obj.prototype.doSomething = function(thing)
{
this["do" + thing]();
}
Obj.prototype.doAlert = function()
{
alert("Alert!");
}
var obj = new Obj();
obj.doSomething("Alert");
This is just a shortened down version of my object, and is a lot bigger.
What I would like to do is that if you pass in 'Alert' it will run this.doAlert(); and if I pass in 'Homework' it will run this.doHomework();
Obviously, in this case, it is stupid to do it like this, but my final project is going to be completely different.
It works fine with window like this:
window["do" + thing]();
but I don't want it to be a global function, but to be part of obj.
Does anyone have an idea how I'd go about doing this?
Thanks in advance!
It turns out that when you get the function through this['functionName'], this is not bound to it.
This means you cannot use this.foo inside any function called like above.
To fix this, I used the following code:
this["do" + thing].bind(this)();
instead of this["do" + thing]();
JSFiddle: https://jsfiddle.net/auk1f8ua/

Need to replace numbers between [...] with Javascript or jQuery [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Inside a string: I need to replace the number between [ ] by another number with Javascript or jQuery.
What is the best way to proceed ?
Eamples
"[12345].Checked" > "[1].Checked"
"[123].RequestID" > "[21].RequestID"
Thanks for your help.
function fixNum(str, newnum) {
return str.replace(/\[\d+\]/,'[' + newnum + ']')
}
Use .replace:
"[12345].Checked".replace(/\[(\d+)\]/, "[1]") // "[1].Checked"
With regular expressions:
/\[(\d+)\]/
It searches a number (no matter length) inside a [] set.
Combined with replace you can make it.
Test it:
https://regex101.com/r/zT5kD0/1
Use replace method to replace the part
function replaceStr(string,orgText,replaceText){
var res = string.replace(orgText, replaceText);
return res;
}
console.log(replaceStr("[12345].Checked",'12345','1'));
jsfiddle

javascript return, not functioning [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this code:
replaceAny('this','that',string);
if(val!="")
the replaceAny function looks like this:
function replaceAny(first,second,ele) {
var val = ele.replace(first,second);
alert(val);
return val;
}
But then after running the replaceAny function (and the alert shows the right value, the if condition tells me that the variable val is not set, why?!
You are not using the return value from the function correctly. This is something like what you want
function replaceAny(first,second,ele) {
return ele.replace(first,second);
}
var val = replaceAny('this','that',string);
if(val!=""){
//something, something darkside
}
I think you are not assigning the value returned by your function to your variable val.
Try like this
val = replaceAny('this','that',string);
if(val!="")

Categories

Resources