I'm trying to create a little powershell program which asks you a random word and you have to translate it. I'm using the index of both arrays to determine if the answer is correct or not.
Everytime the user enters a correct or wrong answer a textbox should increase it's value to show how many the user got right and wrong so far. The program works as it should so far but the counter isn't. At this moment the counter increments only to 1 and doesn't go above 1. Am i missing something?
I think the problem could be that PowerShell always calls the value from the external countercorrect and counterwrong which is 0 but how could i solve it that it only calls the incremented value?
$QuestionArray = New-Object System.Collections.ArrayList
$AnswerArray = New-Object System.Collections.ArrayList
$countercorrect = 0
$counterwrong = 0
#word array
$QuestionArray.Add("word1")
$QuestionArray.Add("word2")
$QuestionArray.Add("word3")
#solution array
$AnswerArray.Add("answer1")
$AnswerArray.Add("answer2")
$AnswerArray.Add("answer3")
#Function to display a new word
function Question {
$global:RandomQuestion = $QuestionArray | Get-Random
$SearchedTextbox.Text = $global:RandomQuestion
}
$InputTextbox.Add_KeyDown({
if ($_.KeyCode -eq "Enter") {
#Get User Guess
$Answer = $InputTextbox.Text
#Get Solution array Index
$IndexPositionQuestion = [array]::indexof($QuestionArray, $global:RandomQuestion)
#Get User answer array Index
$IndexPositionAnswer = [array]::indexof($AnswerArray, $Answer)
#Check if both indexes match
If($IndexPositionAnswer -eq $IndexPositionQuestion){
#this fails / doesn't go above 1
$RightTextBox.Text = countercorrect++
Question
}else{
#this fails / doesn't go above 1
$WrongtTextBox.Text = counterwrong++
Question
}
}
})
I tried using a seperate function to increment its value but even that only increased to 1.
$RightTextBox.Text = countercorrect++. Are these just a copy-paste-edit error?$countercorrect++->$script:countercorrect++,$counterwrong++->$script:counterwrong++. You are in a function's scope when increasing/decreasing the counter (untested but I'm pretty confident). And yeah as @vonPryz mentionned, you miss a couple$signs.