I am trying to do filter based on mens/women in such a way that whenever I click on men then the url should be updated to women by finding men in url and replacing it.
Any help highly appreciated.
$('.men').on('click',function(){
var men = location.href;
if (men.includes("constraint") === false && men.includes("types") === false && men.includes("vendors") === false){
men += "?constraint=men";
}
else if(men.includes("constraint=men") === false && men.includes("+men") === false && men.includes("vendors") === false && men.includes("women") === false){
men += "+men";
// women.replace("women","men");
}
else if(men.includes("types") === true && men.includes("=men") === false){
men += "&constraint=men";
}
else if(men.includes("vendors") === true && men.includes("=men") === false){
men += "&constraint=men";
}
else if(men.includes("men") === true && men.includes("women") === false) {
men.replace("women","men");
}
else if(men.includes("=women") === true || men.includes("+women") === true){
alert("url contains women");
}
else{
men += "";
}
var men_result = location.href = men;
document.getElementById("wow").href = men_result;
});
var href = window.location.href;
if(href.indexOf('men') >= 0 && href.indexOf('women') === -1)
window.location.href = href.replace('men', 'women');
}
Related
I have a click button which is validating data from firebase firestore, but it is not giving the correct output as in the checks applied are getting failed.
createAptBtn.onclick = () => {
if (category.value === "" || email.value === "" || day.value === "" || time.value === "") {
promptContent.innerText = "All fields are required."
}
else if (category.value === "New") {
db.collection("recordsDb").onSnapshot((querySnapshot) => {
querySnapshot.forEach((doc) => {
//Condtion 1
if (email.value === doc.data().email && day.value === doc.data().day && time.value === doc.data().time) {
promptContent.innerText = "Email already exists."
}
//Condition 2
if (email.value !== doc.data().email && day.value === doc.data().day && time.value === doc.data().aptTimeSlot) {
promptContent.innerText = "Slot already filled."
}
//Condition 3
if (email.value === doc.data().email || mobile.value === doc.data().mobile) {
promptContent.innerText = "User already exits."
}
// Condition 4
if(email.value !== doc.data().email && day.value !== doc.data().day && time.value !== doc.data().time) {
promptContent.innerText = "Account created."
}
})
})
}
}
Also even when one of the condition is true it still outputs the else statement which should work when all ifs are getting failed.
However, condition 4 is an else statement but since its not working I'm using it as an if statement by adding some checks in it which though fails.
How do I fix this?
So I have this problem solving how to see when tie happens, when the X wins and when the O wins.
Here's the code to determine the value of my tiles.
Javascript code
let winner;
let winner2;
function checkValueForX() {
if (
board[0].textContent === 'X' &&
board[1].textContent === 'X' &&
board[2].textContent === 'X'
) {
let winner = 'X';
return true;
} else if (
board[3].textContent === 'X' &&
board[4].textContent === 'X' &&
board[5].textContent === 'X'
) {
let winner = 'X';
return true;
} else if (
board[6].textContent === 'X' &&
board[7].textContent === 'X' &&
board[8].textContent === 'X'
) {
let winner = 'X';
return true;
} else if (
board[0].textContent === 'X' &&
board[4].textContent === 'X' &&
board[8].textContent === 'X'
) {
let winner = 'X';
return true;
} else if (
board[2].textContent === 'X' &&
board[4].textContent === 'X' &&
board[6].textContent === 'X'
) {
let winner = 'X';
return true;
} else if (
board[0].textContent === 'X' &&
board[3].textContent === 'X' &&
board[6].textContent === 'X'
) {
let winner = 'X';
return true;
} else if (
board[2].textContent === 'X' &&
board[5].textContent === 'X' &&
board[8].textContent === 'X'
) {
let winner = 'X';
return true;
} else {
return false;
}
}
function checkValueForO() {
if (
board[0].textContent === 'O' &&
board[1].textContent === 'O' &&
board[2].textContent === 'O'
) {
let winner2 = 'O';
return true;
} else if (
board[3].textContent === 'O' &&
board[4].textContent === 'O' &&
board[5].textContent === 'O'
) {
let winner2 = 'O';
return true;
} else if (
board[6].textContent === 'O' &&
board[7].textContent === 'O' &&
board[8].textContent === 'O'
) {
let winner2 = 'O';
return true;
} else if (
board[0].textContent === 'O' &&
board[4].textContent === 'O' &&
board[8].textContent === 'O'
) {
let winner2 = 'O';
return true;
} else if (
board[2].textContent === 'O' &&
board[4].textContent === 'O' &&
board[6].textContent === 'O'
) {
let winner2 = 'O';
return true;
} else if (
board[0].textContent === 'O' &&
board[3].textContent === 'O' &&
board[6].textContent === 'O'
) {
let winner2 = 'O';
return true;
} else if (
board[2].textContent === 'O' &&
board[5].textContent === 'O' &&
board[8].textContent === 'O'
) {
let winner2 = 'O';
return true;
} else {
return false;
}
}
Here's my function to check if a player win or if a tie happens:
function checkWinOrTie() {
if (winner === true) {
displayWinnwer.innerHTML = 'Winner is ' + activePlayer1 + '!';
} else if (winner2 === true) {
displayWinnwer.innerHTML = 'Winner is ' + activePlayer2 + '!';
}
}
I know that there's still something missing here, can you help me to declare how to detect if there's a winner or tie?
PS. Newbie in JavaScript here pardon me.
You can't have two winners, right?
You can rewrite your checkWinOrTie() function like this:
function checkWinOrTie(){
if (checkValueForX()){
/* are you aware that you misspelled "displayWinnwer"? */
displayWinnwer.innerHTML = 'Winner is ' + activePlayer1 + '!';
} else if(checkValueForO()){
displayWinnwer.innerHTML = 'Winner is ' + activePlayer2 + '!';
} else if (isTie()) {
displayWinnwer.innerHTML = 'There was a tie!';
}
}
Run checkWinOrTie() each time a player completes a move.
And then write the isTie() function like this:
function isTie() {
for (let i = 0; i < board.length; i++) {
//this "if" check assumes your textContent would be "falsy" for boxes that haven't been filled yet
if (!board[i].textContent) {
return false; //because we found at least one empty box
}
}
//at this point, code iterated over every box and did
//not find a single empty box, so the board must be full
return true;
}
Your code could be improved in a lot of ways. But since you're asking specifically how to solve the problem with the approach you already established, this should do the trick.
I have already done something similar ...
find! : How can I change the value of the button when clicked?
well, the approach is different, so here it is:
const board =
[ '-','-','-' // 0 1 2
, '-','-','-' // 3 4 5
, '-','-','-' // 6 7 8
]
function check_LCD(pCod) {
// check for Lines
for(l=0;l<9; l+=3)
{ if (board[l]===pCod && board[l+1]===pCod && board[l+2]===pCod) return true }
// check for Columns
for(c=0;l<4; c++)
{ if (board[c]===pCod && board[c+3]===pCod && board[c+6]===pCod) return true }
// check for Diagonals
return ( (board[0]===pCod && board[4]===pCod && board[8]===pCod)
|| (board[2]===pCod && board[4]===pCod && board[6]===pCod) )
}
function checkWinOrTie()
{
winner_X = check_LCD('X')
winner_O = check_LCD('O')
if (winner_X) {
displayWinnwer.textContent = 'Winner is ' + activePlayer1 + '!';
}
else if(winner_O) {
displayWinnwer.textContent = 'Winner is ' + activePlayer2 + '!';
}
}
using an array is less practical.
I have made a solar system simulation and have some 'settings' which the user can change. I have made it so the settings only update when the save button is pressed. This works and the bodies are hidden if a box is checked but say for example, i checked mercury and saved the changes then wanted to check Venus as well when i save the changes Venus is hidden but Mercury is shown again. how can i make it so that Mercury stays hidden until the box in uncheked.
(note the check boxes decide whether a planet should be hidden or not so hideMer for example when true would stop drawing mercury onto the canvas)
code which i think the issue applies to:
function animate() {
//clears canvas each new loop
if (showPath==false){
c.clearRect(-innerWidth/2,-innerHeight/2,innerWidth,innerHeight);
}
hideBodies = [hideMer, hideVen, hideEar, hideMar, hideJup, hideSat, hideUra, hideNep];
drawSun();
for (var i=0; i< xPosList.length; i++){
leapfrog(i);
if (hideBodies[i]==false){
drawBody(i);
}
}
}
function saveChanges() {
showPath=(document.getElementById("showPath").value=="True");
//Mercury
if (document.getElementById("mer").checked && hideMer == false){
hideMer = true;
} else if (document.getElementById("mer").checked && hideMer == true){
hideMer = false;
}
//Venus
if (document.getElementById("ven").checked && hideVen == false){
hideVen = true;
} else if (document.getElementById("ven").checked && hideVen == true){
hideVen = false;
}
//Earth
if (document.getElementById("ear").checked && hideEar == false){
hideEar = true;
} else if (document.getElementById("ear").checked && hideEar == true){
hideEar = false;
}
//Mars
if (document.getElementById("mar").checked && hideMar == false){
hideMar = true;
} else if (document.getElementById("mar").checked && hideMar == true){
hideMar = false;
}
//Jupiter
if (document.getElementById("jup").checked && hideJup == false){
hideJup = true;
} else if (document.getElementById("jup").checked && hideJup == true){
hideJup = false;
}
//Saturn
if (document.getElementById("sat").checked && hideSat == false){
hideSat = true;
} else if (document.getElementById("sat").checked && hideSat == true){
hideSat = false;
}
//Uranus
if (document.getElementById("ura").checked && hideUra == false){
hideUra = true;
} else if (document.getElementById("ura").checked && hideUra == true){
hideUra = false;
}
//Neptune
if (document.getElementById("nep").checked && hideNep == false){
hideNep = true;
} else if (document.getElementById("nep").checked && hideNep == true){
hideNep = false;
}
console.log(hideMer);
alert(hideMer);
}
whole code: https://jsfiddle.net/nczpod06/6/
In the function save changes for each planet I'm only changing the boolean value when it is checked and not when it is unchecked. To fix this (for example for mercury) I needed to add the not boolean logical operator and then it works... for example:
//Mercury
if (document.getElementById("mer").checked && hideMer == false){
hideMer = true;
} else if (!(document.getElementById("mer").checked) && hideMer == true){
hideMer = false;
}
Can anyone tell me what CRM would hate about this onload function I've created?
It's telling me Form_OnLoad is not defined. Looks defined to me. It's enabled in my form onload, published, etc.
Thank you.
function Form_OnLoad() {
//Calculates total commission for AE1
// Products + Services
if (Xrm.Page.getAttribute("new_commissionproductae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionserviceae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionseligible").getValue() == "Yes") {
var comm1 = (Xrm.Page.getAttribute("new_commissionproductae1").getValue() + Xrm.Page.getAttribute("new_commissionserviceae1").getValue());
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(comm1);
} else if {
// Products only
(Xrm.Page.getAttribute("new_commissionproductae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionseligible").getValue() == "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionproductae1").getValue());
} else if {
// Services only
(Xrm.Page.getAttribute("new_commissionserviceae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionseligible").getValue() == "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionserviceae1").getValue());
} else {
// Net Sales
(Xrm.Page.getAttribute("new_commissionnetae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionseligible").getValue() == "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionserviceae1").getValue());
}
}
I believe that's because your JavaScript is not correct. Try to use following code instead of yours:
function Form_OnLoad() { //Calculates total commission for AE1
// Products + Services
if (Xrm.Page.getAttribute("new_commissionproductae1").getValue() !== null &&
Xrm.Page.getAttribute("new_commissionserviceae1").getValue() !== null &&
Xrm.Page.getAttribute("new_commissionseligible").getValue() === "Yes") {
var comm1 = Xrm.Page.getAttribute("new_commissionproductae1").getValue() + Xrm.Page.getAttribute("new_commissionserviceae1").getValue();
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(comm1);
} else if (Xrm.Page.getAttribute("new_commissionproductae1").getValue() !== null &&
Xrm.Page.getAttribute("new_commissionseligible").getValue() === "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionproductae1").getValue());
} else if (Xrm.Page.getAttribute("new_commissionserviceae1").getValue() !== null &&
Xrm.Page.getAttribute("new_commissionseligible").getValue() === "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionserviceae1").getValue());
} else if (Xrm.Page.getAttribute("new_commissionnetae1").getValue() !== null &&
Xrm.Page.getAttribute("new_commissionseligible").getValue() === "Yes") {
Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionserviceae1").getValue());
}
}
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))