lundi 1 juillet 2019

JavaScript: Test if input args are arrays; If yes, then merge arrays into one big array [duplicate]

This question already has an answer here:

I have the following variables which are arrays:

const gumBrands = ['orbit', 'trident', 'chiclet', 'strident'];

const mintBrands = ['altoids', 'certs', 'breath savers', 'tic tac'];

Below I have the following function that uses the variables as input arguments:

function shallowCopy (arrOne, arrTwo) {

    if (arrOne.constructor === 'Array'){

        return [...arrOne, ...arrTwo]; 
    }

    else {
        console.log('test this'); 
    }
}


shallowCopy(gumBrands, mintBrands)

I am expecting my code to return:

[ 'orbit',
  'trident',
  'chiclet',
  'strident',
  'altoids',
  'certs',
  'breath savers',
  'tic tac' ]

Instead the code runs my else statement and returns: test this

What am I doing wrong?

Nested if/and statement not working in excel

I have 2 tables that i am trying to compare and return a result if the columns both match. I am trying to use the formula: =IF(AND(A:A=Rates!A:A,'Rates (2)'!C:C=Rates!B:B),Rates!C:C,"no") and there are definitely cases where the two columns match up but I am always getting a false back. No idea what I am doing wrong here. Very frustrated because the same exact formula that I am trying to use works 20 cells above where I am trying to enter it now...

I have broken down the formula and it is telling me that it is not finding the information in the second logic statement in the and statement, so I copied and pasted the exact information so that formatting was the same and it still didn't work.

=IF(AND(A:A=Rates!A:A,'Rates (2)'!C:C=Rates!B:B),Rates!C:C,"no")

i expect to get true, which is the value in Rates!C:C but I keep getting "no"

Google Sheets Query across sheets using checkboxes to limit results

I am making a mini-search engine in Google Sheets. I have information in 3 sheets (6th Grade, 7th Grade, 8th Grade). In the past, I had a Combined sheet that combined the information from each sheet. However, as I add additional sheets for grades 1-5, the amount of information returned will be excessive. I want to allow the user to use checkboxes to identify which grades they want results from.

Here is the workbook. I've added sheets for all the grades by copying the data from 6th, 7th, and 8th. The format of the data will be the same once I get my hands on it.

Here is the formula I've been playing around with:

=iferror(If(D2=True,Query(
             Kindergarten!A:I,"Select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8 where Col9>0", 
            ),
      Query(
             {Kindergarten!A:I;'1st'!A:I;'2nd'!A:I},"Select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8 where Col9>0", 
            ),
      "No Matches, try a different search"))

Adding to conditions to determine if value should increase or not

I'm looking to create conditions for my ticket system with jQuery. I have my code that I can increase or decrease the basket total depending on the number of tickets but I want to take a step further and have conditions that have to be met to add the tickets with pop ups that tell you there's an issue

For example: you can't have a child ticket without and adult ticket... or you can't have more than 2 carers... etc

I've tried adding additional if statements within my code that adds increments/decrements the ticket totals but I can't get the conditions to work

I tried using a switch statement but I'm really not that experienced with JS to execute that properly but I feel it may be an option?

Here's a simplified snippet of my code:

  var totalPrice = 0;

  $($(".valueBtn")).on("click", function(e) {
  var currTicket = $(this).closest("li");
  var targetClass = e.target.className;
  var targetTicketType = $(this)
    .closest("li")
    .attr("data-type");
  var targetTicketPrice = $(this)
    .closest("li")
    .attr("data-price");

  //plus value
  if (targetClass.indexOf("plus") > 0) {
    var currAmount = $(currTicket)
      .find(".currentValue")
      .html();
    if (currAmount >= 0) {
      $(currTicket)
        .find(".currentValue")
        .html(
          parseInt(
            $(currTicket)
              .find(".currentValue")
              .html()
          ) + 1
        );
      totalPrice = totalPrice + parseFloat(targetTicketPrice);
    }
  }

  //minus value
  if (targetClass.indexOf("minus") > 0) {
    var currAmount = $(currTicket)
      .find(".currentValue")
      .html();
    if (currAmount > 0) {
      $(currTicket)
        .find(".currentValue")
        .html(
          parseInt(
            $(currTicket)
              .find(".currentValue")
              .html()
          ) - 1
        );
      totalPrice = totalPrice - parseFloat(targetTicketPrice);
    }
  }

  //update basket total
  $("#basket").html(parseFloat(totalPrice).toFixed(2));
});

You can see it here: https://codepen.io/cheesycoder/pen/KLjWPM

I'm just looking for a nudge in the right direction.

If anyone can help me create conditions that won't allow child tickets to be added without adult tickets maybe I can work out the rest of my conditions from there.

Problems with a Python if-else statement

I am doing an assignment for an IT class. Very basic Python projects. I have no experience with it but am able to grasp what is going on most of the time. This project involves making choices involving random integer selections. I can get the options to work the way I want in the if-else format but the else command isn't responding the way I want. I have it up on repl.it. The section I am having issues with starts on line 130. The options all load properly but if the choice is 1-3 for some reason the program then prints the "else" statement after printing the "if" statements. This doesn't happen if they choose option 4 or pick an invalid number(the reason for the else statement).

https://repl.it/@MLirange/Adventure

(If you are running it in the console, to get to the second stage the key is either in option 1 or 2, then pick option 4. The second stage is where the if-else error is occurring.)

I didn't have this issue in the previous section of the program. I must have missed something, but I can't tell what it is. I should reiterate I am very much a beginner at this and am basically copy-pasting code as the assignment instructed, and then editing it to suit my needs.

How to break if/else statement in JavaScript once a condition is true

So I have this basic JavaScript code to detect a user language and redirect to the proper page based on the browser language, the issue I am facing is that the if/else statement keeps going as an infinite loop and the browser keeps refreshing.

if(userLang == "en-US"){
  window.location.href = "domainame.com/faq.html";
}
else if(userLang == "nl"){
  window.location.href = "domainame.com/faq-de.html";
}
else if(userLang == "fr"){
  window.location.href = "domainame.com/faq-fr.html";
}
else if(userLang == "es-ES"){
  window.location.href = "domainame.com/faq-es.html";
}
else if(userLang == "ja"){
  window.location.href = "domainame/faq-ja.html";
}

I expect the browser to check for the user language and redirect to the proper page, instead, the browser keeps refreshing.

How to use if else when splitting response

I currently have a small issue trying to use an if/else statement when a returned string could send me different formats

The issue im having is that when i request a code it either returns a list [1234,122324,21242] a single number [3335] two numbers [332435,34324] or nothing []

Currently im using this code which gives me the second code on teh list, but the issue im having is ive no idea how to take the code if there is only one on the list, or handle if there is nothing returned

    String response_body_string = responseBody.body().string();
    String[] tmp = response_body_string.split(",");
    tmp = tmp[1].split(",");
    //Returns second game ID
    String val2 = tmp[0];
    //Only use 0-9, remove any characters or letters
    String val3 = val2.replaceAll("[^0-9]", "");


    System.out.println(val3);
    //System.out.println(response_body_string);

    return val3;