Compare 3 values - Show the highest - javascript

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!');
}

Related

Efficient way to get longest palindrome subsequence in a string

I'm trying to implement a function that takes a string as input and returns the longest palindrome subsequence in the string.
I've tried using dynamic programming and have come up with the following code:
function longestPalindromicSubsequence(str) {
let n = str.length;
let dp = Array(n);
for (let i = 0; i < n; i++) {
dp[i] = Array(n);
dp[i][i] = 1;
}
for (let cl = 2; cl <= n; cl++) {
for (let i = 0; i < n - cl + 1; i++) {
let j = i + cl - 1;
if (str[i] === str[j] && cl === 2)
dp[i][j] = 2;
else if (str[i] === str[j])
dp[i][j] = dp[i + 1][j - 1] + 2;
else
dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]);
}
}
return dp[0][n - 1];
}
However, this code doesn't seem to be giving me efficient and better results for all test cases. The Time and Space Complexity is also be reduced. I've been struggling with this for days and can't seem to find the issue. Can someone help me figure out what's going wrong and how to fix it?
Oh, I think Dynamic Programming does not work with this sort of problem, because it does not break down recursively, i.e. to find the longest palindrome in a string, you don't need all second-largest palindromes. You can just check at each position and see if it is the center of a palindrome longer than any before. This can be solved with a greedy algorithm:
const pals = "asd1234321fghjkl1234567887654321qwertzu1234321"
function palindromeAtPos(str, pos, checkEven = false){
let ix = 0
const off = checkEven ? 2 : 1
while(pos-ix-1 >= 0 && pos+ix+1+off < str.length && str[pos-ix-1] === str[pos+ix+off]){
ix++
}
return ix === 0 ? str[pos] : str.substring(pos-ix, pos+ix+off)
}
function longestPalindrome(str){
let longest = ''
for(let i = 1; i < str.length; i++){
const odd = palindromeAtPos(str, i)
longest = odd.length > longest.length ? odd : longest
const even = palindromeAtPos(str, i, true)
longest = even.length > longest.length ? even : longest
}
return longest
}
console.log(longestPalindrome(pals))
On paper (and for a string like aaaaaaaaaa), this has quadratic complexity, but for most strings, it will be almost linear.
/*
* s => string
* return [] of strings witch have the max lenth
*/
function maxLenPalindromes(s) {
const l = s.length
let c, z, zz, a, b, a1, b1, maxl = 0, result = []
if (l < 2) return result
for (c = 0; c < l - 1; c++) {
a = -1
if (maxl>(l-c)*2+1) return result
if (c > 0 && s[c - 1] == s[c + 1]) {
zz = Math.min(c, l - c - 1)
for (z = 1; z <= zz; z++) {
if (s[c - z] != s[c + z]) {
a = c - z + 1; b = c + z
break
}
else if (z == zz) {
a = c - z; b = c + z + 1
break
}
}
if (a >= 0) {
if (b-a > maxl) {
result = [s.slice(a, b)]
maxl = b-a
}
else if (b-a == maxl) {
result.push(s.slice(a, b))
}
}
}
a=-1
if (s[c] == s[c + 1]) {
if (c == 0 || c == l - 2) {
a = c; b = c + 2
}
else {
zz = Math.min(c, l - c - 2)
for (z = 1; z <= zz; z++) {
if (s[c - z] != s[c + z + 1]) {
a = c - z + 1; b = c + z + 1
break
}
else if (z == zz) {
a = c - z; b = c + z + 2
break
}
}
}
if (a >= 0) {
if (b-a > maxl) {
result = [s.slice(a, b)]
maxl = b-a
}
else if (b-a == maxl) {
result.push(s.slice(a, b))
}
}
}
}
return result
}
const s1="112233111222333"
const s2="11_22_33_111_222_333"
const s3="12345_54321xqazws_swzaq_qwertytrewq"
const s4="sdfgsdfg1qqqqqAAAAA_123456789o o987654321_AAAAAqqqqq;lakdjvbafgfhfhfghfh"
console.log(maxLenPalindromes(s1))
console.log(maxLenPalindromes(s2))
console.log(maxLenPalindromes(s3))
console.log(maxLenPalindromes(s4))

Programming logic - Compare 3 values - JS

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])

Number Formatting for large integer numbers in Javascript

