/    Sign up×
Community /Pin to ProfileBookmark

How to implement STRING object

please help me with this question(from the book of Stefan Stoyanov):
Imagine the String() constructor didn’t exist. Create a constructor function MyString() that acts like String() as closely as possible. You’re not allowed to use any built-in string methods or properties, and remember that String() doesn’t exist. You can use this code to test your constructor:

>>>

var s = new MyString(‘hello’);
>>> s.length;
5
>>> s[0];
“h”
>>> s.toString();
“hello”
>>> s.valueOf();
“hello”
>>> s.charAt(1);
“e”
>>> s.charAt(‘2’);
“l”
>>> s.charAt(‘e’);
“h”
>>> s.concat(‘ world!’);
“hello world!”
>>> s.slice(1,3);
“el”
>>> s.slice(0,-1);
“hell”
>>> s.split(‘e’);
[“h”, “llo”]
>>> s.split(‘l’);
[“he”, “”, “o”]

*You can use a for-in to loop through the input string, treating it as
an array.

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@orbachklauthorJan 12.2012 — I don't understand how to implement length and toString
Copy linkTweet thisAlerts:
@JMRKERJan 12.2012 — Is this a "java" book as opposed to "javascript"?
Copy linkTweet thisAlerts:
@svidgenJan 12.2012 — Is this a "java" book as opposed to "javascript"?[/QUOTE]

Looks like Java[I]Script[/I] to me. Also seems like JavaScript is a silly language to be taking a Data Structures class in. But, the real question is ... shouldn't you be figuring this out on your own!? Or phrased another way, isn't generically asking for "help" about a question from your textbook sort of like .. cheating?
Copy linkTweet thisAlerts:
@orbachklauthorJan 12.2012 — The main question is the quetion I am asking

why do you care about the reasons I am asking the question?


It's JAVASCRIPT

and this is the I learn oop
Copy linkTweet thisAlerts:
@svidgenJan 12.2012 — why do you care about the reasons I am asking the question?[/QUOTE]

Well, because if this is for school, I'm doing you, your classmates, and your educational facility a disservice by giving you a tutorial on answering a homework question.
Copy linkTweet thisAlerts:
@xelawhoJan 12.2012 — The main question is the quetion I am asking[/QUOTE]

I've read your post a few times, and I can't see the question that you're asking - would you mind repeating it?
Copy linkTweet thisAlerts:
@orbachklauthorJan 12.2012 — I'm a university graduate and I decided to do this exercise.

my problem is I don't understand how to implement length and toString properties
Copy linkTweet thisAlerts:
@xelawhoJan 12.2012 — ah, ok.

well what do you have so far?
Copy linkTweet thisAlerts:
@orbachklauthorJan 12.2012 — I think I have to use the operator this but I don't know if I'm right

and I don't know how to do it with "this".

I don't know how to get to the problem

can I treat the object I create as array? I'm not so sure about it
Copy linkTweet thisAlerts:
@xelawhoJan 12.2012 — *You can use a for-in to loop through the input string, treating it as an array.[/QUOTE]

I would say yes
Copy linkTweet thisAlerts:
@TcobbJan 13.2012 — [B]*You can use a for-in to loop through the input string, treating it as an array.[/B]

Sorry- but this is garbage. You must use a data-type which the problem definition says doesn't and cannot exist when the language itself insists does and must exist. If you were talking about C I can understand how this might be a valid problem
Copy linkTweet thisAlerts:
@svidgenJan 13.2012 — [B]*You can use a for-in to loop through the input string, treating it as an array.[/B]

Sorry- but this is garbage. You must use a data-type which the problem definition says doesn't and cannot exist when the language itself insists does and must exist. If you were talking about C I can understand how this might be a valid problem[/QUOTE]


Hehe ... I agree. Even if the problem were set up correctly, forcing instantiation of the MyString object, one of the requirements is to implement a toString() method, which does what? -- Oh yeah, it returns a [B]String[/B] ...
×

Success!

Help @orbachkl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...