JavaScript: Making variable into default? [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
Javascipt: Making variable into default?
I have code like this :
<script>
document.write(ab);
</script>
ab is a variable but it's not declared yet because I want it will be changing by Users. The default result I want it displays is x+y (for example as). But When Users enter any variable, the result need to be changed. For instance :
<script>
var ab = "The users change code";
</script>
The result will change is The users change code
So, Do you have any idea for my issue. Thank for your help.

Just to be pedant :
You said :
ab is a variable but it's not declared yet
shom's answer should work fine.
But if I was a robot which analyze your question :
this would pass although you have declared it :
var ab=undefined;
if (typeof ab === 'undefined')
var ab= 'x+y';
document.write(ab);
the safest way (as answering to your :"not declared yet "):
if (!('ab' in window))
var ab = 'x+y';
document.write(ab);

You can do this in various ways. A ternary is a simple one.
var ab=(typeof ab==='undefined'?) x+y : ab;
This says that if ab is undefined, set it to x+y, otherwise leave it at the current, presumably user set, value.
There may be better ways to handle all of this. Post more code if you'd like.

if (typeof ab == 'undefined')
var ab = 'x+y';
document.write(ab);

Related

How to compare strings in different languages (javaScript? [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 1 year ago.
Improve this question
I need to make a function that compares two strings, no matter the language. Example:
let string1 = 'hola' //Spanish
let string2 = 'hi' //english
console.log (string1 === string2) //true as expected result
Is there a way to compare strings in different languages.
Regards!!
No, not really. Doesn't make much sense either considering that each word might have different meaning in each language depending on context.
Best you can do is try to translate the words and try to find matches in one of the many translations, or use an embedding model for those specific languages and see if the vectors are similar.
The only idea that I have right now is to declare 2 Maps for both languages and then get the key:
var spEn = new Map();
spEn.set('hola', 'hi');
var enSp = new Map();
spEn.set('hi', 'hola');
function spanishEnglish(spanish, en){
translation = spEn.get(spanish);
return translation === en;
}
function englishSpanish(en, spanish){
translation = enSp.get(en);
return translation === spanish;
}
console.log(spanishEnglish('hola', 'hi'))
console.log(englishSpanish('hi', 'hola'))
However this is a just a rudimentary example. There are tons of things you need to pay attention to :)

Why is 'Bring an umbrella' the correct answer? [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 3 years ago.
Improve this question
I am using the Grasshopper app on my phone and I do not understand an example they gave me for if then statements.
They give you the solution because I answered incorrectly, but I do not understand why the solution given is correct.
var todayWeather = 'rainy';
var tommorrowWeather = 'cloudy';
if (todayWeather === 'rainy') {
print('Bring an umbrella');
}
if (todayWeather !== 'rainy') {
print('Maybe the sun will come out');
}
They say the correct answer is 'Bring an umbrella'. But why is this what this code will produce if it is run?
translating the code to english:
create a variable named todayWeather and set it to rainy
create a variable named tommorowWeather and set it to cloudy
if the variable todayWeather is rainy (true) then
print to the screen Bring an umbrella
(close if conditional)
if the variable todayWeather is not rainy (false, because it is set to rainy) then
print to the screen Maybe the sun will come out
(close code conditional)
Note the print statements only execute if the condition is met.
Also the variable values are never changed by this code after they are set.

Which is better : using $("#myVar") directly or saving it to a variable first? [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 7 years ago.
Improve this question
I'm using jQuery and I'd like to know whether it is good practice to save $("#myVar") to a variable first like this
var jQvar = $("#myVar");
or directly using it like this:
var myVar = $("#myVar").val();
$("#myVar").removeProp("disabled");
..
and when is the best time to use the other over the other? (var and NonVar)
Each time you are accessing an element it takes (little but still) time. So saving it to a variable makes sense in terms of performance and efficiency if you access the element more then once in the current scope.
So writing it like this:
var myVar = $("#myVar");
var myVal = myVar.val();
myVar.removeProp("disabled");
will be faster.
In your specific case, this is the difference for me:
Try for your self
var jQvar = $("#myVar");
- jQvar.val();
- jQvar.onclick(function(){
// do something here
});
- jQvar.onSubmit(function(){
// do something here
});
... obviously this is more faster, you can reduce redundancy of variable declaration, much neater and less clutter you don't want to scroll and find every variable in file right?

How to make a javascript count up, such as world population? [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 7 years ago.
Improve this question
I don't know how to make a Javascript count up that is related to the real time, which means when you reload the page, the counter won't start over again. Would anybody tell me how to make that happen :) Example like http://www.worldometers.info/ Thanks a lot.
The code they are using is likely pulling from a database with an actual value increasing live.
Check out this js fiddle I made for an example of how a simple timer can work. Notice that if you press "Run" multiple times, the time itself will stay constant.
Using a remote database will cause a lot more work, but for saving values across browser refreshes you should learn about localStorage I'd check out W3 School's article on the subject.
In my implementation I use
localStorage.setItem("time", currentTime); // Note that CurrentTime must be a string!
in each iteration of your code after setting the currentTime var.
When you start up your application, a simple if statement
if (localStorage.getItem("time") {
CurrentTime = localStorage.getItem("time");
} else {
// set default values
}
will work, as localStorage.getItem will return null if the value doesn't exist (or if you set the value to null manually).
(for localStorage, you can also use [bracket notation] and will probably see that in most common examples)
localStorage["foo"] = "value";
// is the same as:
localStorage.setItem("foo", "value")
// and
var bar = localStorage["foo"];
// is the same as:
var bar = localStorage.getItem("foo");

Abusing JavaScript string operations [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 4 months ago.
Improve this question
I've got this function in javascript:
g.changeImage = function (a) {
h.src = "image" + a + ".png";
};
It's used like this:
g.changeImage(2);
And it changes image h. The problem is, that it cannot use image from other directory than current.
I'm writing "addon" for the website which should hook into the existing script and change few things. One of them require changing h.src. Is there any way I can fool the browser and change the address of h to custom url using only the function given?
I can't access h directly, nor change existing script on the website. I can only use function given.
Thank you for any help.
One option that I can come up with off the top of my head would be to overwrite the function:
Old Function:
g.changeImage = function (a) {
h.src = "image" + a + ".png";
alert(h.src);
};
g.changeImage("test.png")
New Function:
g.changeImage = function (a) {
h.src = "test - new url " + a + ".png";
alert(h.src);
}
g.changeImage("test")
Here's a quick example for you to play with and see if this will work for what you need: http://jsfiddle.net/2BsQP/

Categories