-2

This should take the the letter convert it to it's ASCII value then add all the ASCII values of the letters in the array together and then convert it back into a lowercase letter. The condition is just to check if it goes past 122 aka z. The Write-Host statements are just for figuring out whats wrong.

$t=@('a','b','c')
$y=0
  foreach ($x in $letters) {
    #write-host $x
    $y=+ [byte][int]$x
    write-host $y
    if ($y -gt 122) {
       $y= $y - 24
 }
 }
 $z=[char]$y
 write-host $z
Stormer3
  • 1
  • 1
  • 3

1 Answers1

3

To convert your array of characters to lowercase you can use String.ToLower method:

$letters=@('A','B','C')
$arr = foreach ($x in $letters) {
  $l.toLower()
}

Or even shorter (thx TheIncorrigible1 for mentioning member enumeration in your comment):

$letters=@('A','B','C')
$letters.ToLower()

You can read more about member enumeration in this answer.

If you really want to convert to operate on ASCII values, you have to convert to char and then to int like so:

$letters = @('A','B','C')
$arr = foreach ($l in $letters) {
  $converted = [int][char]$l
  # Check if uppercase
  if ($converted -ge 65 -and $converted -le 90) {
    # Convert to lowercase by adding 32
    $converted += 32
  }
  # Return
  $converted
}
$convertedToLower=[char[]]$arr
Write-Host $convertedToLower

Few tips to your code:

  • use meaningful variable naming - it makes code easier to understand
  • if using multiple conversions, make sure they work. For example start with:
    $x = $t[0]
    [int]$x
    
    and you'll receive an error immediately.
  • By using $y = 0 you specify $y to be int. When you add any numeric value to int, it'll sum it instead creating array. If you want an array, initialize it with $y = @().
  • You tried to compare $y with 122 while $y should be (that's just my guess as it's unclear from your code) array. Comparing array to integer doesn't sound like good idea so try to avoid it as it might give you surprising results.
Robert Dyjas
  • 4,979
  • 3
  • 19
  • 34
  • 1
    You don't even need to do all that work. `$letters.ToLower()` will work just fine (automatic member enumeration introduced in v3) – Maximilian Burszley Dec 11 '19 at 20:19
  • I'm sorry when i copied the code i actually put the array in uppercase letters and didnt notice this is a code wars activity i'm doing and it does only give me lowercase letters. – Stormer3 Dec 12 '19 at 13:22
  • @Stormer3 [edit] your question to be more precise - currently the question is closed as not detailed enough. See [ask] for more hints. – Robert Dyjas Dec 12 '19 at 14:17