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.
Related
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 2 months ago.
Improve this question
Maybe tittle is not too descriptive, but how do I get this kind of result
var string = 11+'-'+10
// expected result 11-10
console.log(string);
Every time I try to do the above I get 1, or whatever the result of the subtraction is
I will be a little bit clear about this. What I want to do with this is generate a button with onclick like this:
onClick = method(1,[11-10, 12-10])
method(id,...array){
console.log(array)
//result [1,2]
}
even if inspecting the button actually shows the correct output
In your first example, you use
11+'-'+10
In the second one, you use
11-10
There is a clear difference
Using the first method in the second code will work as expected
method(1,[11+'-'+10, 12+'-'+10])
To make it shorter just use strings
method(1,['11-10', '12-10'])
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 2 years ago.
Improve this question
Was going through MDN's tutorials and wanted expand on a part which is where I ran into this issue. When I ran the code below:
let examScore = 45;
let examHighestScore = 70;
examReport = `You scored ${ examScore }/${ examHighestScore } (${ Math.round((examScore/examHighestScore*100)) }%). ${ examScore >= 49 ? 'Well done, you passed!' : 'Bad luck, you didn\'t pass this time.' }`;
it returns "You scored 45/70 (64%). Bad luck, you didn't pass this time." When I set a new value to examScore, the return doesn't reflect the new value.
I set a new value for examScore then ran examReport again like below.
examScore = 60;
examReport;
I was expecting:
"You scored 60/70 (86%). Well done, you passed!"
What I got was:
"You scored 45/70 (64%). Bad luck, you didn't pass this time."
I even checked examScore and it returns 60.
Not sure if its a developer console or js issue. Any guidance would be appreciated!
examReport has been already evaluated in previous line.
You need to set it again as new template string and it will work.
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?
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 9 years ago.
Improve this question
If the title isn't clear enough, which is probably the case, here's what I mean :
Is it better to do this :
function example() {
if (condition) {
//The whole function code
}
}
or
function example() {
if (!condition) {
return;
}
//The whole function code
}
What is the cleanest way ?
Edit : I am not asking for your opinion. I'm wondering if there's any rule/convention/performance improvement.
I prefer the second way, "golden path" style.
Since I started to write all of my code like that I've found it to be a lot easier to read later. Perform checks in the order that makes sense, and return as soon as anything unexpected happens.
Wrapping code in a new block also adds indentation, which is kind of a waste of screen real-estate.
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);