samedi 28 août 2021

Calculate total score and categorize it based on input

I'm trying to create an application for the SLE disease scoring system, in which each symptom is represented by a different number, 

The total of symptom scores will be divided into a range of scores to determine disease activity.

Here is the code that I'm trying to write, but I haven't been able to figure out what the problem is that is keeping this program from running.



ui <- fluidPage(
    checkboxGroupInput("variable", "Variables to show:",
                       c("Neurological disorder" = "neu",
                         "Kidney disorder" = "gin",
                         "Vasculitis" = "vas",
                         "Haemolysis/ Thrombositopenia" = "hem",
                         "Myositis" = "mio",
                         "Artritis" = "art",
                         "Mucocutaneous disorder" = "muc",
                         "Serositis" = "ser",
                         "Fever/ Fatique" = "fev",
                         "Leukopenia/ Lhymphopenia" = "leu"
                         )),
    textOutput("data")
)

server <- function(input, output, session) {

       
    output$data <- reactive({
        input$neu == 8
        input$gin == 6
        input$vas == 4
        input$hem == 3
        input$mio == 3
        input$art == 2
        input$mio == 2
        input$ser == 2
        input$fev == 1
        input$leu == 1
        
    
        total <- input$neu + input$gin + input$vas + input$hem + input$mio +  
            input$art + input$mio + input$ser + input$fev + input$fev + input$leu
        
        if (total <= 2) {"Mild disease activity"}
        else if (total > 2 && < 5) {"Moderate disease activity"}
        else if (total > 5) {"Severe disease activity"}
})

shinyApp(ui, server)


Thank you for your help

How do I use the If statement?

I'm unexperienced and don't even know how to use this.
I have written this, but obviously it's not correct:

print("How much cookies you have?")
cookie_ammount = input()
cookie_minimum = "1"
If cookie_ammount > cookie_minimum:
print("Gimme")
Else:
print("awe")

(JavaScript) What structure is that?

I need to make a Fibonacci sequence and I found this code below here. When I try to understand all code, I saw "(i <= 1) ? i : arr[i-2] + arr[i-1]" I thought that was an simple if else shortcut condition, and I try to replace by the extended way just to be sure (see the last code). But now the code didn't work...

//Stackoverflow code:
function fib(n){
  return new Array(n).fill(1).reduce((arr, _ ,i) => {
      arr.push((i <= 1) ? i : arr[i-2] + arr[i-1])
      return arr
  },[]) ;
}
console.log(fib(10))

//My code with (extended if else):
function fib(n){
    return new Array(n).fill(1).reduce((arr, _ ,i) => {
        arr.push(if (i <= 1) {
            i
        } else {
            arr[i-2] + arr[i-1]            
        })
        return arr
    },[]) ;
  }
  console.log(fib(10))

Why my code do not work? Is "(i <= 1) ? i : arr[i-2] + arr[i-1]" a kind of if/else statement?

Small glitch in finding number game

So I am making this small project where it picks a random number from 1 to 100 and it keeps printing it till it sees 20 in it and stops it. Though I am getting the random numbers it does not do anything when it prints out 20. Maybe this is me being dumb but it would be appreciated if someone could help me. Thanks!

The script:

import random

while True:
    n = random.randrange(1,101)
    print(n)


if n == 20:
    print("That is the number we are looking for")
    break

Rock Paper Scissors does an extra loop before return, not sure how to fix this

If you scroll down to my Game loop comment, I have my function game(). I want my game to stop looping when either the playerPoints or computerPoints reaches 5. Both of them have a separate return in the if else statements. The problem for me is that instead of adding the final point and then returning the "you win" or "you lose" statements, it prompts the user one more time, and then returns the "You win" or "you lose". It doesn't interfere with the points, however it's a small bug I still want to fix.

When I review my code, I know my alert(playRound) is after my gameContinue declaration and I wonder if it's placement is the reason it is happening again and then returning my result. If that is the problem, how can I continue my game loop until someone reaches five points? If that isn't the problem, then could I just get a tip on what method I should try instead? Thank you.

let playerPoints = 0;
let computerPoints = 0;


// Computer Selection
function computerPlay() {

    const compAnswer = options[Math.floor(Math.random() * options.length)];
    return compAnswer;
}
// One round of the game results

function playRound(playerSelection, computerSelection) {

    let rock = options[0];
    let paper = options[1];
    let scissors = options[2];
    let youWin = "You Win! ";
    let youLose = "You Lose! ";
    let rockWin = "Rock beats scissors.";
    let scissorWin = "Scissors beats paper.";
    let paperWin = "Paper beats rock.";
    let tie = "It's a tie, no points added."
    if (playerSelection === computerSelection) {
        console.log(playerPoints, computerPoints);
        return tie;
    } else if (playerSelection === rock && computerSelection === scissors) {
        playerPoints = playerPoints + 1;
        console.log(playerPoints, computerPoints);
        return youWin + rockWin;

    } else if (playerSelection === rock && computerSelection == paper) {
        computerPoints = computerPoints + 1;
        console.log(playerPoints, computerPoints);
        return youLose + paperWin;

    } else if (playerSelection === scissors && computerSelection === rock) {
        computerPoints = computerPoints + 1;
        console.log(playerPoints, computerPoints)
        return youLose + rockWin;
    } else if (playerSelection === scissors && computerSelection === paper) {
        playerPoints = playerPoints + 1;
        console.log(playerPoints, computerPoints)
        return youWin + scissorWin;
    } else if (playerSelection === paper && computerSelection === rock) {
        playerPoints = playerPoints + 1;
        console.log(playerPoints, computerPoints);
        return youWin + paperWin;
    } else if (playerSelection === paper && computerSelection === scissors) {
        computerPoints = computerPoints + 1;
        console.log(playerPoints, computerPoints);
        return youLose + scissorWin;
    } else {
        console.log(playerPoints, computerPoints);
        return "I'm sorry please try another answer"
    }


};
//Game loop
function game() {

    let gameContinue = true;
    while (gameContinue) {
        const computerSelection = computerPlay();
        const playerSelection = prompt("Choose your weapon");

        if (playerPoints < 5 && computerPoints < 5) {
            gameContinue = true;
            alert(playRound(playerSelection, computerSelection));
        } else if (playerPoints === 5) {
            gameContinue = false;
            console.log("You win");
        } else if (computerPoints === 5) {
            gameContinue = false;
            console.log("You Lose")
        }
    }

};

game();

How can I use conditional logic with JavaScript form validation?

I have the following JavaScript function which is triggered by an onclickevent and is working fine.

<script>
  function validateForm() {
    let xgame_name = document.forms['myForm']['game_name'].value;
    if (xgame_name == '') {
      alert('Game Name must be filled out');
      return false;
    }
    let xdev_name = document.forms['myForm']['developer_name'].value;
    if (xdev_name == '') {
      alert('Developer Name must be filled out');
      return false;
    }
    let xdev_email = document.forms['myForm']['email'].value;
    if (xdev_email == '') {
      alert('Developer Email must be filled out');
      return false;
    }
    let xdemo_rom = document.forms['myForm']['demo_rom'].value;
    if (xdemo_rom == '') {
      alert('Demo Rom must be uploaded');
      return false;
    }

    let xpromo_image = document.forms['myForm']['promo_image'].value;
    if (xpromo_image == '') {
      alert('Promo must be uploaded');
      return false;
    }
  }
</script>

I am trying to add this so if one of the radio buttons with a value of 1 is selected on the form it will check an additional field to see if there is a value and show an alert.

let xcartridge = document.forms['myForm']['cartridge'].value;
if (xcartridge == '1') {
  let xcover_art = document.forms['myForm']['cover_art'].value;
  if (xcover_art == '') {
    alert('If Cartridge is selected you must proved Cover Art');
    return false;
  }
}

This follows the same syntax of the above code example that is working but this does not send an alert but rather the form validation does not work at all. How can I get the alert to show when one fields condition is met, where it is 1 and that prompts an alert on an additional field?

How do I take elements from a list of n length?

I am trying to make a fun program. Its making a print statement for the famous quote: "The name is Bond, James Bond. It was really easy to make with a first name and last name, but my question now is what happens if a person has a middle name too or four names? What would the code look like? Like let us say your name Augustus De Morgan, i want the code to then give the output:

The name is de Morgan, Augustus De Morgan...

I've tried with list comprehension but can't figure it out. I must be missing out on something here.

name = input('My name is: ')
split = name.split() #makes a list

if len(split) == 2:
    print(f'The name is {split[1]}, {split[0]} {split[1]}') #picking strings from list
elif len(split) > 2:
        [print(split[i], split[0], split[i]) for i in split]