[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

explode a string function gets an undefined result

jrough

9/7/2015 1:04:00 AM

var str = 'janis';
var myfunc = function explode(str){
for (var i=0, out; i< str.length; i++){
out = out + " " + str.charAt(i);

}
alert(out);
}
output = myfunc(str);


why is the result "undefined j a n i s???, thanks
9 Answers

Tim Streater

9/7/2015 8:37:00 AM

0

In article <f938f9dd-e322-4370-bc80-379ea36b83aa@googlegroups.com>,
JRough <janis.rough@gmail.com> wrote:

>var str = 'janis';
>var myfunc = function explode(str){
>for (var i=0, out; i< str.length; i++){
>out = out + " " + str.charAt(i);
>
>}
> alert(out);
>}
>output = myfunc(str);
>
>why is the result "undefined j a n i s???, thanks

You haven't initialised "out".

--
"People don't buy Microsoft for quality, they buy it for compatibility
with what Bob in accounting bought last year. Trace it back - they buy
Microsoft because the IBM Selectric didn't suck much" - P Seebach, afc

Stefan Weiss

9/7/2015 12:26:00 PM

0

On 2015-09-07 03:03, JRough wrote:
> var str = 'janis';
> var myfunc = function explode(str){
> for (var i=0, out; i< str.length; i++){
> out = out + " " + str.charAt(i);
>
> }
> alert(out);
> }
> output = myfunc(str);
>
>
> why is the result "undefined j a n i s???, thanks

See Tim Streater's answer: `out` is declared but not defined.
undefined + " " -> "undefined "

I couldn't help noticing two other things (apart from the unconventional
indentation):

1) Do you want to address your function as myfunc() or explode()?

2) Consider "janis".split("").join(" ")


- stefan

Tim Streater

9/7/2015 2:59:00 PM

0

In article <msjvp4$vde$1@news.albasani.net>, Stefan Weiss
<krewecherl@gmail.com> wrote:

>On 2015-09-07 03:03, JRough wrote:
>> var str = 'janis';
>> var myfunc = function explode(str){
>> for (var i=0, out; i< str.length; i++){
>> out = out + " " + str.charAt(i);
>>
>> }
>> alert(out);
>> }
>> output = myfunc(str);
>>
>>
>> why is the result "undefined j a n i s???, thanks
>
>See Tim Streater's answer: `out` is declared but not defined.
>undefined + " " -> "undefined "
>
>I couldn't help noticing two other things (apart from the unconventional
>indentation):
>
>1) Do you want to address your function as myfunc() or explode()?
>
>2) Consider "janis".split("").join(" ")

I thought there were probably better answers than mine. :-)

--
"If you're not able to ask questions and deal with the answers without feeling
that someone has called your intelligence or competence into question, don't
ask questions on Usenet where the answers won't be carefully tailored to avoid
tripping your hair-trigger insecurities." - D M Procida, UCSM

jrough

9/8/2015 9:30:00 PM

0

On Sunday, September 6, 2015 at 6:03:44 PM UTC-7, JRough wrote:
> var str = 'janis';
> var myfunc = function explode(str){
> for (var i=0, out; i< str.length; i++){
> out = out + " " + str.charAt(i);
>
> }
> alert(out);
> }
> output = myfunc(str);
>
>
> why is the result "undefined j a n i s???, thanks

thanks for the split and join method of chaining, more clean but I sitll don't know why mine doesn't work? I tried it with JSlint.
var str = 'janis';
function explode(str) {
use 'strict';
var i, out;
var mystr = str;
for (i=0; i< mystr.length; i++){
use 'strict';
out = out + " " + mystr.charAt(i);
}
return out;
}
var res= explode(str);
alert(res);

now it says unexpected string. Is it saying the mystr is not a string for the charAt method?

Evertjan.

9/8/2015 10:02:00 PM

0

JRough <janis.rough@gmail.com> wrote on 08 Sep 2015 in comp.lang.javascript:
> var str = 'janis';
> function explode(str) {
> use 'strict';

'use strict';

> var i, out;
> var mystr = str;
> for (i=0; i< mystr.length; i++){
> use 'strict';

'use strict';

> out = out + " " + mystr.charAt(i);
>}
> return out;
>}
> var res= explode(str);
> alert(res);

