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])
Is there a way to simplify this statement to be a 'prettier' comparison:
a <= 1 ||
b <= 1 ||
d <= 1 ||
!c;
You could put a, b, and d in an array, and check whether some of them are <= 1:
[a, b, d].some(num => num <= 1) || !c
I have 9 random integer numbers. Is there a faster way how to find maximum without explicitly compare everything?
I am performing this operation milion times per frame and this is quite slow.
function calculateMax(a, b, c,
d, e, f,
g, h, i){
var max = (a > b) ? a : b;
var max2 = (c > d) ? c : d;
var max3 = (e > f) ? e : f;
var max4 = (g > h) ? g : h;
max = (i > max) ? i : max;
max = (max2 > max) ? max2 : max;
max = (max3 > max) ? max3 : max;
max = (max4 > max) ? max4 : max;
return max;
}
The results seem rather disappointing after running few times, but posting in case someone wants to do better test on other browsers, or improve (might be faster with SIMD instructions)
function max1(a, b, c, d, e, f, g, h, i) { // shortened calculateMax
var max = (a > b) ? a : b; var max2 = (c > d) ? c : d;
var max3 = (e > f) ? e : f; var max4 = (g > h) ? g : h;
max = ( i > max) ? i : max; max = (max2 > max) ? max2 : max;
max = (max3 > max) ? max3 : max; max = (max4 > max) ? max4 : max;
return max;
}
function max2(a, b, c, d, e, f, g, h, i) {
if (a < b) a = b; if (a < c) a = c; if (a < d) a = d; if (a < e) a = e;
if (a < f) a = f; if (a < g) a = g; if (a < h) a = h; if (a < i) a = i;
return a;
}
//function max(a, b) { return a - ((a -= b) & (a >> 31 )); }
//function max(a, b) { return (a - b >>> 31) * b | (b - a >>> 31) * a }
function max(a, b) { return (a - b >> 31) & b | (b - a >> 31) & a }
function max3(a, b, c, d, e, f, g, h, i) {
return max(a, max(b, max(c, max(d, max(e, max(f, max(g, max(h, i))))))))
}
function max4(a, b, c, d, e, f, g, h, i) {
a = (a - b >> 31) & b | (b - a >> 31) & a
a = (a - c >> 31) & c | (c - a >> 31) & a
a = (a - d >> 31) & d | (d - a >> 31) & a
a = (a - e >> 31) & e | (e - a >> 31) & a
a = (a - f >> 31) & f | (f - a >> 31) & a
a = (a - g >> 31) & g | (g - a >> 31) & a
a = (a - h >> 31) & h | (h - a >> 31) & a
return (a - i >> 31) & i | (i - a >> 31) & a
}
var l = console.log, p = performance
var t = p.now(), m = max1(1,2,3,4,5,6,7,8,9); t -= p.now(); l(m, 1, -t)
var t = p.now(), m = max2(1,2,3,4,5,6,7,8,9); t -= p.now(); l(m, 2, -t)
var t = p.now(), m = max3(1,2,3,4,5,6,7,8,9); t -= p.now(); l(m, 3, -t)
var t = p.now(), m = max4(1,2,3,4,5,6,7,8,9); t -= p.now(); l(m, 4, -t)
The best way would be to use native implementation:
Array.max = function( array ){
return Math.max.apply( Math, array );
};
console.log(Array.max([1,2,9,8,3,2,1,2,5])); // 9
cf: https://johnresig.com/blog/fast-javascript-maxmin/
I would like to simplify a code snippet where I have one main loop in which I put 2 if statements. The first if statement is about a test "if (test1 or test2)" and the second one is a test "if (test1 and test2)".
Curently, to differentiate them, I have to put at a higher level (but still in the main loop) another "if" (test on diagExpression boolean, see below); here's the code :
// Main loop
while (Hit.arrayCurrent[Hit.coordCurrent[0]+k][Hit.coordCurrent[1]+l] == Hit.currentPlayer[1])
{
if (diagExpression)
{
if ((a > b) || (b > c))
return;
else if (d)
{
//do stuff1
}
}
else
{
if ((a > b) && (b > c))
return;
else if (d)
{
//do stuff1
}
}
}
I don't know how to do for simplifying this code snippet and avoiding to use the stuff1 2 times.
If anyone could see a solution.
UPDATE :
diagExpression is computed before the main loop :
// Boolean for 2 versions
var diagExpression = false;
if (Hit.direction == 'rightbottom')
{
diagExpression = true;
shift_x = 1;
shift_y = 1;
factor_x = 1;
factor_y = 1;
limit_x = 7;
limit_y = 7;
}
else if (Hit.direction == 'left')
{
shift_x = -1;
shift_y = 0;
factor_x = -1;
factor_y = 1;
limit_x = 0;
limit_y = -1;
}
...
// Main loop
while (Hit.arrayCurrent[Hit.coordCurrent[0]+k][Hit.coordCurrent[1]+l] == Hit.currentPlayer[1])
I use different directions values into my code and this boolean is true if I have diagonal directions and false for vertical/horizontal directions.
First you could simplify your code so that you'd need to write the stuff1 only once:
while (…) {
if (diagExpression) {
if ((a > b) || (b > c))
return;
} else {
if ((a > b) && (b > c))
return;
}
if (d) {
// do stuff1
}
}
or even
while (…) {
if (diagExpression ? (a > b) || (b > c) : (a > b) && (b > c)) {
return;
} else if (d) {
// do stuff1
}
}
and then you can get to the more advanced stuff, like
while (…) {
if ((a > b) + (b > c) >= 2 - diagExpression) {
return;
} else if (d) {
// do stuff1
}
}
though that gets rather unreadable, and might even be slower.
You can use this way :
if (diagExpression) {
if ((a > b) || (b > c))
return;
if ((a > b) && (b > c))
return;
}
if (d){
//do stuff1
}
This is what I believe #Daniel Daranas means as well.
I have an entry level question for javascript. When executing the following the result is
false
true
Some numbers are equal
number, number
numbernumbernumber
Why are the first 2 values different?
Why is the first one wrong?
JS:
function sort3(a, b, c)
{
document.getElementById("output").innerHTML =
(typeof a) + (typeof b) + (typeof c);
if(a > b > c) { return [a, b, c];
else if(a > c > b) return [a, c, b];
else if(c > a > b) return [c, a, b];
else if(c > b > a) return [c, b, a];
else if(b > c > a) return [b, c, a];
else if(b > a > c) return [b, a, c];
else return "Some numbers are equal";
}
HTML:
<p id="ex1"></p>
<p id="output"></p>
<script type="text/javascript">
var m = parseFloat(1);
var k = parseFloat(2);
var l = parseFloat(3);
var q = typeof m;
var w = typeof k;
var e = typeof l;
var res1 = (l>k>m) + "</br>";
res1 += (m<k<l) + "</br>";
var res2 = sort3(m,k,l) + "</br>";
var res3 = (typeof m) + ", " + (typeof Number(m))
document.getElementById("ex1").innerHTML = res1 + res2 + res3;
</script>
To do compound comparisons in JavaScript (or any other language syntactically derived from B), you don't do things like
(l>k>m) // <=== Wrong
Instead, if you want to know if l is greater than k and k is greater than m, you use && (the logical "and" operator), like this:
(l>k && k>m)
Details:
Your original expression (l>k>m) breaks down into this:
((l>k)>m)
which means you'll get one of these:
(true>m)
// or
(false>m)
When comparing a boolean to a number, the boolean is coerced to a number, so that becomes in effect:
(1>m)
// or
(0>m)