Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7867965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:50:59+00:00 2026-06-03T00:50:59+00:00

I’ve created a subclass of Array and written some messages for it that have

  • 0

I’ve created a subclass of Array and written some messages for it that have worked out well so far. However, I’m now having trouble with my last message Here’s the body of my code:

startString: charToTest
"Returns a list of all the words that start with a given character"
|  toReturn i j numWords |
i:= 1.
j:= 1.
numWords := 0.
[i <= self size.] whileTrue: [
    (((self at: i) at: 1) = charToTest)
        ifTrue: [numWords := numWords + 1].
    i:= i+1.
].
toReturn := MyArray new: numWords.
i := 1. 
[i <= numWords.] whileTrue: [
    (((self at: i) at: 1) = charToTest)
        ifTrue: [toReturn at: j put: (self at: i). j := j+1].
    i := i + 1.
].
^toReturn

So, essentially, the method looks at itself and creates a new array the size of the number of words that start with the character. It then iterates through again, and puts these words into a new array which it Returns. My workspace looks exactly like this:

|freshArray testArray|
freshArray := MyArray new: 5.
freshArray
    at: 1 put: 'some';
    at: 2 put: 'things';
    at: 3 put: 'are';
    at: 4 put: 'simple';
    at: 5 put: 'things'.
testArray := freshArray startString: $s

When I select this code in my workspace and print it, the method is almost correct. But not quite. For some reason the second slot in “toReturn” isn’t filled. The output looks like:

 a MyArray('some' nil)

Why is this happening? Am I separating my statements correctly in the second iterative loop after the if statement? I’m genuinely confused as to what I’ve been doing wrong. Any help would be appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:51:00+00:00Added an answer on June 3, 2026 at 12:51 am

    Your first iteration counts the number of words that start with the given character (which assumes that the Strings aren’t empty, but well you can include a check for that later). So far so good. The second time you’re using that count (numWords) to iterate over the array again though – which means that only the words that are in the first ‘numWords’ slots of the initial array are even considered.

    (You may also want to consider replacing your whiles with do: aBlock – that’s much less messy and more Smalltalk-y)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.