How do I do this if statement? [closed] - javascript

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 3 years ago.
Improve this question
I am attempting to learn code from scratch and am doing the pre course work for lambda and I have hit a hard brick wall lol. I'm a bit older (36) and have no college education and wanted to pursue this for a while but always figured I wasn't smart enough but here I am attempting it. I am on the section of logical operators and if/else statements. I sort of get the concept but I think I am just not following how they word certain questions. I have been on the following question for about three hours and am ready to put my head thru the computer. Any help would be greatly appreciated.
function exerciseThree(typeOfPizza){
let lovesPizza;
// In this exercise, you will be given a variable, it will be called: typeOfPizza
// You are also given another variable called: lovesPizza;
// Using an if/else statement assign lovesPizza to true if typeOfPizza is 'pepperoni', assign it to false if it is 'olives'

Everyone starts at the beginning. Good on you for spending the time to learn. The people who are downvoting are probably doing so because you're supposed to show what you've tried so far in your post. When you get the chance, please do so. But here's the code before this question gets too many downvotes to be answered. I hope it helps. I trust you'll take the time to study it and learn from it. If you have questions about it, leave a comment.
function exerciseThree(typeOfPizza){
let lovesPizza;
if(typeOfPizza == 'pepperoni'){
lovesPizza = true;
}
else if(typeOfPizza == 'olives'){
lovesPizza = false;
}
}

Related

I need a Javascript REGEX for Integers between 18 and 140 inclusive [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 5 years ago.
Improve this question
I'm still trying to find my way around Javascript regular expressions, and I have not been able to deduce how to write this regex I need.
Please I need a Javascript regex basically for age. I want to allow anybody between ages 18 and 140. With 18 being allowed and 140 being allowed. I want a regex that basically accepts 18,19,20,21,...138,139,140. and ONLY THIS.
I'd appreciate any help.
P.S. I hope I just get a simple answer without Stack Overflow closing the question down and saying 'duplicate' or this or that... if not this might be my very last question on stack overflow, because sadly, it SEEMS like Stack Overflow is making it harder to ask a simple question. The point they want you to do a TON OF RESEARCH...or at least a lot, before you ask a question. Even though many times we ask questions precisely because we DON'T have the the time to do a lot of research. :: sigh ::
When Answering your question, I am not the person to ask why you need regex for this.
And the regex you want is
/^(1[89]|[2-9][0-9]|1([0-3][0-9]|40))$/
Sample
var age=/^(1[89]|[2-9][0-9]|1([0-3][0-9]|40))$/;
console.log(age.test(18));
console.log(age.test(140));
console.log(age.test(12));
console.log(age.test(142));
But, you can simply use the following code to test
if(age>=18 && age<=140)
That is
function test(age){
return age>=18 && age<=140;
}
console.log(test(18));
console.log(test(140));
console.log(test(12));
console.log(test(142));
Try this:
/^(1[89]|[2-9]\d|1[0-3]\d|140)$/
Let me know if that does what you need.

Is it good practice to replace short code that yields a boolean for use in conditionals with a function [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 6 years ago.
Improve this question
I have on several lines of code a conditional like this:
if ( i < DEVICES ) { .... some code
This code is repeated several times in my code, 15 times to be exact.
The thing is that since this is repeated I created a function that replaces the validation:
function isDevice(i) {return i < DEVICES; }
And replaced all my conditionals with the function:
if ( isDevice(i) ) { ... some code
Now I am thinking, even though the code is repeated several times, is it really worth using the function? I am asking because it's more lines of code that I am using to abstract something that is repeated 15 times, but is a very simple validation.
Yes it is. If you had to change something about that condition (e.g. you made a mistake and it has to be
i < DEVICES-1
or something like that.
You don't have to change it 15 times, but only once.
As commented by #str, readability is king. If your program is of any use, it will spend a lot more time being maintained than being developed. So, when in doubt, always code for a future you (or a future colleague) that will have no idea of what you were thinking when you originally wrote the code.
Since i < DEVICES-1 is less readable than isDevice(i), I would vote for using the function.

Value of a number going up [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
Right, i honestly have no idea what to even search to find this out! i have searched many phrases trying to find out and i keep coming back with nothing.
I have a client who has asked if it is possible to have a number like 2.00000000 and have it constantly going up at a variable speed. The task he wants is to have is for different users to have a number that keeps going up (for what reason i don't know, i said ill look into it for him to find out if there is a plugin or something).
But either way he wants it to b able to even while the user is offline the value still continues to go up.
Is there such a plugin to achieve this? cheers
If it is at variable speed couldn't he just check the date time at which he initialize the sequence and compare it to the time at which he checks the number. The difference in time would be that offset he would call variable speed
For example if he wanted the number to increase 10 every 24 hours then:
double intialTimeInSeconds = //Whatever the Start time would be
double amountGainedEverySecond = 24/10/60/60;
double nowTime = //whatever time it is now;
return (nowTime - initialTimeInSeconds)*amountGainedEverySecond;

Generic functions: JavaScript or PHP? [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 8 years ago.
Improve this question
While working on a recent project, I began wondering when somebody may use JavaScript vs. PHP for a generic function. Take this basic function (in JS) as an example, which simply returns whether or not a number falls within a particular range:
function range(num, var1, var2) {
if ((num >= var1) && (num <= var2)) {
return true;
}
else {
return false;
}
}
For something that doesn't query a database, nor is it information that should be — or needs to be — indexed for SEO (I know by default JavaScript will not be indexed), then my inference would be that JavaScript would be sufficient. But at the same time, PHP could be as well.
Basically, if the ONLY point of the application were a simple function like above (not that I can see a reason for that, but I digress...), then which langauge would be better to write this in? JavaScript or PHP?
Would love any insight as to which would be the best method to use and why. I recognize there is no right or wrong answer necessarily, but would like to hear arguments for or against one over the other.
Thanks!
As you point out, there is no necessarily right or wrong answer.
I would say that it depends:
-Is it a problem for people to be able to reverse engineer the code?
-Is it a problem if it does not execute because JavaScript might be disabled?
-Is it preferable to have code execute client-side versus server-side from a performance point of view?
-Does the content generated by the output of this function qualify as something you might want indexed by Search Engines?
Depending on the importance of the above criteria/questions, JavaScript might be disqualified.
From the points above, the common thing seems to be that choosing for JavaScript is more likely to lead to potentially undesirable side-effects.
The safest bet, from what I theorize, is therefore the server-side language, PHP.

When to use continue [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 9 years ago.
Improve this question
I've been reading JavaScript: The good parts. Theres a section: The Bad Parts. This is straight from the book:
continue Statement
The continue statement jumps to the top of the loop. I have never seen a piece of code that was not improved by refactoring it to remove the continue statement.
So my question is, is there ever a situation continue is required?
There are strong opinions on both sides, wether continue is useful or not. But it is never required. You can always find a way around it, if you want.
There are some believe that if there is a continue in your loops, that it should probably be refactored or rewritten. There are legitimate uses of continue, but they can usually be avoided. To give an example:
for (var i = 0; i < particles.count(); ++i)
{
if (OutOfBounds(particles[i])) {
RemoveFromArray(particles[i]));
++i;
continue;
}
}
In this case, the continue is unnecessary because there is nothing else in the loop, but it would be if you were to do something else after the initial check.
Also, it helps with readability, if you have a complicated loop with nested conditionals. For example, you may have a bunch of extraneous variables like quitLoop1 and quitLoop2 and so on.
In my experience, the continue statement is useful if a condition in a loop is met, and you don't want to execute the rest of the code in the loop on that iteration. The reason why you'll get mixed opinions on its usefulness is that many will argue there are better ways to rewrite your code so you don't have to use it. In my opinion that argument is strictly for readability purposes.
I have never used continue in javascript because most of the javascript I write is simple DOM manipulation. However, I have used continue in various Python programs, and it can be useful (as mentioned above) to skip over the loop under certain conditions.

Categories

Resources