New to Powershell, Stuck with BobResponse Question on Exercism

Hello, I have written the following codes for the 2nd problem in the following Powershell track on Exercism: Bob in PowerShell on Exercism

Code1 (10/25 tests passed):

Function Get-BobResponse() {

    [CmdletBinding()]
    Param(
        [string]$HeyBob
    )
    $temp = [string]$HeyBob
    $temp = $temp.ToUpper()
    if ([string]::IsNullOrWhiteSpace($HeyBob)) {
        return "Fine. Be that way!"
    } elseif ($HeyBob -eq $temp -and $HeyBob.EndsWith("?")) {
        return "Calm down, I know what I'm doing!"
    } elseif ($HeyBob -eq $temp) {
        return "Whoa, chill out!"
    } elseif ($HeyBob.EndsWith("?")) {
        return "Sure."
    } else {
        return "Whatever."
    }
}

Code2 (11/25 tests passed):

Function Get-BobResponse() {
    [CmdletBinding()]
    Param(
        [string]$HeyBob
    )
    $temp = [string]$HeyBob
    $temp = $temp.ToUpper()

    switch -Wildcard ($HeyBob) {
        { [string]::IsNullOrWhiteSpace($_) } { return "Fine. Be that way!" }
        { $_ -like "*?" } { return "Sure." }
        { $_ -eq $temp -and $_.EndsWith("?") } { return "Calm down, I know what I'm doing!" }
        { $_ -eq $temp } { return "Whoa, chill out!" }
        default { return "Whatever." }
    }
}

I am unable to understand why the responses are going haywire to pretty standard inputs.

Could someone help solve this and point out what I could have done differently, as I am unable to research a solution from the Internet :"

  1. You can format your code with a codeblock to make it easier to read.
  2. What does the test output show?

Hi IsaacG,

  1. I have tried to make it more readable in the new edit. Thanks for the pointer.

  2. There are 25 tests to be run out of which almost 15 are failing and each has a different input, expected output, and actual output. To give the outputs for all would clutter this space.

Might I suggest running any of the above codes in the online IDE of this link Exercism ? Will just take a minute of your time.

Thank you!

The forum will scroll long outputs in the codeblock. If you’d like help, please do provide as much relevant detail as you can, including whichever failed tests you’re stuck on. This is true everywhere, not just on Exercism. The more details you can provide, the easier people can help you.

See, too, How to ask for help with your code online | Angelika.me

This is as close to posting test cases I could come:

TEST FAILURE
Message: Expected strings to be the same, but they were different.
Expected length: 9
Actual length: 16
Strings differ at index 2.
Expected: ‘Whatever.’
But was: ‘Whoa, chill out!’
–^

Stack-trace: at Get-BobResponse -HeyBob “Tom-ay-to, tom-aaaah-to.” | Should -BeExactly “Whatever.”, /mnt/exercism-iteration/BobResponse.tests.ps1:8
at , /mnt/exercism-iteration/BobResponse.tests.ps1:8

TEST FAILURE
Message: Expected strings to be the same, but they were different.
Expected length: 5
Actual length: 33
Strings differ at index 0.
Expected: ‘Sure.’
But was: ‘Calm down, I know what I’m doing!’
^

Stack-trace: at Get-BobResponse -HeyBob “Does this cryogenic chamber make me look fat?” | Should -BeExactly “Sure.”, /mnt/exercism-iteration/BobResponse.tests.ps1:20
at , /mnt/exercism-iteration/BobResponse.tests.ps1:20

TEST FAILURE
Message: Expected strings to be the same, but they were different.
Expected length: 5
Actual length: 33
Strings differ at index 0.
Expected: ‘Sure.’
But was: ‘Calm down, I know what I’m doing!’
^

Stack-trace: at Get-BobResponse -HeyBob “You are, what, like 15?” | Should -BeExactly “Sure.”, /mnt/exercism-iteration/BobResponse.tests.ps1:24
at , /mnt/exercism-iteration/BobResponse.tests.ps1:24

TEST FAILURE
Message: Expected strings to be the same, but they were different.
Expected length: 5
Actual length: 33
Strings differ at index 0.
Expected: ‘Sure.’
But was: ‘Calm down, I know what I’m doing!’
^

Stack-trace: at Get-BobResponse -HeyBob “fffbbcbeab?” | Should -BeExactly “Sure.”, /mnt/exercism-iteration/BobResponse.tests.ps1:28
at , /mnt/exercism-iteration/BobResponse.tests.ps1:28

TEST FAILURE
Message: Expected strings to be the same, but they were different.
Expected length: 9
Actual length: 16
Strings differ at index 2.
Expected: ‘Whatever.’
But was: ‘Whoa, chill out!’
–^

Stack-trace: at Get-BobResponse -HeyBob “Let’s go make out behind the gym!” | Should -BeExactly “Whatever.”, /mnt/exercism-iteration/BobResponse.tests.ps1:32
at , /mnt/exercism-iteration/BobResponse.tests.ps1:32

TEST FAILURE
Message: Expected strings to be the same, but they were different.
Expected length: 9
Actual length: 16
Strings differ at index 2.
Expected: ‘Whatever.’
But was: ‘Whoa, chill out!’
–^