I know there are in-build function in javascript like toLocalString() to achieve number formatting. But this question is purely for learning and logic understanding.
I have a function in javascript that formats given number in Indian Number formatting standards (eg: 1,234 | 12,21,123 | etc)
Code
function formatter(input) {
var inputStr = input.toString(), l = inputStr.length;
var c = 1, f = 0;
console.log(l);
for (var x=l-1; x>=0; x--) {
if (x === 0) {
continue;
}
if (c === 3 && f === 0) {
inputStr = inputStr.substring(0, x) + ',' + inputStr.substring(x);
f = 1;
c = 0;
} else if (c % 2 === 0 && f === 1) {
inputStr = inputStr.substring(0, x) + ',' + inputStr.substring(x);
c = 0;
}
c++;
}
return inputStr;
}
Now this works for most part (as far as I have test, do point out bugs if you spot any). But my question is how do I handle large number in this, i.e. how do I handle values greater than 9007199254740991.
Hope this now fixes the issue:
function formatter(inputStr) {
inputStr+="";
let c = 1, f = 0;
for (let x=inputStr.length-1; x>=0; x--) {
if (x === 0) continue;
if (c === 3 && f === 0) {
inputStr = inputStr.substring(0, x) + ',' + inputStr.substring(x);
f = 1;
c = 0;
} else if (c % 2 === 0 && f === 1) {
inputStr = inputStr.substring(0, x) + ',' + inputStr.substring(x);
c = 0;
}
c++;
}
return inputStr;
}
//========= Tests =======
console.log(formatter(9007199254740991));
console.log(formatter("900719925474093454549341"));
console.log(formatter("123456678890987665443221112345667676766545434243"));

While loop runs forever in JavaScript

I've writen a function in JavaScript that checks through all the combinations of three digits between 1 and 9 and gives me the number of combinations that follow this pattern
√(x^2 + y^2 + z^2) = a natural number (a full number like 24 or 34 but not 2.54)
√ = square root , ^2 = to the power of 2,
My problem is that whenever I run the function the computer gets stuck and the function never ends so it doesn't return an answer.
I would very much appreciate if someone could tell me whats wrong with it
(I'm running my programs on the chrome browser console)
function mmd() {
var chk = false;
var a = 1;
var b = 1;
var c = 1;
var d = 1;
var e = 0;
while(chk != true) {
d = Math.sqrt(Math.pow(a, 2)+Math.pow(b, 2)+Math.pow(c, 2));
if( d == d.toFixed(0)) {
e++;
}
else {
if((b == 9) && (a == 9) && (c == 9)) {chk = true;}
else if((a == 9) && (b == 9)) {c++;}
else if(b == 9) {b = 1; a++;}
else if(c == 9) {c = 1; b++;}
else if(c < 9) {c++;}
}
}
return e
}
This part of the code is causing it to never end:
if (d == d.toFixed(0)){} else {}
If the result of the formula is an integer, you add 1 to e, but you don't increment the other variables, because of the else. It keeps doing e++ for ever. So you need to remove that else.
I also took the liberty or removing that chk variable, and instead used while(true), which will be ended by a return of the final result:
function mmd() {
var a = 1, b = 1, c = 1, d, e = 0;
while(true) {
d = Math.sqrt(Math.pow(a, 2)+Math.pow(b, 2)+Math.pow(c, 2));
if( d == parseInt(d, 10)) {
e++;
}
if((b == 9) && (a == 9) && (c == 9)) {return e;}
else if((a == 9) && (b == 9)) {c++;}
else if(b == 9) {b = 1; a++;}
else if(c == 9) {c = 1; b++;}
else {c++;}
}
}
alert(mmd());
It gets stuck once it hits the e++ block and never increases a, b, or c.
function mmd()
{
var keepGoing = true;
var a = 1, b = 1, c = 1, d, e = 0;
while(keepGoing)
{
// calculate d
d = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2) + Math.pow(c, 2));
// check if it is a whole number
if(d == d.toFixed(0)) e++;
// if we're done then stop
if(a == 9 && b == 9 && c == 9){ keepGoing = false; }
// if c is less than 9 then increase it
else if(c < 9){ c++; }
// if c is 9 and b is less than 9 then set c back to 1 and increase b
else if(b < 9){ c = 1; b++; }
// if c is 9 and b is 9 then set both back to 1 and increase a
else if(a < 9){ c = b = 1; a++; }
}
return e;
}

simplification of 2 if statements into a loop

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.

Categories