However, why do you need 'use strict' and why twice?


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Stefan Weiss

9/8/2015 10:51:00 PM

0

On 2015-09-09 00:01, Evertjan. wrote:
> JRough <janis.rough@gmail.com> wrote on 08 Sep 2015 in comp.lang.javascript:
>> var str = 'janis';
>> function explode(str) {
>> use 'strict';
>
> 'use strict';

Exactly. I have to admit, I've made variations of this error myself a
few times... old Perl habits die hard :)

> However, why do you need 'use strict'

To make JSLint happy, I suppose. Using a lint checker before posting is
commendable, but I would personally recommend JSHint over JSLint (for
various reasons; the margin of this message is too narrow to contain
them all). Both linters have on/off settings for strict mode.

Janis: This script will have the same problem as before, even when the
syntax error is resolved. `out` is still undefined the first time you
add " " to it, resulting in "undefined " again. To solve this, you need
to initialize `out` to an empty string.

var i, out = "";
for (i = 0; i < mystr.length; i++) {
...

When this works, the result should be " j a n i s". Note the leading
space. If you don't want that, your function could return out.trim() or
out.substring(1) or out.slice(1).


- stefan

Evertjan.

9/9/2015 7:20:00 AM

0

Stefan Weiss <krewecherl@gmail.com> wrote on 09 Sep 2015 in
comp.lang.javascript:

>> However, why do you need 'use strict'
>
> To make JSLint happy, I suppose. Using a lint checker before posting is
> commendable, but I would personally recommend JSHint over JSLint (for
> various reasons; the margin of this message is too narrow to contain
> them all). Both linters have on/off settings for strict mode.

This is a strange habit in my eyes.

Writing correct and beautifull code should and does make me happy,
my quest is not making code that makes some other code happy.

The F12-console of Chrome immediately pointed
to the syntax-error in "use 'strict';".

However, if those inanimate Lints are your god,
you are not alone, it seems.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

jrough

9/9/2015 5:30:00 PM

0

On Tuesday, September 8, 2015 at 3:50:49 PM UTC-7, Stefan Weiss wrote:
> On 2015-09-09 00:01, Evertjan. wrote:
> > JRough <janis.rough@gmail.com> wrote on 08 Sep 2015 in comp.lang.javascript:
> >> var str = 'janis';
> >> function explode(str) {
> >> use 'strict';
> >
> > 'use strict';
>
> Exactly. I have to admit, I've made variations of this error myself a
> few times... old Perl habits die hard :)
>
> > However, why do you need 'use strict'
>
> To make JSLint happy, I suppose. Using a lint checker before posting is
> commendable, but I would personally recommend JSHint over JSLint (for
> various reasons; the margin of this message is too narrow to contain
> them all). Both linters have on/off settings for strict mode.
>
> Janis: This script will have the same problem as before, even when the
> syntax error is resolved. `out` is still undefined the first time you
> add " " to it, resulting in "undefined " again. To solve this, you need
> to initialize `out` to an empty string.
>
> var i, out = "";
> for (i = 0; i < mystr.length; i++) {
> ...
>
> When this works, the result should be " j a n i s". Note the leading
> space. If you don't want that, your function could return out.trim() or
> out.substring(1) or out.slice(1).
>
>
> - stefan

thanks,

Tim Streater

9/9/2015 9:16:00 PM

0

In article <0cf4dd22-2e79-4c15-96b3-484c4a88aea5@googlegroups.com>,
JRough <janis.rough@gmail.com> wrote:

>On Sunday, September 6, 2015 at 6:03:44 PM UTC-7, JRough wrote:
>> var str = 'janis';
>> var myfunc = function explode(str){
>> for (var i=0, out; i< str.length; i++){
>> out = out + " " + str.charAt(i);
>>
>> }
>> alert(out);
>> }
>> output = myfunc(str);
>>
>>
>> why is the result "undefined j a n i s???, thanks
>
>thanks for the split and join method of chaining, more clean but I sitll don't
>know why mine doesn't work? I tried it with JSlint.

I fucking told you why it doesn't work. Get a grip, why don't you.

--
Lady Astor: "If you were my husband I'd give you poison." Churchill: "If
you were my wife, I'd drink it."