lundi 27 mai 2019

jquery If and preventDefault [duplicate]

This question already has an answer here:

I want to preventDefault click action on the link only on the page with a specific Id. But preventDefault seems to override if condition and run on all the page.

$(function(){
if($("body#Id")){
   $("a").click(function(event) {
    event.preventDefault();
   });
 }
});

event.preventDefault() work on all page not only on body#Id.

Does either C# or C++ optimize branching at runtime when the condition can be known ahead of time?

I apologize in advance if this is a stupid question, as I'm not as familiar with the .NET or gcc compilers as I'd like to be and my attempts to benchmark here gave me quite inconsistent results.

I know that an if statement where the condition is const gets optimized out to not perform the irrelevant branch. E.G:

const bool br = false;
static void Stuff(){
     if(br){
          Foo();
     }else{
          Bar();
     }
}


would make calling Stuff() equivalent to just calling Bar(), since the compiler knows that the branch will always go straight to else.

My question is, will this work at runtime? Obviously, in the case of

static bool br = false;
static void Stuff(){
     if(br){
          Foo();
     }else{
          Bar();
     }
}


The compiler can't optimize out the branch because it doesn't know what the value of br is ahead of time. But in this C# code:

static readonly bool br = false; //Emphasis on the READONLY

static void Stuff(){
     if(br){
          Foo();
     }else{
          Bar();
     }
}


The value of br is set in the static constructor, before any use of Stuff(), and it seemse that it would be possible for the JIT to know what br is when it reaches Stuff(), or for the .NET runtime to ignore the branch anyway, saving an extra branch evaluation and allowing more of the program to be cached in advance.

Additionally, if anyone knows what the GCC or CLang compilers would do in this scenario, this would be appreciated, as the C# code here may end up being compiled with IL2CPP. The primary use-case here is still using .NET however.

This might seem like a silly micro-optimization, but this code does end up getting called literally quadrillions of times and being able to keep more of the code cached instead of risking misprediction would produce a noticeable speedup.

Thanks!

Question about recoding categorical variables in R

Very green R user here. I have to categorical variables: One is, committee crime against child or has mental health issues (dichotomous, 1= crime and 0 = mental health) and committed crime against child or has substance abuse issues (dichotomous, 1= crime and 0 = substance abuse). I'd like to combine these groups so I will have a dichotomous, crime against child and mental health or substance abuse group. What would be the best way to do this? I have been playing around with "if" statements but to no avail.

I have been playing around with "if" statements but to no avail. I want to make a new variable in my dataset (com) that combines crime against child (1) and mental health/substance abuse (0)

x$com <- if(x$Group.finalCvsM == 1) {
  x$com("1") }
  if(x$Group.finalCvsS == 1) {
    x$com("1") }
  if(x$Group.finalCvsM == 0) {
    x$com("0") }
    if(x$Group.finalCvsS == 0) {
      x$com("0") }

Output using 2 conditions, second condition will use one of 2 different formulas

Currently trying to make this formula using IF/IFS , AND currently looks like this: =IF((D7=Yes)AND(C7>2500),C7*E5) so I am trying to check for 2 conditions

  1. I have a cell that has a "yes, no" pull down. So it is checking for a yes
    • if it is yes then it should check if another cell is less than 2500.

If it is >2500, then a specific formula should be used (C7*E5)

If it was <2500, then it would use a different formula (basically the same, just E5 would be F%, fulling from a different number.

I am not sure if I am on the right track at all.

I have tried using AND, IF, IFS. I am sure I am close but not doing the right combo.

I don't understand the answer to this question

What sequence of numbers would be printed if the following function were executed with the value of N being 0?

def xxx(N):
        print(N)
        if (N < 5):
        xxx(N + 2)
            print(N)

in my answers, it says the output should be : 0, 2, 4, 6, 6, 4, 2, 0

Drop Down for Business form

I have a form that has a drop down box i need to expand to more than two selections currently i have a functions.php file with the code. I need some help if you could. On expanding the list. It shouldn't take much i would assume. I need to add

spare_needed

between open and closed.

I tried spreading it out to else if statements but that didn't work.

    if($new_old == "open"){
        $no_select = '                                      <option value="">SELECIONAR (SELECT ONE)</option>
                                          <option value="open" selected>ABERTO (OPEN)</option>
                                          <option value="closed">ENCERRADO (CLOSED)</option>';
    }
    else{
        $no_select = '                                      <option value="">SELECIONAR (SELECT ONE)</option>
                                          <option value="open">ABERTO (OPEN)</option>
                                          <option value="closed" selected>ENCERRADO (CLOSED)</option>';
    }

                                                if(${'new_old' . $i} == "open"){
                                                    echo '<option value="">SELECIONAR (SELECT ONE)</option>
                                                          <option value="open" selected>ABERTO (OPEN)</option>
                                                          <option value="closed">ENCERRADO (CLOSED)</option>';                                                  
                                                }
                                                else if(${'new_old' . $i} == "closed"){
                                                    echo '<option value="">SELECIONAR (SELECT ONE)</option>
                                                          <option value="open">ABERTO (OPEN)</option>
                                                          <option value="closed" selected>ENCERRADO (CLOSED)</option>';                                                 
                                                }

                                        echo '

Why can't I access the 2nd element in this array, while loop issue

I have this while loops and an embedded if statement which places a hyperlink in the appropriate table cell. When I press edit the URL reflects what I want to see (which is the current word to be edited) but the text field the word is suppose to populate is only ever the first entry in the database table.

I have tried Using PHP variables inside HTML tags? which I think does not work because mine is an internal link, not another website.

I have also tried to store $row['yourword'] in another var, so when the while loop encounters that if statement which puts the edit in the right cell, it also would save the correct yourword, but it just results in an error.

Here is the while loop

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['uname'] . "</td>";
echo "<td>" . $row['clickrate'] . "</td>";
if($row['uname'] == $logedInUsername)
{
    echo "<td>" . $row['yourword'] . "<a href='edityourword.php?edit=$row[yourword]'> edit</a></td>";
}
else
{
    echo "<td>" . $row['yourword'] . "</td>";
}
echo "</tr>";
}
echo "</table>";
?>

And here is the destination

<form action="edityourword.php" method="POST">
    word: <input type="text" name="newName" value="<?php echo $row[yourword]; ?>"><br><br>
    <input type="submit" value="Update">    
</form>

The expected result should be the 2nd word showing up in the text field on the next page, instead of the first one each time, editing the word still works, but the text field is just populated with wrong one each time if it is not the user which is the first on the table.