Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tip #06 incorrect code for what it suggests #27

Closed
volcodc opened this issue Jan 7, 2016 · 7 comments
Closed

Tip #06 incorrect code for what it suggests #27

volcodc opened this issue Jan 7, 2016 · 7 comments

Comments

@volcodc
Copy link

volcodc commented Jan 7, 2016

Tip #6 suggests that it's code returns the first letter capital rather than the whole word.

printUpperCase("cactus");
// => Cactus

Ideally, if the end result wanted to look like that, the code should reflect something like this:

function printUpperCase(words) {
  var elements = [].concat(words); 
  for (var i = 0; i < elements.length; i++) {
    var tmpArr = elements[i].split(''),
        retVal = tmpArr.shift().toUpperCase() + tmpArr.join('');
    console.log(retVal);
  }
}

Just wanted to assist! Thanks!
-Chris

@loverajoel
Copy link
Owner

@Mattfxyz ping here!

@mattfaluotico
Copy link
Contributor

You're right Chris. This was the print every word in all uppercase.m. I incorrectly wrote the sample output.

@grav
Copy link

grav commented Jan 7, 2016

Consider map'ing a function over an array instead of changing each function to contain a for-loop

["foo","bar"].map(someFunction)

It is an ES5 feature: http://caniuse.com/#search=map

@Roland1975
Copy link

I'd rather avoid passing an array of items. Let the function proceed the arguments as an array using the native 'slice':

function printUpperCase(/* item1, item2, item n */) {
  var elements = Array.prototype.slice.call(arguments);
  //...
}
printUpperCase('item1', 'item2'); //ITEM1, ITEM2

An array is an object; if you keep it local inside the function, the browser will garbage it as soon as the function exits.

@kurtextrem
Copy link
Contributor

That only works with use strict, else we leak arguments.

Roland notifications@github.com schrieb am Fr., 8. Jan. 2016 18:15:

I'd rather avoid passing an array of items. Let the function proceed the
arguments as an array using the native 'slice':

function printUpperCase(/* item1, item2, item n */) {
var elements = Array.prototype.slice.call(arguments);
//...
}

printUpperCase('item1', 'item2'); //ITEM1, ITEM2


Reply to this email directly or view it on GitHub
#27 (comment).

Mit freundlichen Grüßen,
Jacob Groß

@loverajoel
Copy link
Owner

Hey can we close this?

@zenopopovici
Copy link
Collaborator

Please submit changes in a PR and re-open this if you believe the tip is still not OK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants