Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #28655 > unrolled thread

explode a string function gets an undefined result

Started byJRough <janis.rough@gmail.com>
First post2015-09-06 18:03 -0700
Last post2015-09-09 22:15 +0100
Articles 10 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  explode a string function gets an undefined result JRough <janis.rough@gmail.com> - 2015-09-06 18:03 -0700
    Re: explode a string function gets an undefined result Tim Streater <timstreater@greenbee.net> - 2015-09-07 09:37 +0100
    Re: explode a string function gets an undefined result Stefan Weiss <krewecherl@gmail.com> - 2015-09-07 14:26 +0200
      Re: explode a string function gets an undefined result Tim Streater <timstreater@greenbee.net> - 2015-09-07 15:58 +0100
    Re: explode a string function gets an undefined result JRough <janis.rough@gmail.com> - 2015-09-08 14:29 -0700
      Re: explode a string function gets an undefined result "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-09 00:01 +0200
        Re: explode a string function gets an undefined result Stefan Weiss <krewecherl@gmail.com> - 2015-09-09 00:50 +0200
          Re: explode a string function gets an undefined result "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-09 09:19 +0200
          Re: explode a string function gets an undefined result JRough <janis.rough@gmail.com> - 2015-09-09 10:30 -0700
      Re: explode a string function gets an undefined result Tim Streater <timstreater@greenbee.net> - 2015-09-09 22:15 +0100

#28655 — explode a string function gets an undefined result

FromJRough <janis.rough@gmail.com>
Date2015-09-06 18:03 -0700
Subjectexplode a string function gets an undefined result
Message-ID<f938f9dd-e322-4370-bc80-379ea36b83aa@googlegroups.com>
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

[toc] | [next] | [standalone]


#28657

FromTim Streater <timstreater@greenbee.net>
Date2015-09-07 09:37 +0100
Message-ID<070920150937156731%timstreater@greenbee.net>
In reply to#28655
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

[toc] | [prev] | [next] | [standalone]


#28658

FromStefan Weiss <krewecherl@gmail.com>
Date2015-09-07 14:26 +0200
Message-ID<msjvp4$vde$1@news.albasani.net>
In reply to#28655
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

[toc] | [prev] | [next] | [standalone]


#28659

FromTim Streater <timstreater@greenbee.net>
Date2015-09-07 15:58 +0100
Message-ID<070920151558455577%timstreater@greenbee.net>
In reply to#28658
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

[toc] | [prev] | [next] | [standalone]


#28663

FromJRough <janis.rough@gmail.com>
Date2015-09-08 14:29 -0700
Message-ID<0cf4dd22-2e79-4c15-96b3-484c4a88aea5@googlegroups.com>
In reply to#28655
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?

[toc] | [prev] | [next] | [standalone]


#28664

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-09-09 00:01 +0200
Message-ID<XnsA5105679FD8eejj99@194.109.6.166>
In reply to#28663
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)

[toc] | [prev] | [next] | [standalone]


#28665

FromStefan Weiss <krewecherl@gmail.com>
Date2015-09-09 00:50 +0200
Message-ID<msnonv$5h2$1@news.albasani.net>
In reply to#28664
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

[toc] | [prev] | [next] | [standalone]


#28667

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-09-09 09:19 +0200
Message-ID<XnsA5105EE536415eejj99@194.109.6.166>
In reply to#28665
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)

[toc] | [prev] | [next] | [standalone]


#28668

FromJRough <janis.rough@gmail.com>
Date2015-09-09 10:30 -0700
Message-ID<71bb8b99-c209-4491-8edf-7eea8a30eb44@googlegroups.com>
In reply to#28665
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,

[toc] | [prev] | [next] | [standalone]


#28669

FromTim Streater <timstreater@greenbee.net>
Date2015-09-09 22:15 +0100
Message-ID<090920152215384863%timstreater@greenbee.net>
In reply to#28663
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."

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web