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 7 years ago.
Improve this question
I have 2 variables... both of them can get a status (0,1,2,"xxx")
What would be the best way to compare these variables? Or are a lot of if's and else if's the best way?
if((Status1 && Status2) == 2){
connectorImage = "connector.png";
}
else if((Status1 && Status2) == 1){
connectorImage = "connector_grey.png";
}
else if(Status1 == 1 && Status2 == 2){
connectorImage = "connector_greytogreen.png";
}...
You could try something like
if(Status1 == Status2){
//Do something
}else if(Status1 < Status2){
//Do something else
}else if(Status2 > Status1){
// Do something else
}
This should work, unless if all 16 combinations require a unique action. Then you're stuck with lots of if's and else's
it depends on what exactly you need to compare.
e.g.: If you need to get the correct image to a status i would use something like this:
function getImageByStatus(int status) {
switch (status) {
case 1:
return "connector.png";
case 2:
return "connector_grey.png";
}
return "";
}
In case you have more complex comparisons, your only way might be some if-statements
You can construct connector image from statuses:
var connectorImage = status1 + "_" + status2 + ".png";
and rename you images to something like "0_0.png", "1_0.png", etc.
Or rename your status to something more meaningfull
Or create a mapping between combined statues and connector names:
var statusesToConnectorImages = {
"0_0" : "connector.png",
"0_1" : "connector_grey.png"
}
var connectorImage = statusesToConnectorImages[status1 + "_" + status2];
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
Is there any difference between else and !== in JavaScript?
I am beginner in JavaScript and I can't find their difference, for example in this code I expected the text to appear when I enter other days.
const day= prompt(`Insert The Day`);
if(day===`monday`){
console.log(`Pray All Day Long🙏🏽`)
}
else if(day===`tuesday`){
console.log(`Code All Day Long`)
}
else if(day===`wednesday`){
console.log(`Meditate All Day Long`)
}
else if(day===`Thursday`){
console.log(`Design All Day Long`)
}
else if(day!==`monday`){
console.log(`Why Not Monday?`)
}
else{
console.log(`Invalid Day`)
}
These 2 are entirely different operators.
else is to be applied after an if block, and is run if the condition from the if block wasn't met.
== is a "loose" equals, which means that values of different types with the same value are considered to be equal. This means that 1 == "1" will return true, even if 1 is an int and "1" is a string.
=== checks for absolute equality, which means that only values with the same type and value are considered equal. This means that 1 === "1" is false, but 1 === 1 is true.
!== is the opposite of ===, which means that any value with a different type or value is considered not equal. This means that 1 !== 2 is true, and 1 !== "1" is as well.
Firstly, I think you should learn about if..else. You can read more here, and the !== is an operator
And in your case, I suggest using switch
switch(day) {
case 'monday': console.log('Pray All Day Long🙏🏽');
break;
case 'tuesday': console.log('Code All Day Long');
break;
...
default: console.log('Invalid Day');
break;
}
Finally, the better performance sulotions are Map and using [key, value] of Object
Hope useful for you.
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 months ago.
Improve this question
i have a problem in laravel with links in a fullcalednar script. I need the links to be only with date: 2022-06-28 not with T00:00:00+03:00. i do this but not work any ideea?
// timeZoneOffset is in minutes
function buildIsoString(marker, timeZoneOffset, stripZeroTime) {
if (stripZeroTime === void 0) { stripZeroTime = false; }
var s = marker.toISOString();
s = s.replace('.000', '');
if (stripZeroTime) {
s = s.replace('T00:00:00Z', '');
}
if (s.length > 10) { // time part wasn't stripped, can add timezone info
if (timeZoneOffset == null) {
s = s.replace('Z', '');
}
else if (timeZoneOffset !== 0) {
s = s.replace('Z', formatTimeZoneOffset(timeZoneOffset, true));
}
// otherwise, its UTC-0 and we want to keep the Z
}
return s;
}
I don't fully understand the problem. So, please bear with me.
If you want to strip the hours, minutes, seconds and miliseconds from an ISO string (from Date.toISOString()) you can simply do the following:
Option 1: isoString = isoString.slice(0, 10);
Option 2: isoString = isoString.replace(/T.*$/, "");
I'm not sure this answers your question. I would need to know more about how and where the buildIsoString function is being called and what the formatTimeZoneOffset does.
I hope this helps.
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 trying to use 1 If statement to check that both values are not zero
if ((minvalue !== 0) && (maxvalue !== 0)) {
// Both are not 0
}
else
{
// Both values are 0
}
I can get it to work by using two if statements
if ((minvalue !== 0){
if(maxvalue !== 0){
// Both values are not zero
}
}
But I'm not sure how to do it in one If.
This will also work
if (minvalue || maxvalue) {
// Both are not 0
}else {
// Both values are 0
}
Edit :
If you example doesn't work, you should consider doing
console.log(minvalue,maxvalue);
Your code works, so that's your minvalue and maxvalue which are wrong. Might be strings
Your first code block should be fine. If you want to get a bit more clever about it you could instead test that the product of both values is not zero (as anything multiplied by zero will be zero).
const minvalue = 1
const maxvalue = 3
if (minvalue * maxvalue !== 0) {
console.log('foo!') // foo!
} else {
console.log('bar...') // [not hit]
}
Also, stylistically, it's considered bad practice to leave hanging curly braces. Move your else and subsequent opening block curly brace up a line (as above).
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 6 years ago.
Improve this question
So I am reasonably new to javascript and jquery. I have been looking at tutorials which have been a good starting point however they all seem to have one question that is right, so they are able to check within a function if the users answer is correct via a if equal to statement for example.
if (answers[i] == userAnswers[i]) {
flag = true;
}
What I would like to do however, is have a different value attached to each of the potential answers for example
var 1 = 50
var 2 = 0
var 3 = 10
Question 1
var a = 1
var b = 2
var c = 3
var d = 3
Question 2
var a = 3
var b = 1
var c = 2
var d = 3
What would be the best way to do something like this?
First, an assumption:
answers[i] contains a number which is what the user picked as an answer to question i (Note: if answers[i] is in fact a character, you can get the integer value by subtracting answers[i] by the ascii value of 'a').
To do what you want to do, simply define a 2-dimensional array score[i][j], where i is the index of the question, and j is the option number (ie the asnwer to question i), then score[i][j] gives the score for this question. So let's say for question 1, the options are as you described above (ie a = good, b = wrong, ...), then you would have
//set the values for the answers
score[1][1] = good; score[1][2] = wrong; score[1][3] = wrong; score[1][4] = okay
score[2][1] = okay; score[2][2] = good; score[2][3] = wrong;
//more score setting
//get the value of the answer
var score_q1 = score[1][answers[1]]
var score_q2 = score[2][answers[2]]
ie, since answers[i] contains the value of the answer as a number, by calling score[question_nb][answers[question_nb]] you get the score for that question. In this case, there are no if statements. If you want to get the total score, just loop over all questions and sum up the score for that question.
The best way would be a key/value pair. I would try something like json. You would have your json with a key of good and a value (for points) as 50. The json would look something like below:
var kvPair = {"good":"50", "wrong":"0", "okay":"10"};
then when someone clicks an answer it would run ajax to determine the score:
$.ajax({
type: "POST",
dataType: "json",
url: "someUrl",
data: kvPair,
success: function (data) {
//do something with score data
},
error: function (event) {
ShowErrorLabel("ERROR in ajax call('someUrl'): \n" + "Error : " + event.status + " - " + event.statusText);
}
});
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 have a number for example 4, i want to get next number on multiples of 3
Multiples of three: [3,6,9,12,15,18,21,24,27,30,...]
the result must be 6
i'm looking for a javascript function
something like this:
function (myNum) { //myNum = 4;
var multiples = [3,6,9,12,15,18,21,24,27,30];
var result;
// do something!!
return result; // returns 6
}
thanks
I suggest another solution:
function getNext(num, dep){
return (((num % dep) ? dep:0) - num % dep) + num;
}
document.write(getNext(4, 3));//6
//document.write(getNext(200, 7));//203
Updated: You can use this method for finding next number on multiples of any number
There are a lot of ways you can achieve this. Here is an easy one. Increment the number until you get a multiple of three.
function multipleOfThree(num){
while(num % 3 != 0)
num++;
return num;
}
You must try before asking a question. If you stuck at somewhere then it is good to ask questions with problem. By the way here what you can try:
function multiple(number) {
return number % 3 === 0 ? ((number/3) * 3) : parseInt((number/3) + 1) * 3;
}