If condition does not follow (JavaScript) - javascript

Creating a SharePoint Portal using JavaScript and HTML where the problem is when i'm inputting a number around 100,000 to 800,000; ex(523,546) it would enter in the first if condition and do the statement below in the. even though the value is less than the given MDV "Total Estimated Freight".
The value of the pagetdv is inputted.
var pagetdvz = document.getElementById('pagetdv');
var pagesrz = document.getElementById('pagesr');
var pagemdvz = document.getElementById('pagemdv');
/------------ Maximum Declared Value ----------/
if (document.getElementById('dropct').selectedIndex == 0)
document.getElementById('pagemdv').value = 1500000;
else if (document.getElementById('dropct').selectedIndex == 1)
document.getElementById('pagemdv').value = 3000000;
/------------ Dest + 20 Ftr ----------/
if ((document.getElementById('dropdest').selectedIndex == 0) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 45900;
else if ((document.getElementById('dropdest').selectedIndex == 1) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 50000;
else if ((document.getElementById('dropdest').selectedIndex == 2) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 46700;
else if ((document.getElementById('dropdest').selectedIndex == 3) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 47583.67;
else if ((document.getElementById('dropdest').selectedIndex == 4) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 59981.33;
else if ((document.getElementById('dropdest').selectedIndex == 5) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 45900;
else if ((document.getElementById('dropdest').selectedIndex == 6) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 59000;
else if ((document.getElementById('dropdest').selectedIndex == 7) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 45900;
else if ((document.getElementById('dropdest').selectedIndex == 8) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 59,981.33;
else if ((document.getElementById('dropdest').selectedIndex == 9) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 58500;
else if ((document.getElementById('dropdest').selectedIndex == 10) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 49000;
else if ((document.getElementById('dropdest').selectedIndex == 11) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 46700;
else if ((document.getElementById('dropdest').selectedIndex == 12) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 51000;
/------------ Dest + 40 Ftr----------/
if ((document.getElementById('dropdest').selectedIndex == 0) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 89600;
else if ((document.getElementById('dropdest').selectedIndex == 1) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 95500;
else if ((document.getElementById('dropdest').selectedIndex == 2) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 91096.87;
else if ((document.getElementById('dropdest').selectedIndex == 3) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 94944.66;
else if ((document.getElementById('dropdest').selectedIndex == 4) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 119739.26;
else if ((document.getElementById('dropdest').selectedIndex == 5) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 89600;
else if ((document.getElementById('dropdest').selectedIndex == 6) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 117487.73;
else if ((document.getElementById('dropdest').selectedIndex == 7) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 89600;
else if ((document.getElementById('dropdest').selectedIndex == 8) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 119739.26;
else if ((document.getElementById('dropdest').selectedIndex == 9) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 113000;
else if ((document.getElementById('dropdest').selectedIndex == 10) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = "--Null--";
else if ((document.getElementById('dropdest').selectedIndex == 11) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 91096.87;
else if ((document.getElementById('dropdest').selectedIndex == 12) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = "--Null--";
/------------ Total Estimated Freight ----------/
if (document.getElementById('pagetdv').value > document.getElementById('pagemdv').value){
var tdva = parseFloat(pagetdvz.value) - parseFloat(pagemdvz.value);
var tdvb = tdva / 1000;
var tdvc = tdvb * 3.36;
var tef = tdvc + parseFloat(pagesrz.value);
document.getElementById('pagetef').value = tef;
}
else if (document.getElementById('pagetdv').value <= document.getElementById('pagemdv').value)
document.getElementById('pagetef').value = document.getElementById('pagesr').value;

Related

Max Value with decimal 99.999 HTML block input

I'm trying to make the input takes only the value 99.999. I don't want to use MaxLength because it would not calculate the length of the decimal digits. I don't want to use any other functions that erase when it doesn't match a specific regex. I want it to stop it in the input.
function IsCurrencyNoMinus1 (e, thisobj, min, max) {
var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode == 44) || (keyCode == 46) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode))
var inStr = $(thisobj).val();
if (ret && (keyCode == 45) && ((thisobj.selectionStart != 0) || (inStr.indexOf('-') != -1)))
ret = false;
if (ret && (keyCode == 46) && (inStr != '' && inStr.indexOf('.') != -1) && !(Math.abs(thisobj.selectionStart - thisobj.selectionEnd) == inStr.length)) {
ret = false;
}
var dotPos = (inStr.indexOf('.') != -1) ? inStr.indexOf('.') : inStr.length;
inStr = inStr.replace(/\,/g, '');
var parts = inStr.split('.');
var maxParts = max.toString().split('.');
if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57))) {
if ((parts[0].length >= maxParts[0].length) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0)
&& (thisobj.selectionStart <= dotPos)) {
ret = false;
}
if (ret && (parts[1] != undefined && parts[1].length >= 2) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0)
&& (thisobj.selectionStart > dotPos) && (thisobj.selectionStart <= dotPos + 3))
ret = false;
var firstPos = thisobj.selectionStart < thisobj.selectionEnd ? thisobj.selectionStart : thisobj.selectionEnd;
if (ret && (parts[0].length >= maxParts[0].length) && (parts[1] != undefined && parts[1].length >= 1)
&& ((dotPos - firstPos == 0 && Math.abs(thisobj.selectionStart - thisobj.selectionEnd) < 4)
|| (dotPos - firstPos == 1 && (Math.abs(thisobj.selectionStart - thisobj.selectionEnd) >= 2 && Math.abs(thisobj.selectionStart - thisobj.selectionEnd) < 4))))
ret = false;
}
if (Number(inStr) > max) {
thisobj.value = '';
ret = true;
}
if (Number(inStr) < min) {
thisobj.value = '';
ret = true;
}
// var re = new RegExp(/^\(?-?[0-9]{0,12}(\.[0-9]{0,2})?\)?$/)
// if (!re.test(inStr)) {
// thisobj.value = ""
// }
return ret
}
I found the solution! Please check the code below in case someone needs it.
function Format3DigitDecimal(e, thisobj, min, max)
{
var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode
var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode == 44) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode))
var inStr = $(thisobj).val()
inStr = inStr.replace(/\,/g, '')
if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57)))
{
if ((inStr.length >= max.toString().length) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0))
{
ret = false
}
}
if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57)))
{
if ((inStr.length == 2) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0))
{
ret = false
}
}
return ret
}

Uncaught RangeError: Maximum call stack size exceeded, Program does not stop

Hi everyone I'm making Tic-Tac-Toe Game for school. But when al boxes are checked i need the program to stop and say: you win, you lose, or draw.
But is doesn't. when you lose or win before al 9 boxes are checked it says you won or lose.
I also get this error when all boxes are checked:
Uncaught RangeError: Maximum call stack size exceeded
Code:
var button = [];
for (var i = 1; i < 10; i++) button[i] = document.getElementById('canvas'+i);
var ctx = [];
for (var i = 1; i < 10; i++) ctx[i] = button[i].getContext('2d');
var bDisabled= [];
for (var i = 1; i < 10; i++) bDisabled[i] = false;
var isResult = false;
var content = [];
function loop (x)
{
if(!bDisabled[x])
{
bDisabled[x] = true;
button[x].style.opacity = 0.7;
content[x] = 'x';
button[x].style.webkitTransform="rotatey(180deg)";
{
ctx[x].lineWidth=3;
ctx[x].beginPath( );
ctx[x].moveTo(10,10);
ctx[x].lineTo(40,40);
ctx[x].moveTo(40,10);
ctx[x].lineTo(10,40);
ctx[x].stroke();
ctx[x].closePath();
computerTurn();
}
setTimeout(checkWinner, 1000);
}
}
function computerTurn()
{
var r = Math.random();
if (r < 0.1 && !bDisabled[1]) draw0Steps(1);
else if (r < 0.2 && !bDisabled[2]) draw0Steps(2);
else if (r < 0.3 && !bDisabled[3]) draw0Steps(3);
else if (r < 0.4 && !bDisabled[4]) draw0Steps(4);
else if (r < 0.5 && !bDisabled[5]) draw0Steps(5);
else if (r < 0.6 && !bDisabled[6]) draw0Steps(6);
else if (r < 0.7 && !bDisabled[7]) draw0Steps(7);
else if (r < 0.8 && !bDisabled[8]) draw0Steps(8);
else if (r < 1 && !bDisabled[9]) draw0Steps(9);
else computerTurn();
}
function draw0Steps(x)
{
bDisabled[x]=true;
button[x].style.opacity=0.7;
content[x]='0';
button[x].style.webkitTransform="rotateX(180deg)";
setTimeout(function()
{
ctx[x].beginPath();
ctx[x].lineWidth=3;
ctx[x].arc(25,25,17,0,Math.PI*2,false);
ctx[x].stroke();
ctx[x].closePath();
}, 300);
}
function checkWinner()
{
if(!isResult)
{
if (content[1] == 'x' && content[2] == 'x' && content[3] == 'x') s howWinner('Je hebt gewonnen!');
else if (content[4] == 'x' && content[5] == 'x' && content[6] == 'x') showWinner('Je hebt gewonnen!');
else if (content[7] == 'x' && content[8] == 'x' && content[9] == 'x') showWinner('Je hebt gewonnen!');
else if (content[1] == 'x' && content[4] == 'x' && content[7] == 'x') showWinner('Je hebt gewonnen!');
else if (content[2] == 'x' && content[5] == 'x' && content[8] == 'x') showWinner('Je hebt gewonnen!');
else if (content[3] == 'x' && content[6] == 'x' && content[9] == 'x') showWinner('Je hebt gewonnen!');
else if (content[1] == 'x' && content[5] == 'x' && content[9] == 'x') showWinner('Je hebt gewonnen!');
else if (content[3] == 'x' && content[5] == 'x' && content[7] == 'x') showWinner('Je hebt gewonnen!');
else if (content[1] == '0' && content[2] == '0' && content[3] == '0') showWinner('Je hebt verloren!');
else if (content[4] == '0' && content[5] == '0' && content[6] == '0') showWinner('Je hebt verloren!');
else if (content[7] == '0' && content[8] == '0' && content[9] == '0') showWinner('Je hebt verloren!');
else if (content[1] == '0' && content[4] == '0' && content[7] == '0') showWinner('Je hebt verloren!');
else if (content[2] == '0' && content[5] == '0' && content[8] == '0') showWinner('Je hebt verloren!');
else if (content[3] == '0' && content[6] == '0' && content[9] == '0') showWinner('Je hebt verloren!');
else if (content[1] == '0' && content[5] == '0' && content[9] == '0') showWinner('Je hebt verloren!');
else if (content[3] == '0' && content[5] == '0' && content[7] == '0') showWinner('Je hebt verloren!');
else if(
(content[1]=='x' || content[1]=='0')&&
(content[2]=='x' || content[2]=='0')&&
(content[3]=='x' || content[3]=='0')&&
(content[4]=='x' || content[4]=='0')&&
(content[5]=='x' || content[5]=='0')&&
(content[6]=='x' || content[6]=='0')&&
(content[7]=='x' || content[7]=='0')&&
(content[8]=='x' || content[8]=='0')&&
(content[9]=='x' || content[9]=='0')
)
showWinner("Gelijkspel. Speel opnieuw!");
}
}
function showWinner(x)
{
alert(x);
isResult=true;
}

Javascript Rule of Three it's not working

This rule of three needs to identify were is the X or Y and then calculate it from it's position and the type of the proporcion (direct or inverse). But for some reason the page in HTML it's returning the value "ERROR!" (set on the else condition).
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<input type="radio" id="inv">Inversamente</input>
<input type="radio" id="dir">Diretamente</input><br />
<input type="text" id="I1"></input>
<input type="text" id="I2"></input><br />
<input type="text" id="I3"></input>
<input type="text" id="I4"></input><br /><br />
<button id="button" onclick="button()">Calcular</button>
<script type="text/javascript">
function button() {
var i1 = parseInt(document.getElementById("I1"));
var i2 = parseInt(document.getElementById("I2"));
var i3 = parseInt(document.getElementById("I3"));
var i4 = parseInt(document.getElementById("I4"));
var i5 = document.getElementById("inv").value;
var i6 = document.getElementById("dir").value;
if (i1.value == "x" && i5.value === true || i1.value =="y" && i5.value === true){
var r1 = i3.value*i4.value/i2.value;
document.write("Resultado é " + r1);
}
else if (i1.value == "x" && i6.value === true || i1.value =="y" && i6.value === true) {
var r2 = i3.value*i2.value/i4.value;
document.write("Resultado é " + r2);
}
else if (i2.value == "x" && i5.value === true || i2.value =="y" && i5.value === true) {
var r3 = i3.value*i4.value/i1.value;
document.write("Resultado é " + r3);
}
else if (i2.value == "x" && i6.value === true || i2.value =="y" && i6.value ===true) {
var r4 = i1.value*i4.value/i3.value;
document.write("Resultado é " + r4);
}
else if (i3.value == "x" && i5.value === true || i3.value =="y" && i5.value === true) {
var r5 = i1.value*i2.value/i4.value;
document.write("Resultado é " + r5);
}
else if (i3.value == "x" && i6.value === true || i3.value =="y" && i6.value === true) {
var r6 = i1.value*i4.value/i2.value;
document.write("Resultado é " + r6);
}
else if (i4.value == "x" && i5.value === true || i4.value =="y" && i5.value === true) {
var r7 = i1.value*i2.value/i3.value;
document.write("Resultado é " + r7);
}
else if (i4.value == "x" && i6.value === true || i4.value =="y" && i6.value === true) {
var r8 = i2.value*i3.value/i1.value;
document.write("Resultado é " + r8);
}
else{
document.write("ERROR!");//whatever the case of the rule of the above, it aways return "ERROR!"
}
}
</script>
I'm sorry for my english. I'm brazilian.
You were running parseInt() on a DOM element instead of the element's value, but you're also looking for character input into anyone of those inputs. I've corrected the issues regarding value comparisons, but if all the inputs are not filled, you'll receive a NaN response.
Corrected JS:
function button() {
var i1 = document.getElementById("I1");
var i2 = document.getElementById("I2");
var i3 = document.getElementById("I3");
var i4 = document.getElementById("I4");
var i5 = document.getElementById("inv");
var i6 = document.getElementById("dir");
console.log(i1.value);
console.log(i2.value);
console.log(i3.value);
console.log(i4.value);
console.log(i5.checked);
console.log(i6.checked);
if (i1.value == "x" && i5.checked === true || i1.value == "y" && i5.checked === true) {
var r1 = i3.value * i4.value / i2.value;
console.log("Resultado é " + r1);
} else if (i1.value == "x" && i6.value === true || i1.value == "y" && i6.value === true) {
var r2 = i3.value * i2.value / i4.value;
console.log("Resultado é " + r2);
} else if (i2.value == "x" && i5.value === true || i2.value == "y" && i5.value === true) {
var r3 = i3.value * i4.value / i1.value;
console.log("Resultado é " + r3);
} else if (i2.value == "x" && i6.value === true || i2.value == "y" && i6.value === true) {
var r4 = i1.value * i4.value / i3.value;
console.log("Resultado é " + r4);
} else if (i3.value == "x" && i5.value === true || i3.value == "y" && i5.value === true) {
var r5 = i1.value * i2.value / i4.value;
console.log("Resultado é " + r5);
} else if (i3.value == "x" && i6.value === true || i3.value == "y" && i6.value === true) {
var r6 = i1.value * i4.value / i2.value;
console.log("Resultado é " + r6);
} else if (i4.value == "x" && i5.value === true || i4.value == "y" && i5.value === true) {
var r7 = i1.value * i2.value / i3.value;
console.log("Resultado é " + r7);
} else if (i4.value == "x" && i6.value === true || i4.value == "y" && i6.value === true) {
var r8 = i2.value * i3.value / i1.value;
console.log("Resultado é " + r8);
} else {
console.log("ERROR!"); //whatever the case of the rule of the above, it aways return "ERROR!"
}
}

Combining two scripts in Google sheets (Javascript)

I'm trying to combine two scripts I use on google sheets into one.
They will both work on different tabs.
What is the proper way of combining scripts together?
My two scripts are as follows:
function onEdit(e) {
var sheets = ['Sanshiro', 'Yujiro', 'Mei', 'Suil', 'Martin', 'Yuta', 'Rachel','So'],
cols = [1, 6, 4],
writeCols = [15, 11],
ind = cols.indexOf(e.range.columnStart);
if (sheets.indexOf(e.source.getActiveSheet()
.getName()) === -1 || ind === -1 || !e.value) return;
if (ind === 0 && e.value === 'Update') {
e.range.setValue(new Date());
} else if (ind === 1) {
if (e.range.offset(0, 5)
.getValue() === '') e.range.offset(0, 5)
.setValue(2);
if (e.range.offset(0, 9)
.getValue() === '') e.range.offset(0, 10)
.setValue(new Date());
if (e.range.offset(0, -5)
.getValue() === '') e.range.offset(0, -5)
.setValue(new Date());
else if (ind === 1) {
if (e.range.offset(0, 1)
.getValue() === 'Updated') e.range.offset(0, 1)
.setValue(Col1);
}
}}
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Must Place candis" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 2 ) { //checks the column
var nextCell = r.offset(0,12);
nextCell.setValue(new Date());
}
}
}
Any help would be much appreciated! If you can help me understand the process then I would like to learn!
Thanks so much!
How about following script? 2 script was summarized in one using "IF".
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
var sheets = ['Sanshiro', 'Yujiro', 'Mei', 'Suil', 'Martin', 'Yuta', 'Rachel','So'];
if (sheets.indexOf(e.source.getActiveSheet().getName()) > -1){
script1();
}
if(s.getName() == "Must Place candis") {
script2(s);
}
}
function script1(){
var cols = [1, 6, 4],
writeCols = [15, 11],
ind = cols.indexOf(e.range.columnStart);
if (ind === 0 && e.value === 'Update') {
e.range.setValue(new Date());
} else if (ind === 1) {
if (e.range.offset(0, 5)
.getValue() === '') e.range.offset(0, 5)
.setValue(2);
if (e.range.offset(0, 9)
.getValue() === '') e.range.offset(0, 10)
.setValue(new Date());
if (e.range.offset(0, -5)
.getValue() === '') e.range.offset(0, -5)
.setValue(new Date());
else if (ind === 1) {
if (e.range.offset(0, 1)
.getValue() === 'Updated') e.range.offset(0, 1)
.setValue(Col1);
}
}
}
function scritp2(s){
if( s.getName() == "Must Place candis" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 2 ) { //checks the column
var nextCell = r.offset(0,12);
nextCell.setValue(new Date());
}
}
}

How can I write this javascript logic code into a more efficient/compact way? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
In a project I am working on I have 21 buttons that all have active and inactive states. The state of certain buttons is affected by other buttons being pressed as well as that button being pressed. In my html I use ng-click to call a function updateActiveButtons(num) to activate or deactivate certain buttons.
The best way I could think of was to use an array of 21 elements, all of which were set to false by default and then changed when they were pressed.
The problem is that my code is UGLY and I know that there has to be a much better way to logic it out.
Here is my updateActiveButtons function:
/* Array for active buttons
0: Company Name 1: Country 2: Industry 3: Search 4: Company Name - Seller Name 5: Company Name - Buyer Name 6: Country - USA 7: Country - China 8: Country - Israel
9: Country - Russia 10: Country - India 11: Country - Japan 12: Industry - Tech 13: Industry - Consumer 14: Industry - Pharma 15: Industry - Financial 16: Industry - Biotech 17: Industry - Industrial
18: Date 19: Valuation 20: Industry - Business
*/
$scope.activeButtonArray = new Array(21);
for (var i = 0; i < $scope.activeButtonArray.length; i++) { $scope.activeButtonArray[i] = false; }
//pos = position in array
$scope.updateActiveButtons = function(pos) {
console.log($scope.activeButtonArray[20]);
if(pos != 0 || pos != 1 || pos != 2 || pos != 3 || pos != 4 || pos != 5) {
$scope.activeButtonArray[pos] = !$scope.activeButtonArray[pos];
} else if(pos == 3 && !$scope.activeButtonArray[pos]) {
$scope.activeButtonArray[pos] = true;
} else if(pos == 3 && $scope.activeButtonArray[pos]) {
$scope.activeButtonArray[pos] = false;
}
if(pos == 18 || pos == 19) {
$scope.activeButtonArray[0] = false;
if($scope.activeButtonArray[6] == false && $scope.activeButtonArray[7] == false && $scope.activeButtonArray[8] == false && $scope.activeButtonArray[9] == false && $scope.activeButtonArray[10] == false && $scope.activeButtonArray[11] == false) {
$scope.activeButtonArray[1] = false;
}
if($scope.activeButtonArray[12] == false && $scope.activeButtonArray[13] == false && $scope.activeButtonArray[14] == false && $scope.activeButtonArray[15] == false && $scope.activeButtonArray[16] == false && $scope.activeButtonArray[17] == false && $scope.activeButtonArray[20] == false) {
$scope.activeButtonArray[2] = false;
}
}
if(pos == 0) {
$scope.activeButtonArray[0] = true;
if($scope.activeButtonArray[4] || $scope.activeButtonArray[5]) {
$scope.activeButtonArray[0] = true;
}
if($scope.activeButtonArray[6] == false && $scope.activeButtonArray[7] == false && $scope.activeButtonArray[8] == false && $scope.activeButtonArray[9] == false && $scope.activeButtonArray[10] == false && $scope.activeButtonArray[11] == false) {
$scope.activeButtonArray[1] = false;
}
if($scope.activeButtonArray[12] == false && $scope.activeButtonArray[13] == false && $scope.activeButtonArray[14] == false && $scope.activeButtonArray[15] == false && $scope.activeButtonArray[16] == false && $scope.activeButtonArray[17] == false && $scope.activeButtonArray[20] == false) {
$scope.activeButtonArray[2] = false;
}
if($scope.search.text == undefined || $scope.search.text == '') {
$scope.activeButtonArray[3] = false;
}
}
if(pos == 1) {
if($scope.activeButtonArray[4] == false && $scope.activeButtonArray[5] == false) {
$scope.activeButtonArray[0] = false;
}
if($scope.activeButtonArray[6] == true || $scope.activeButtonArray[7] == true || $scope.activeButtonArray[8] == true || $scope.activeButtonArray[9] == true || $scope.activeButtonArray[10] == true || $scope.activeButtonArray[11] == true) {
$scope.activeButtonArray[1] = true;
}
if($scope.activeButtonArray[12] == false && $scope.activeButtonArray[13] == false && $scope.activeButtonArray[14] == false && $scope.activeButtonArray[15] == false && $scope.activeButtonArray[16] == false && $scope.activeButtonArray[17] == false && $scope.activeButtonArray[20] == false) {
$scope.activeButtonArray[2] = false;
}
if($scope.search.text == undefined || $scope.search.text == '') {
$scope.activeButtonArray[3] = false;
}
}
if(pos == 2) {
if($scope.activeButtonArray[4] == false && $scope.activeButtonArray[5] == false) {
$scope.activeButtonArray[0] = false;
}
if($scope.activeButtonArray[6] == false && $scope.activeButtonArray[7] == false && $scope.activeButtonArray[8] == false && $scope.activeButtonArray[9] == false && $scope.activeButtonArray[10] == false && $scope.activeButtonArray[11] == false) {
$scope.activeButtonArray[1] = false;
}
if($scope.activeButtonArray[12] == true || $scope.activeButtonArray[13] == true || $scope.activeButtonArray[14] == true || $scope.activeButtonArray[15] == true || $scope.activeButtonArray[16] == true || $scope.activeButtonArray[17] == true || $scope.activeButtonArray[20] == true) {
$scope.activeButtonArray[2] = true;
}
if($scope.search.text == undefined || $scope.search.text == '') {
$scope.activeButtonArray[3] = false;
}
}
if(pos == 3) {
if($scope.activeButtonArray[4] == false && $scope.activeButtonArray[5] == false) {
$scope.activeButtonArray[0] = false;
}
if($scope.activeButtonArray[6] == false && $scope.activeButtonArray[7] == false && $scope.activeButtonArray[8] == false && $scope.activeButtonArray[9] == false && $scope.activeButtonArray[10] == false && $scope.activeButtonArray[11] == false) {
$scope.activeButtonArray[1] = false;
}
if($scope.activeButtonArray[12] == false && $scope.activeButtonArray[13] == false && $scope.activeButtonArray[14] == false && $scope.activeButtonArray[15] == false && $scope.activeButtonArray[16] == false && $scope.activeButtonArray[17] == false && $scope.activeButtonArray[20] == false) {
$scope.activeButtonArray[2] = false;
}
}
if(pos == 4) {
$scope.activeButtonArray[4] = true;
$scope.activeButtonArray[5] = false;
}
if(pos == 5) {
$scope.activeButtonArray[4] = false;
$scope.activeButtonArray[5] = true;
}
}
I have a lot of repeated code that comes out in a way that just doesn't feel very well done or professional. I wouldn't be proud to send this to a client. Does anyone have any suggestions as to how I could make this better?
On way would be to replace entire conditions (or blocks) by methods/functions
so
if($scope.activeButtonArray[4] || $scope.activeButtonArray[5]) {
$scope.activeButtonArray[0] = true;
}
becomes
if (somethingIsSomething($scope))
This has the added benefit of be much more self-documenting so you can "read" what you're doing.
I liked pixelearth's recommendation to just create another function so I did.
I decided to make a function that took an array, a start, and a end point as parameters and return true if any of the array values in that range are true.
Here is the function:
var arrayContainsTrue = function(arr, start, end) {
for(var i = start; i <= end; i++) {
if(arr[i] == true) {
return true;
}
}
return false;
}
and then to shorten my code I just did this (with different start and end points based on what was needed):
if(!arrayContainsTrue($scope.activeButtonArray, 6, 11))

Categories