Stack-trace: at Get-BobResponse -HeyBob “It’s OK if you don’t want to go to the DMV.” | Should -BeExactly “Whatever.”, /mnt/exercism-iteration/BobResponse.tests.ps1:36
at , /mnt/exercism-iteration/BobResponse.tests.ps1:36

TEST FAILURE
Message: Expected strings to be the same, but they were different.
Expected length: 9
Actual length: 16
Strings differ at index 2.
Expected: ‘Whatever.’
But was: ‘Whoa, chill out!’
–^

Stack-trace: at Get-BobResponse -HeyBob “1, 2, 3” | Should -BeExactly “Whatever.”, /mnt/exercism-iteration/BobResponse.tests.ps1:48
at , /mnt/exercism-iteration/BobResponse.tests.ps1:48

TEST FAILURE
Message: Expected strings to be the same, but they were different.
Expected length: 5
Actual length: 33
Strings differ at index 0.
Expected: ‘Sure.’
But was: ‘Calm down, I know what I’m doing!’
^

Stack-trace: at Get-BobResponse -HeyBob “4?” | Should -BeExactly “Sure.”, /mnt/exercism-iteration/BobResponse.tests.ps1:52
at , /mnt/exercism-iteration/BobResponse.tests.ps1:52

TEST FAILURE
Message: Expected strings to be the same, but they were different.
Expected length: 9
Actual length: 16
Strings differ at index 2.
Expected: ‘Whatever.’
But was: ‘Whoa, chill out!’
–^

Stack-trace: at Get-BobResponse -HeyBob “Ending with ? means a question.” | Should -BeExactly “Whatever.”, /mnt/exercism-iteration/BobResponse.tests.ps1:64
at , /mnt/exercism-iteration/BobResponse.tests.ps1:64

TEST FAILURE
Message: Expected strings to be the same, but they were different.
Expected length: 5
Actual length: 33
Strings differ at index 0.
Expected: ‘Sure.’
But was: ‘Calm down, I know what I’m doing!’
^

Stack-trace: at Get-BobResponse -HeyBob “:) ?” | Should -BeExactly “Sure.”, /mnt/exercism-iteration/BobResponse.tests.ps1:68
at , /mnt/exercism-iteration/BobResponse.tests.ps1:68

TEST FAILURE
Message: Expected strings to be the same, but they were different.
Expected length: 5
Actual length: 33
Strings differ at index 0.
Expected: ‘Sure.’
But was: ‘Calm down, I know what I’m doing!’
^

Stack-trace: at Get-BobResponse -HeyBob “Wait! Hang on. Are you going to be OK?” | Should -BeExactly “Sure.”, /mnt/exercism-iteration/BobResponse.tests.ps1:72
at , /mnt/exercism-iteration/BobResponse.tests.ps1:72

TEST FAILURE
Message: Expected strings to be the same, but they were different.
Expected length: 9
Actual length: 16
Strings differ at index 2.
Expected: ‘Whatever.’
But was: ‘Whoa, chill out!’
–^

Stack-trace: at Get-BobResponse -HeyBob “nDoes this cryogenic chamber make me look fat?nno” | Should -BeExactly “Whatever.”, /mnt/exercism-iteration/BobResponse.tests.ps1:88
at , /mnt/exercism-iteration/BobResponse.tests.ps1:88

TEST FAILURE
Message: Expected strings to be the same, but they were different.
Expected length: 9
Actual length: 16
Strings differ at index 2.
Expected: ‘Whatever.’
But was: ‘Whoa, chill out!’
–^

Stack-trace: at Get-BobResponse -HeyBob " hmmmmmmm…" | Should -BeExactly “Whatever.”, /mnt/exercism-iteration/BobResponse.tests.ps1:92
at , /mnt/exercism-iteration/BobResponse.tests.ps1:92

TEST FAILURE
Message: Expected strings to be the same, but they were different.
Expected length: 5
Actual length: 16
Strings differ at index 0.
Expected: ‘Sure.’
But was: ‘Whoa, chill out!’
^

Stack-trace: at Get-BobResponse -HeyBob "Okay if like my spacebar quite a bit? " | Should -BeExactly “Sure.”, /mnt/exercism-iteration/BobResponse.tests.ps1:96
at , /mnt/exercism-iteration/BobResponse.tests.ps1:96

TEST FAILURE
Message: Expected strings to be the same, but they were different.
Expected length: 9
Actual length: 16
Strings differ at index 2.
Expected: ‘Whatever.’
But was: ‘Whoa, chill out!’
–^

Stack-trace: at Get-BobResponse -HeyBob "This is a statement ending with whitespace " | Should -BeExactly “Whatever.”, /mnt/exercism-iteration/BobResponse.tests.ps1:104
at , /mnt/exercism-iteration/BobResponse.tests.ps1:104

Using codeblocks would help with the formatting :slight_smile:

Is there no “CODE RUN” blocks?

The first test says it is calling HeyBob “Tom-ay-to, tom-aaaah-to.” and expects Whatever but got Whoa, chill out!.

  1. Do you understand why that test case should return “Whatever”?
  2. Do you understand why your code returns “Whoa, chill out!” for that input?