vendredi 1 septembre 2017

Average with If condition

I want to calculate the average of the cells in column E, only for the rows that have 0 in column I. It needs an if condition and then perform the standard average line of code.. I am providing the code I have written to calculate the average for all cells in column E. This code needs editing to include this if condtion. If someone knows what to add to have this if condition I would appreciate it !

Also, I am providing a screenshot

enter image description here

lastrow = Cells(Rows.Count, "D").End(xlUp).Row
Range("C1").Formula = "=AVERAGE(E2:E" & lastrow & ")"

R: If Else Statement with Four Criteria returning NA

I have this dataset:

Pars <-structure(list(Pars.PeopleSoftID = c(11, 18, 18, 22, 39, 181), 
    Pars..Central.1. = c(0, 1, 1, 1, 1, 1), Pars..Central.2. = c(0, 
    1, 1, 1, 1, 1), Pars.External = c(3, 2, 2, 1, 2, 5), Pars.Minimum = c(15, 
    2, 1.71, 1, 1, 4.39), Pars.Maximum = c(15, 2.76, 2, 1, 1, 
    5.48)), .Names = c("Pars.PeopleSoftID", "Pars..Central.1.", 
"Pars..Central.2.", "Pars.External", "Pars.Minimum", "Pars.Maximum"
), row.names = c(NA, 6L), class = "data.frame")

I'm trying to add a column that calculates a number based on what is in the columns Central 1, Central 2, and External and multiplies that number by the value in Pars$Minimum.

The logic is:

If Central 1 = 0, Central 2 = 0, External = 0 THEN <-- 0 * Pars$Minimum

If Central 1 = 1, Central 2 = 0, External = 0 THEN <-- 1 * Pars$Minimum

If Central 1 = 1, Central 2 = 1, External = 0 THEN <-- 0.7 * Pars$Minimum

If Central 1 = 1, Central 2 = 1, External > 0 THEN <-- 0.7 * Pars$Minimum

If Central 1 = 1, Central 2 = 0, External > 0 THEN <-- 0.8 * Pars$Minimum

