I only want to show unique variables. Is there an easier or cleaner way to compare three js variables?
if (a==b) {
if (a==c) {
//show only a
} else {
//show a and c
}
} else {
if (a==c) {
//show a and b
} else {
//show a and b and c
}
}
Not really, no:
if (a == b && a == c) {
// show a
} else if (a == b) {
// show a and c
} else if (a == c) {
// show a and b
} else {
// show a, b, and c
}
I know you have not specified to use jQuery, but this is another way you could do it (should you choose to use Jquery)
var array1 = [];
var a = 2, b = 3, c= 2;
array1.push(a);
array1.push(b);
array1.push(c);
console.log(array1);
var unique = $.unique(array1);
console.log(unique);
P.S. - I have considered the variables to be numbers.
Related
I have this snippet that is is getting 3 numbers and its working distinguing them. If one of 3 numbers is diferent than the others it must return its correspondend.
An input example:
1 1 0
0 0 0
1 0 0
output must be:
C
*
A
The approach i had was that one:
var input = require('fs').readFileSync('stdin', 'utf8')
var lines = input.split('\n')
for (let i = 0; i < lines.length; i++) {
var round = lines[i].split(' ').map(i => parseInt(i))
// console.log(round); [1, 1, 0]
var A = round[0]
var B = round[1]
var C = round[2]
if(A === B && A === C){
console.log("*");
} else if (A === B && A !== C) {
console.log("C");
} else if (A !== B && A === C) {
console.log("B");
} else if (A !== B && A !== C) {
console.log("A");
}
}
I'm not sure what the problem is exactly but if it is to try to minimize the code in some way one thing to notice is that we don't care whether the the values are 0 or 1 only whether the players have chosen the same or not so once we have the values of A, B and C for a round we can just do:
(UPDATE: thanks to a comment from #Samathingamajig a redundant != comparison has been removed)
console.log( ((A==B)&&(B==C)) ? '*' : (A==B) ? 'C' : (A==C) ? 'B' : 'A' );
Like #Samathingamajig said:
function _0or1(arr) {
[A,B,C] = arr
if (A === B && A === C) {
console.log("*");
} else if (A === B) {
console.log("C");
} else if (A === C) {
console.log("B");
} else {
console.log("A");
}
}
_0or1([0,0,0])
_0or1([1,0,0])
_0or1([0,1,0])
_0or1([0,0,1])
_0or1([1,1,0])
_0or1([0,1,1])
_0or1([1,0,1])
_0or1([1,1,1])
Currently, the only way I know of to do what the title states is by writing the following:
var a = 3, b = 5, c = 3;
if (a === b && a === c) {
// code
}
Or by using the ternary operator:
(a === b && a === c) ? /* code */ : /* else */
Is there a way to check a against both b and c? Something like this perhaps:
if (a === (b && c)) {
// code
}
Obviously this doesn't work as intended, which is why I'm asking the question. Any help is appreciated.
This is not a duplicate of the other 2 - those two are using the OR operator. I'm asking about the AND operator.
For a simple case like this? Definitely not. There are some handy little tricks you can use if you build an array, however. For example:
var my_array = [3, 5, 3];
if(my_array.every(function(el) {return el == my_array[0];})) {
// code
}
Unfortunately no. That's what you have. Unless you convert it to an Array and test with a loop.
if a,b and c are all numbers then you can do simple maths to check for parity of b and c against a.
Simply add b and c, divide by 2 to get the average number and use that to compare against a.
var a = 3, b = 5, c = 3;
a === (b + c)/2
? console.log('yes')
: console.log('no')
// console gives "no"
var a = 3, b = 3, c = 3;
a === (b + c)/2
? console.log('yes')
: console.log('no')
// console gives "yes"
var a = 3, b = 4, c = 2;
a === (b + c)/2
? console.log('yes')
: console.log('no')
// console gives "yes"
OK, so im trying to make a small sudoku generator, and i didn't get far before i met a problem. I meet the error "Maximum call stack size exceeded" when the output in console.log is "a == b". Here is my code (Im not an experienced coder FUI)
function en_Til_Ni() {
return Math.floor(Math.random()*9)+1;
}
var enTilNi = en_Til_Ni();
console.log(enTilNi);
var a = en_Til_Ni();
console.log("a" + a);
var b = en_Til_Ni();
console.log("b" + b);
var c = en_Til_Ni();
console.log("c" + c);
console.log("---------------------------------------------");
function ikkeLike() { //this is where it goes wrong
if (a != b) {
console.log(a, b);
}
else if (a == b) { // It logs the numbers just fine, untill a == b
ikkeLike();
}
}
ikkeLike();
Of course you are getting this result, because you have no working exit condition.
You neeed to change the random values and check again until the wanted state is reached.
function en_Til_Ni() {
return Math.floor(Math.random() * 9) + 1;
}
function ikkeLike() {
a = en_Til_Ni();
b = en_Til_Ni();
if (a !== b) {
console.log(a, b);
} else { // no need for the opposite check
ikkeLike();
}
}
var a, b; // global variables
ikkeLike();
Better version without recursive call and a do ... while loop.
function en_Til_Ni() {
return Math.floor(Math.random() * 9) + 1;
}
function ikkeLike() {
do {
a = en_Til_Ni();
b = en_Til_Ni();
} while (a === b)
console.log(a, b);
}
var a, b; // global variables
ikkeLike();
This is little hack for your problem :
Use setTimeout to avoid this error message , app see problem with infinity loops .
Also you need to make new values for a and b !
function en_Til_Ni() {
return Math.floor(Math.random()*9)+1;
}
var enTilNi = en_Til_Ni();
console.log(enTilNi);
var a = en_Til_Ni();
console.log("a" + a);
var b = en_Til_Ni();
console.log("b" + b);
var c = en_Til_Ni();
console.log("c" + c);
console.log("---------------------------------------------");
function ikkeLike() {
if (a != b) {
console.log(a, b);
}
else if (parseInt(a) == parseInt(b) ) { // It logs the numbers just fine, untill a == b
console.log (" a == b TRUE" );
a = en_Til_Ni();
b = en_Til_Ni();
c = en_Til_Ni();
setTimeout ( ikkeLike , 1 )
console.log("cool")
}
}
ikkeLike();
OK, so im trying to make a small sudoku generator, and i didn't get
far before i met a problem.
Looks like you want to check if three values you have generated - a, b and c are not equal. And you want to keep generating the values of b and c till they are all different.
Change your method en_Til_Ni in such a way that it will keep generating random values till values are unique.
function en_Til_Ni()
{
var newValue = Math.floor(Math.random()*9)+1;
args = [].slice.call(arguments);
var isNotNew = args.some( function( item ){
item == newValue;
});
return !isNotNew ? newValue : en_Til_Ni.apply( this, args ) ;
}
Now this method will always return unique values
var a = en_Til_Ni();
console.log("a" + a);
var b = en_Til_Ni(a);
console.log("b" + b);
var c = en_Til_Ni(a,b);
console.log("c" + c);
This is a recursion without the increment part. Let's look on ikkeLike() without the first condition:
function ikkeLike() {
// We removed the first case for the simplicity
if (a == b) {
ikkeLike();
}
}
Now you can easly see that in this case it is just an infinite loop...
You need to solve this case by changing 'a' or 'b' just before the recursive call:
function ikkeLike() {
if (a == b) {
// <--Change 'a' or 'b' here before the recursive call to avoid infinite loop!
ikkeLike();
}
}
Doing a survey where a user picks :
A B or C
I then need to know if the user has picked Mostly A's, B's or C's.
I'm trying a few jQuery logics' but not having much luck, Due to expression expected error.
Is there a neater / better way to show purely which variable is the highest?
My current jQuery :
var count = 0;
var count_a = 0;
var count_b = 0;
var count_c = 0;
$('.radio-select').click(function()
{
var chosen_option = $(this).val();
if(chosen_option == 'a')
{
count++;
count_a ++;
}
if(chosen_option == 'b')
{
count++;
count_b ++;
}
if(chosen_option == 'c')
{
count++;
count_c ++;
}
check_numbers(count, count_a, count_b, count_c);
})
function check_numbers(count, a, b, c)
{
parseInt(a);
parseInt(b);
parseInt(c);
if(count == '8')
{
if ((a > b ) && (a > c))
{
alert("A is Highest");
}
if ((b > a ) && (b > c))
{
alert("B is Highest");
}
if(c > b) && (c > a))
{
alert("C is highest!");
}
}
}
jsFiddle Example
If you wanted a smaller way of doing it you could use inline if statements. Up to you if this is a better way, I like it though.
a = 5
b = 11
c = 6
console.log((a > b && a > c? a : (b > c ? b : c)))
Firstly you do not need to use parseInt() on a, b, c as they are already integers. And again count is an integer while you are comparing it to a string. This should work.
if(count == 8)
{
if ((a > b ) && (a > c))
{
alert("A is Highest");
}
else if ((b > a ) && (b > c))
{
alert("B is Highest");
}
else
{
alert("C is highest!");
}
You need to fetch the value returned by parseInt. Use it like: a = parseInt(a); and same for the other variables before comparing them in the if...else.
function check_numbers(count, a, b, c)
{
var x = parseInt(a),
y = parseInt(b),
z = parseInt(c);
if(count == 8)
{
var result = (x > y ? (x > z ? x : z) : (y > z ? y : z));
}
}
#StuBlackett you can consider adding the values and labels to an array then sorting Descending and returning the lable at the top.
function CompareIndexZero(a, b) {
if (a[0] < b[0]) return 1;
if (a[0] > b[0]) return -1;
return 0;
}
function myFunction() {
var count_a = 2;
var count_b = 5;
var count_c = 4;
var arrHighest = [];
arrHighest.push([count_a, "A"]);
arrHighest.push([count_b, "B"]);
arrHighest.push([count_c, "C"]);
arrHighest.sort(CompareIndexZero);
alert(arrHighest[0][1] + " is the highest");
}
Here is a modified version of check_numbers() that works as intended if I got you right. The point I want to make is the use of Math.max() to find the highest number from a selection of numbers.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
function check_numbers(count, a, b, c) {
if(count === 8) {
var numArray = [a, b, c];
var highest = Math.max.apply(null, numArray);
console.log(highest);
if (highest === a) {
console.log('a is highest');
} else if (highest === b) {
console.log('b is highest');
} else if (highest === c) {
console.log('c is highest');
}
}
}
check_numbers(8, 1 , 2, 5);
check_numbers(8, 5, 2, 1);
check_numbers(8, 1 , 5, 2);
Have you also taken into account that multiple answers could share the highest count?
My 2 cents on that:
var count = count_a = count_b = count_c = 0;
$('.radio-select').on('click', function() {
var chosen_option = $(this).val();
if (chosen_option == 'a') {
count_a++;
}
else if (chosen_option == 'b') {
count_b++;
}
else if (chosen_option == 'c') {
count_c++;
}
if (++count == 8) {
check_numbers(count_a, count_b, count_c);
}
});
function check_numbers(a, b, c) {
var highest = ((a > b && a > c) ? a : (b > c)? b : c),
multiple = false,
alertText = '';
if (a == highest) {
alertText += 'A';
}
if (b == highest) {
if (alertText != '') {
multiple = true;
alertText += ' and ';
}
alertText += 'B';
}
if (c == highest) {
if (alertText != '') {
multiple = true;
alertText += ' and ';
}
alertText += 'C';
}
alert(alertText + ' ' + (multiple ? 'are' : 'is') + ' highest!');
}
so I wrote this for my first computer science class assignment. However, the page returns that if input is gpa(A), the results is 3. It's like only the first conditionals if is running. I switched around the A, B ,C and 2, 3, 4 but it's always the first if no matter what the gpa(r) is. How does this happen?
var gpa = function(r) {
if (r = "B"){
return 3;
}
if (r = "C"){
return 2;
}
if (r = "A"){
return 4;
}
}
In order to compare between two values you should use == or === and not = which assigns a value.
var gpa = function(r) {
if (r == "B"){
return 3;
}
if (r == "C"){
return 2;
}
if (r == "A"){
return 4;
}
}
Read here about the difference between == and ===.