function inside an object - javascript

I have a function inside an object but it isn't executed. Where did I go wrong.
JS:
switch(sometext){
case "GetStatus":
AgentReply = {
IsCustomer: false,
//UserText:"in process"
UserText: function () {
alert("test");
var text = "";
switch ($scope.LanguageId) {
case "1":
text = "is in process";
break;
case "2":
text = "test message";
break;
}
return text;
}
}
break;
}
This code was working fine. But now I have conditions for 'UserText'. So, commented the original code and wrote a function to get the desired message for 'UserText'. But the alert never pops up.

You should execute function, add () after }, like so
UserText: function() {
alert("test");
var text = "";
switch ($scope.LanguageId) {
case "1":
text = "is in process";
break;
case "2":
text = "test message";
break;
}
return text;
}()

Related

How to go to an a Specific HTML when a word is detected (Java)

Java I made an HTML called hello.html and now I want to use the replace() function in Java to go to the HTML page when the word "Covid" is detected on Google, I tried but it doesn't work for some reason, can you see where I am going wrong, or do I have to change my entire code?
function redirectURL() {
var specWord = getSpecificWord();
switch(specWord)
{
case 'corona':
window.location.replace('hello.html');
break;
case 'covid':
window.location.replace('hello.html');
break;
case 'covid-19':
window.location.replace('hello.html');
break;
default:
return true;
break;
}
return false; // don't let the form submit
}
function getSpecificWord(Element) {
var specificWord = "corona";
return specificWord;
}
The code does not work since you are not calling the redirectURL function.
Append redirectURL() to your code or use this instead:
(function redirectURL() {
var specWord = getSpecificWord();
switch (specWord) {
case 'corona':
window.location.replace('hello.html');
break;
case 'covid':
window.location.replace('hello.html');
break;
case 'covid-19':
window.location.replace('hello.html');
break;
default:
return true;
break;
}
return false; //don't let the form submit
})();
function getSpecificWord(Element) {
var specificWord = 'corona';
return specificWord;
}

Typescript condition with strings in function always return false

I have a weird error in a typescript condition in a function. There is my current code. My params came from outside:
getLevel(validation: string, status: string): string {
let card = "";
if (validation == "A") {
if (!status) {
card = "card-success";
} else {
switch (status) {
case "S":
// console.log("case s");
card = "card-success";
break;
case "C":
case "U":
case "V":
// console.log("case cuv");
card = "card-low";
break;
default:
// console.log("case default");
card = "card-success";
break;
}
}
}
return card;
}
For test I tried this :
getLevel(validation2: string, status2: string): string {
let card = "";
const validation = "A"; // for test purpose
const status = "U"; // for test purpose
if (status == status2) { // for test purpose
console.log("aeae"); // not working
}
console.log(status); //display U
console.log(status2); // display U
console.log(typeof status); //display string
console.log(typeof status2); // display string
if (validation == "A") {
switch (status) {
case "S":
console.log("case s");
card = "card-success";
break;
case "C":
case "U":
case "V":
console.log("case cuv");
card = "card-low"; // should display this one and works with variables defined within the function
break;
default:
console.log("case default"); // display this one with data from function args
card = "card-success";
break;
}
}
return card;
}
So when I try with variable in my code it works but not with my params. I also tried with a simple if statement but it doesn't work either.
There is my debugger screenshot
You're declaring const status = "U". if you hover over status in your IDE, it says that the type of status is "U". it means that the status variable can only be "U", no other characters or strings. if you want to manually say to typescript that status shouldn't be of type "U", you can say:
const status : string = "U"
Or in a more logical way, declare your status variable with let so typescript knows that there's the possibility that you may change the value of status variable down the line.
let status = "U"
function getLevel(validation2: string, status2: string): string {
let card = "";
const validation = "A";
let status = "U";
if (status == status2) {
console.log("aeae"); // not working
}
console.log(status); //display U
console.log(status2); // display U
console.log(typeof status); //display string
console.log(typeof status2); // display string
if (validation == "A") {
switch (status) {
case "S":
console.log("case s");
card = "card-success";
break;
case "C":
case "U":
case "V":
console.log("case cuv");
card = "card-low"; // should display this one and works with variables defined within the function
break;
default:
console.log("case default"); // display this one with data from function args
card = "card-success";
break;
}
}
return card;
}
Playground.
I just copied and pasted the original getLevel() method into a test project and called it and it worked fine:
test() {
console.log(this.getLevel("A", null));
console.log(this.getLevel("A", "S"));
console.log(this.getLevel("A", "C"));
console.log(this.getLevel("A", "U"));
console.log(this.getLevel("A", "X"));
console.log(this.getLevel("A", "UNKNOWN"));
}
Output was what I would expect:
card-success
card-success
card-low
card-low
card-success
card-success
You must have an error in your calls.
I found out why it was not working:
I check my parameters length and there was a space after the strings.
status.length // 2
So I add a .trim() to my variable
const trimedStatus = status.trim();
I wonder why I didn't see it in the Google console.
Please check your variable declaration once. I have reproduced the working example.
You were missing status2 variable declaration.
function getLevel(validation, status) {
let card = "";
if (validation == status) { // for test purpose
console.log("aeae"); // not working
}
if (validation == "A") {
switch (status) {
case "S":
console.log("case s");
card = "card-success";
break;
case "C":
case "U":
case "V":
console.log("case cuv");
card = "card-low"; // should display this one and works with variables defined within the function
break;
default:
console.log("case default"); // display this one with data from function args
card = "card-success";
break;
}
}
return card;
}
getLevel('A','U');
getLevel('A','A');
getLevel('U','U');

