I'm trying to create a prompt that has a time delay, the value that is written in the prompt is then used in other areas of the form. I have written some javascript coding but I believe there is a minor thing that i am doing wrong as currently the prompt and delay are working, but because the setTimeout function is being used, that is what is being displayed in the form, instead of the content of the prompt. This is my Javascript?
var name = setTimeout(function(){ prompt("What is your name?", "Type your full name here")},750);
document.write("Document Written By: " + name + " (" + day + "/" + month + "/" + year + ") ")
If it depends on the value, and a function is asynchronous, you've got do it in the callback. Just like every other asynchronous piece of JavaScript...
setTimeout(function(){
name = prompt("What is your name?", "Type your full name here");
document.write("Document Written By: " + name + " (" + day + "/" + month + "/" + year + ") ");
},750);
But as #Jon commented, please do not use document.write.
Related
I'm building an app that will send an email with variables, but i only want to include certain things if a variable is true (checkbox)
So I think i need If statments inside the email.putExtra(Intent.EXTRA_TEXT,""); but I dont seem to figure out how. it would look something like this:
public void Email (){
Date today = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-YYYY HH.mm");
String Time = formatter.format(today);
Intent email = new Intent(Intent.ACTION_SENDTO);
email.setData(Uri.parse("mailto:"));
email.putExtra(Intent.EXTRA_SUBJECT, "Audit" );
email.putExtra(Intent.EXTRA_TEXT, Time + "\n" + "Audit Results:"
if (MainActivity2.Send_A)=true{
MainActivity2.Ok_A1 + "\n" + MainActivity2.EditText1 + "\n" + MainActivity2.POA1
}
);
I know the code is completly wrong, but is it possible to do something like this?
Build up the string, than set that string to your function call.
var extraText = Time + "\n" + "Audit Results: ";
if (MainActivity2.Send_A === true){
extraText += MainActivity2.Ok_A1 + "\n" + MainActivity2.EditText1 + "\n" + MainActivity2.POA1;
}
email.putExtra(Intent.EXTRA_TEXT, extraText);
I just learned about the prompt() command; I know that the prompt() command returns user input in the form of a string. I was messing with the program below, and I typed in Per "Dead" Ohlin for the male name. Why did this work and not cause any problems? "Per "Dead" Ohlin..." should have caused a problem. Does the interpreter automatically fix this by putting an escape character before the quotation marks?
let nameOfTheKiller = prompt("Type in a male name.");
let nameOfTheVictim = prompt("Type in a female name.");
let nameOfDrug = prompt("Type in the name of a drug.");
let nameOfAlchoholicBeverage = prompt("Type in the name of an alchoholic beverage.");
let story = nameOfTheKiller
story += " went to a diner, met "
story += nameOfTheVictim + ", and asked her to hangout."
story += " She said yes, so " + nameOfTheKiller + " took her home. As soon as they arrived to "
story += nameOfTheKiller + " relax-location, " + nameOfTheKiller
story += " pulled out " + nameOfDrug + " and " + nameOfAlchoholicBeverage + ". "
story += nameOfTheKiller + " and " + nameOfTheVictim
story += " started using the party favors and got really high and drunk. The party favors gave "
story += nameOfTheKiller + " auditory halucinations that comanded him to kill "
story += nameOfTheVictim + ", so he did." ;
alert("We are done asking you questions. We are generating a story for you. The story will be finished, shortly.");
document.write(story) ;
prompt is not eval - whatever you pass to it will be interpreted as a string. Typing in
Per "Dead" Ohlin
when this line runs
let nameOfTheKiller = prompt("Type in a male name.");
is like doing
let nameOfTheKiller = `Per "Dead" Ohlin`;
Any characters you include in the string you enter which happen to also be valid string delimiters in Javascript will be interpreted as those literal characters (", ', backtick), rather than as delimiters.
in my project when i run it in chrome it is showing wrong time. explorer it is showing true. So i write this js code but it is still doesnt working.
This is my js;
var FormatTrxStartDate = function (value, record) {
var processDate = new Date(value);
return processDate.getDate() + "." + (processDate.getMonth() + 1) + "." +
processDate.getFullYear() + " " + processDate.getHours() + ":" +
processDate.getMinutes() + ":" + processDate.getSeconds();
};
And this is where i use it;
<ext:ModelField Name="TrxStartDate" Type="String" >
<Convert Fn='FormatTrxStartDate' />
</ext:ModelField>
Pass Parameter Value in not proper Date Format
Use processDate.getFullYear()==> processDate.getFullYear().toDateString() date data convert into String
X.Js.Call("FormatTrxStartDate ",value,record);
please try this code
I am playing with some basic js I am just beginning to learn, so far I have the code below. I am trying to ask the user what their name is and then tell them if they share the same name as a racing driver (from my array driversNames).
If they have the same name as a racing driver it would tell them they do, if not it would tell them they don't. However I have a feeling I have something wrong here: if (yourName === driversNames) but I cannot figure it out.
It doesn't matter what I enter into the prompt, it always says sorry you don't have the same name.
var driversNames = ["Lewis", "Fernando", "Sebastian", "Jenson", "Daniel"]
for (var i = 0; i < driversNames.length; i++) {
console.log(driversNames[i] + " " + "is a drivers name");
}
var yourName = prompt("What is your name?")
console.log("Your name is" + " " + yourName)
if (yourName === driversNames) {
console.log("Awesome" + " " + yourName + " " + "you share the same name as a Formula 1 driver!")
} else {
console.log("Sorry" + " " + yourName + " " + "you don't have the same name as any Formula 1 drivers")
}
You made one mistake in this line if (yourName === driversNames).
It doesnt compare your name with names from driversNames. The most easiest way: its use indexOf method. So this line should be like below
if (driversNames.indexOf(yourName) > -1) //Get Name otherwise no
And jsfiddle example for you, also indefOf link
Thanks
You are comparing a string to an array, so the comparison will return false. You have a few options to fix this though - I'll explain using a loop to check each string, and using indexOf.
Loop: You need to loop through each element in the driversNames array and compare each one. This is the manual way.
var sameName = false; //flag to keep track of if name matches or not
driversNames.forEach(function(name) { //loop through each name in driversNames
if(yourName === name) { //compare your name to driver name
sameName = true; //if match, set flag to true
}
}); //loop ends here
if(sameName) { //if flag is true, a name matched
console.log("Awesome" + " " + yourName + " " + "you share the same name as a Formula 1 driver!"); //Console log success statement
} else { // else, no name matched
console.log("Sorry" + " " + yourName + " " + "you don't have the same name as any Formula 1 drivers"); //console log fail statement
}
IndexOf: This method uses less lines of code, but isn't compatible on all browsers - I believe anything under IE8 will break when using this for example. But if compatibility isn't an issue, it looks like this:
if (driversNames.indexOf(yourName) > -1) { //indexof returns -1 for no match, and a number for match
console.log("Awesome" + " " + yourName + " " + "you share the same name as a Formula 1 driver!"); //console log success statement
} else {
console.log("Sorry" + " " + yourName + " " + "you don't have the same name as any Formula 1 drivers"); //console log fail statement
}
indexof is a little more elegant, although easy to forget the compatibility issue. Code is commented but just to explain it: Arrays have a method you can call, called indexOf() which takes a parameter. This method will then check if that parameter is in the array and if it is, return a value which is it's position in the array. If it isn't in the array, it will return -1.
I know the title may be a bit confusing so allow me to explain my situation.
Basically I made a dragon mini-game and one of the features is a talk function which allows the console to randomize a message after striking the dragon. Now however, I want to add a 2nd feature that allows that talk function to exist and activate, but in the case that its already activated in the previous turn, I want it to not work and use a different talk message.
So instead of saying something like:
I striked the dragon!
I striked the dragon!
I striked the dragon!
I hit the dragon!
I want it to always randomize each time and not repeat itself.
I striked the dragon!
I hit the dragon!
I striked the dragon!
I hit the dragon!
The code i'm using is way to long so i'll give you a snipet of what I can.
player.TALK = Math.floor(Math.random() * 3);
if (player.HP >= 1) {
if ((player.DAMAGE >= 1) && (player.AIM >= 3) && (player.SPECIALAIM >= 2)){
npc.HP = npc.HP - player.DAMAGE;
if (player.TALK === 0) {console.log("PLAYER HP: " + player.HP + " --You hammer the Dragon for " + player.DAMAGE + " hp! " + "It only has " + npc.HP + "hp left!--");
}
if (player.TALK === 1) {console.log("PLAYER HP: " + player.HP + " --You slice the Dragon for " + player.DAMAGE + " hp! " + "It only has " + npc.HP + "hp left!--");
}
if (player.TALK === 2) {console.log("PLAYER HP: " + player.HP + " --You sever the Dragon for " + player.DAMAGE + " hp! " + "It only has " + npc.HP + "hp left!--");
}
This is a small piece of the larger code I have which would be way too much to post here.
Basically what i'm trying to figure out is how to get the computer to execute a statement once, and when it loops back around to it again, if its ALREADY used it last time, to not use it again, but not completely break it where it NEVER uses it again, just if it used it last time, then it won't repeat, and use a different talk option instead.
You could use an auxiliary array with gets emptied from time to time, and refilled then. Leaving hp and stuff aside, it's something like:
player.TALK=['you pwn the dragon','you kick the dragon','you eviscerate the dragon'];
player.AUXTALK=player.TALK.slice(0);
function killdragon(player) {
var talkindex = Math.floor(Math.random() * player.AUXTALK.length);
console.log(player.AUXTALK[talkindex]);
delete player.AUXTALK[talkindex];
if (player.AUXTALK.length==0) player.AUXTALK=player.TALK.slice(0);
}