I just tried this, but it seems to be just returning values of NA for everything:

 Pars$Cen1 <- with(Pars, if(`Central 1` == 0 , 0, ifelse(`Central 1` == 1 & `Central 2` == 0 & External == 0, 1, ifelse(`Central 1` == 1 & `Central 2` == 1 & External == 0, .7, ifelse(`Central 1` == 1 & `Central 2` == 1 & External > 0, .7, ifelse(`Central 1` == 1 & `Central 2` == 0 & External > 0, .8)

SQL - if statement only

I need to set if-statement without else part in SQL query. But if-statement syntax is IF(<condtion>, <true part>, <false part>). I do not have (not need to set) any value for else part.

Example: From user table, if user id is greater than 0 then only I need to add user id. How can I implement/add such condition?

SELECT
    u.fname,
    u.lname,
    IF(u.id>0, u.id, ? )  // Here I do not have value for else part.
FROM
    user_table u;

Substr - Can't use value return in parse context

I am trying to create an IF function with two conditions however, I am getting parse error "can't use value return in parse context". Any ideas please?

    $youlink = get_field('fl_tube');               
    if (strlen ($youlink) < 50 && substr($youlink, 0, 23) = "https://www.youtube.com"; ) {
some code here...    
    }

Simplify c# if/else statement with different List<>

Is there any way to simplify the following code? supportList is an object which is getting from constructor:

 public void FillCollection(object supportList)

Code here:

 if (supportList is List<Bank>)
        {
            foreach (var temp in supportList as List<Bank>)
            {
              //code here

            }
        }
        else if (supportList is List<Currency>)
        {
            foreach (var temp in supportList as List<Currency>)
            {
                //code here

            }
        }
        else if (supportList is List<Amount>)
        {
            foreach (var temp in supportList as List<Amount>)
            {
                //code here

            }
        }

EDIT 1: All foreach parts do the same code like

foreach (var temp in supportList as List<Bank>)
            {
                string yesno;
                if (temp.Id == CurrentId)
                {
                    yesno = "" + yes;
                }
                else
                {
                    yesno = "" + no;
                }

                CollectionSupport.Add(new SupportModel { Id = temp.Id, Name = temp.Name, Code = temp.Code, YesNo = "" + yesno });

            }

If statement in r, help translate from sas to r

How can I translate the SAS code below to r code

Data d;
 set d;
if ssn = 'shoe' then ssn ='5567';
if ssname ='Prodigy' then ssn= '1958';
if ssemail= 'compliance@ownregistrar.com' then ssn ='2400';
if ssurl= 'www.test.com' then ssn='9999';
if ssn= '8901' then ssname= 'Tech Start';
if ssn= '8901' then ssn= '1060'; 
if ssname = 'Ybm' then ssn='1495';
run;

Hangman Game - can't make the loop stop deducting lives - works otherwise

As a couple other folks here, I am trying to build a hangman game for class. All things work more or less fine, but somehow I am struggling to stop the script when lives === 0. The script keeps running and just keeps deducting lives...

I tried to append the if statement in several ways, but must be missing something...

Could somebody take a look and help me understand what I am doing wrong?

Git-Hub

 
var word = "Flower";
var gameOptions = ["Flower", "Fruit", "Table", "Chair", "Love", "Family", "Water", "Picture", "Refrigerator", "Giraffe"]
 // The word we will use to play, will be randomized later
var gameOptionsLength; // The comptuer needs to know the length of the array, so we can set the upper limit for the random function
var computerChoice; // Computer will pick random word from gameOptions array
var gameWord; // This variable will transform the computer choosen string into an upper case string
var gamewordLength; // In order to show the right character amout to be guessed, we need to know the word length
var blanks= "";
var winword="";
var userInput;
var lives = 6; // According to Wikipedia, the user has 6 lives
var alphabet;
var outputElement;
var guessedLetters = []; // stores the letters the user guessed

gameOptionsLength = gameOptions.length;

computerChoice=gameOptions[Math.floor(Math.random() * gameOptionsLength)];
        console.log("computer choice " + computerChoice);

gameWord = computerChoice.toUpperCase();
gamewordLength = gameWord.length;

var j = 0;  // Creates the blanks i.e. Flower has 6 characters and needs 6 blanks _ _ _ _ _ _
while (j < gamewordLength) {
    blanks += " _";
    j++;
   }

var j = 0;  // Creates the blanks i.e. Flower has 6 characters and needs 6 blanks _ _ _ _ _ _
while (j < gamewordLength) {

    winword += " "+ gameWord.charAt(j);
    j++;



alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
outputElement = $('#guessed-letters');


$(document).on('keyup', function (event) {
  var key = event.key.toLowerCase();
  if (alphabet.indexOf(key) !== -1) {
    // the key is a letter in the alphabet
    if (guessedLetters.indexOf(key) === -1) {
      // the key has not been guessed
      guessedLetters.push(key);
      var string = guessedLetters.join(''); // join the letters together to one single string
      outputElement.text(string);
    }
  }
});


var paragraph = document.getElementById("demo").innerHTML = "The word you are looking for has " + gamewordLength + " characters, can you guess it?";
var paragraph1 = document.getElementById("blanks").innerHTML =  "Word:"+blanks;
var pLives = document.getElementById("pLives").innerHTML = " Lives: " + lives;

document.onkeyup = function(event){
      userInput = event.key;
      userInput = userInput.toUpperCase();

      console.log("the userInput is " + userInput) // 
      
  

    var index = gameWord.indexOf(userInput);

        if (index >= 0) { 
        for (var i = 0; i < gamewordLength; i++){
        console.log("Here is the gamword from within the loop:" + " "+ blanks); 
        var index = gameWord.indexOf(userInput,i); // indexOf checks whether a character is within a string, if the character exists the return value is >=0
        console.log(index);
        console.log("that works, the right position is " +index);
                blanks = blanks.split('');
                blanks[index*2+1] = userInput; 
                blanks = blanks.join('');
                console.log("here are the new blanks:" + " "+ blanks);
                var paragraph1 = document.getElementById("blanks").innerHTML =  "Word:"+blanks;
                                
        }}


         else {  
        // deducting lives if indexOf === -1, as the user selected character does not exist in the string
                lives = lives -1;
                console.log("lives = " +lives)
                var pLives = document.getElementById("pLives").innerHTML = " Lives: " + lives;
        }


        if (lives === 0){
                var gameOver = document.getElementById("gameOver").innerHTML = " GameOver - refresh the site to play again!";
                }
        
                                
        else if (blanks === winword) {
                var winner = document.getElementById("winner").innerHTML = "Well done, you guessed the right word!!";
        }

}}
<!DOCTYPE html>
<html>
<head>
        <title>Hangman Game</title>
        <link rel="stylesheet" href="assets/css/style.css">
          <script type="text/javascript" src="http://ift.tt/2n9t8Vj"></script>


</head>
<body>

<h1>Hangman</h1>
<p id="demo"></p> 
  <!-- Js includes the following text: The word you are looking for has " + gamewordLength + " characters, can you guess it?"; -->
 <h1 id="blanks"></h1> <!-- If word is "Love", there will be 4 - - - -  -->
 <h1 id="pLives"></h1> <!-- Lives:  -->
 <h1 id="gameOver"></h1> <!-- GameOver -->
 <h1 id="winner"></h1> <!--Winner -->
 <h3>You already used the following letters:</h3>
 <h3 id="guessed-letters"></h3> <!--Displays used letters -->

<!-- <div id="display"></div>
  <div id="buttons"></div>
  <div id="AllKeys">
  <button id="allButtons">A</button> -->

 </div>

<script src="assets/javascript/game.js"></script>

</body>
</html>