PDF Javascript - Setting field values

I'm just getting into Javascript and I'm working on editing a pdf for a charactersheet in a rpg me and some friends play, for ease of use. I want it to fill in forms automatically - and so far I've gotten it to work, but I'm wondering if there isn't a way to condense my code a bit and make it less repetitive?
var one = this.getField("Stat.0.0");
var statfield = this.getField("Text100.0.1");
if(one.value=="1"){
statfield.value="-30";
}
if(one.value=="2"){
statfield.value="-20";
}
if(one.value=="3"){
statfield.value="-10";
}
if(one.value=="4"){
statfield.value="-5";
}
if(one.value=="5"){
statfield.value="0";
}
if(one.value=="6"||one.value=="7"){
statfield.value="5";
}
if(one.value=="8"||one.value=="9"){
statfield.value="10";
}
if(one.value=="10"){
statfield.value="15";
}
if(one.value=="11"||one.value=="12"){
statfield.value="20";
}
if(one.value=="13"||one.value=="14"){
statfield.value="25";
}
if(one.value=="15"){
statfield.value="30";
}
if(one.value=="16"||one.value=="17"){
statfield.value="35";
}
if(one.value=="18"||one.value=="19"){
statfield.value="40";
}
if(one.value=="20"){
statfield.value="45";
}
Any tips on cleaning this up?
Use a switch() instead if statements. Don't forget break's:
var one = this.getField("Stat.0.0").value();
var statfield = this.getField("Text100.0.1");
var oneval = one.value;
switch (oneval) {
case "1":
statfield.value = "-30";
break;
//case...
case ("1" || "7"):
statfield.value = "5";
break;
//case...
default:
break;
}
or (notice break only on case "7"):
switch (oneval) {
//case...
case "1":
case "7":
statfield.value = "5";
break;
//case...
}

Can I implement/put an array on a switch conditional?

I was building my code when came to my mind a bizarre idea, can I implement/put an array inside a switch?
I mean, how can I make the codeHide case work? with this piece of code it don't work.
When I ask to set the command and I put hide() (that is codeHide[0] on the codeHide array) I want to switch take the codeHide case (my if-statement) and return an alert telling me the alertMessage of that particular array element.
If I put hide(background) (that is codeHide[1] on the codeHide array) I want to switch take the codeHide case else (of my if-statement) and return an alert telling me the alertMessage of that particular array element(in the is-statement).
Hope you understand me.
Doing this it don't work and I think it's because the "case codeHide:".
And this is what I've done so far:
var codeHide = ['hide()', 'hide(background)'];
$(".code").on("click", function () {
var codePrompt = prompt("Set the code in the command line."),
alertMessage = "",
consoleMessage = "Used '" + codePrompt + "' command.";
switch (codePrompt) {
case codeHide:
if (codeHide[0]) {
alertMessage = "Hiding elements...";
} else {
alertMessage = "Hiding Background...";
}
break;
default:
alertMessage = consoleMessage = "We are sorry but you entered a WRONG command, try again tho!\ntyped: " + codePrompt;
break;
}
alert(alertMessage);
console.log(consoleMessage);
});
I think you are trying something like
var commands = {
hide: 'hide()',
hideBg: 'hide(background)'
};
var codePrompt = prompt("Set the code in the command line."),
alertMessage;
switch (codePrompt) {
case commands.hide:
alertMessage = "Hiding elements...";
break;
case commands.hideBg:
alertMessage = "Hiding Background...";
break;
default:
alertMessage = "WRONG command";
break;
}
}
However, you can also use
var commands = {
'hide()': "Hiding elements...",
'hide(background)': "Hiding Background..."
};
var codePrompt = prompt("Set the code in the command line.");
var alertMessage = commands[codePrompt] || "WRONG command";
I guess you also want to run some functions:
var commands = {
'hide()': {
text: "Hiding elements...",
funcion: someFunctionToHide
},
'hide(background)': {
text: "Hiding Background...",
funcion: someFunctionToHideBackground
}
};
var codePrompt = prompt("Set the code in the command line."),
command = commands[codePrompt];
if(!command) {
alertMessage = "WRONG command";
} else {
alertMessage = command.text;
command.function();
}
switch operates by comparing the value being switched on to each of the possible cases using the identity operator ===. This means that you can put an array inside a case, and it will work as specified (but certainly not very intuitively for arrays):
var x = [1];
var a = [1];
switch (x) {
case [1]: alert("it's [1]!"); break;
case a: alert("it's a!"); break;
case x: alert("it's x!"); break;
}
This will alert "it's x!", while you might be expecting that either of the preceding two cases would be "good enough" to trigger. But that's just how === works:
[1] === x // false
a === x // true
x === x // true
So while you can technically use an array, in practice it would be very unusual to have a situation where it's actually useful to do so.
Going back to your code, since the values you are interested in are strings it seems that using a simple object as a map would do just fine:
var commands = {
"hide()": {
alert: "Hiding elements...",
console: "Blah blah"
}.
"hide(background)": {
alert: "Hiding background...",
console: "Blah blah"
}.
};
var fallback = {
alert: "Sorry, wrong command",
console: "Sorry, wrong command"
};
which would then allow you to write
var result = commands[input] || fallback;
alert(result.alert);
console.log(result.console);

Changing an image in a switch statement

I have an image in my HTML, but i would like to change the image when an answer is given by the user. For some reason the first function works, but the image doesn't change.. Can someone help me please.
This is my code so far:
window.onload = function () {
'use strict';
var showMessage = function (text) {
var messageCenter = document.getElementById('messageCenter');
messageCenter.innerHTML += "<p>" + text + "</p>\n";
};
var checkName = function (name) {
switch (prompt("What is your name?")) {
case "Leonard":
showMessage("Leonard! How did I not recognize you! Why would you like to get rid of me!");
break;
case "Penny":
showMessage("Penny Penny Penny! You've got so much to learn.");
break;
case "Howard":
showMessage("Howard, the only one without a Ph.D");
break;
case "Raj":
showMessage("Interesting. You're afraid of insects and women. Ladybugs must render you catatonic!");
break;
default:
showMessage("Well, I'm not familiair with you. I don't like to talk to strange people.");
break;
}
};
checkName()
var myImage = document.getElementById('sheldon');
var checkCase = function (img) {
switch (img) {
case "Leonard":
myImage("verbaasd.jpg");
break;
case "Penny":
myImage("les.jpg");
break;
case "Howard":
myImage("howard.jpg");
break;
case "Raj":
myImage("loser.jpg");
break;
default:
myImage("sheldon.jpg");
break;
}
};
checkCase()
};
Your error is in myImage(). You set this variable with a DOM object and call it as a normal function. This gives you an error. You also forgot to store the name and pass it as argument to your checking functions.
Your code should be:
window.onload = function () {
'use strict';
var name = prompt("What is your name?");
var showMessage = function showMessage(text) {
var messageCenter = document.getElementById('messageCenter');
messageCenter.innerHTML += "<p>" + text + "</p>\n";
};
var checkName = function (name) {
switch (name) {
case "Leonard":
showMessage("Leonard! How did I not recognize you! Why would you like to get rid of me!");
break;
case "Penny":
showMessage("Penny Penny Penny! You've got so much to learn.");
break;
case "Howard":
showMessage("Howard, the only one without a Ph.D");
break;
case "Raj":
showMessage("Interesting. You're afraid of insects and women. Ladybugs must render you catatonic!");
break;
default:
showMessage("Well, I'm not familiair with you. I don't like to talk to strange people.");
break;
}
};
checkName(name);
var myImage = function myImage(img) {
var sheldon = document.getElementById('sheldon');
sheldon.innerHTML = '<img src="' + img + '"/>';
};
var checkCase = function (name) {
switch (name) {
case "Leonard":
myImage("verbaasd.jpg");
break;
case "Penny":
myImage("les.jpg");
break;
case "Howard":
myImage("howard.jpg");
break;
case "Raj":
myImage("loser.jpg");
break;
default:
myImage("sheldon.jpg");
break;
}
};
checkCase(name);
};
Now your myImage() is a function which insert a new image inside a container with ID sheldon.
If you don't want to have a container for the image, and instead you want reference directly to the image, can change the myImage() function in:
var myImage = function myImage(img) {
var sheldon = document.getElementById('sheldon');
sheldon.src = img;
};
BTW, I see that you're learning so it's right to start from the basics, but remember that there are cleaner ways to do that.
Hope it's helpful to you.

Categories

Resources