Isn't this ugly coding? (FCC length-of-a-string)

Isn't this ugly coding? (FCC length-of-a-string)
0

#1

I don’t know much about programming, or javascript, but when I look at this, even as a beginner this looks to me like some form of ugly coding in this FCC excercise here on appending variables:

var firstNameLength = 0;
var firstName = "Ada";

firstNameLength = firstName.length;

I mean: it seems so arbitrary to first set firstNameLength to zero, it could be any number really…
Because later on firstNameLength is going to be set to a different number anyway!
Am I wrong/right?

What would be a more beautiful way to do this?
(probably something advanced but that’s okay).


#2

which challenge is this? I looked up the appending variables to strings challenges and it was this:

var someAdjective = "fun";
var myStr = "Learning to code is ";
myStr += someAdjective;

#3

Yes, there are much more elegant ways to do most of the things that you learn as a beginner. A lot of what you learn is to acquaint you with certain practices that will help later on when you’re writing more complex code. You’re probably learning how to store data in multiple ways so you can more easily manipulate it. Once you grasp the concepts, you’re free to code any way you wish (for personal projects at least).

One reason to declare a variable with a value of 0 is to basically say “this is a number” since JavaScript isn’t strongly typed, and 0 is like a placeholder or default position (my bank account could have any value, yet they keep insisting that I start with $0.00). In other languages, like C++ or Java, you have to declare the type of data, such as ‘int’ for integer instead of ‘var’. When JavaScript code compiles just before runtime, if there is no clear type associated with a variable, it will make its best guess. This is inefficient and could lead to errors. It could also lead to programmer error resulting in conflicting variable definitions.

What we do with firstNameLength depends on the context and purpose of the code. You could just use firstName.length. But firstName could change later on, yet you might for some reason still want the length of the original firstName. More likely there’ll be many firstNames and firstNameLength will be bound to the scope of a function and reused for each firstName. If this makes no sense, just wait until you get to Objects, which shouldn’t be far off.


#4

Thanks for the feedback! If you feel there is a need for improvement or better implementation please checkout the Contribution Guides section of the Wiki https://github.com/FreeCodeCamp/FreeCodeCamp/wiki/Wiki-Central#contribution-guides

Thanks :slight_smile: and happy coding (:


#5

Ah I see, as a beginner I actually find it very, very confusing that javascript isn’t strongly typed. I wish it was :nerd:


#6

It’s also alright to initialize variables without giving values at first :

var firstNameLength, firstName; //initialize both variables with an "undefined" value

firstName = "Ada";

firstNameLength = firstName.length // firstNameLength now is 3
 

#7

Some tips for surviving JS’s broken type system:

  1. Never write functions that accept multiple types for the same parameter. There are challenges where this is the case (like the card challenge taking a number or face card name). That’s okay for the challenge because sadly you will run into this in the real world, but don’t do it yourself.

  2. The same goes for return values of functions. The challenges violate this even more often and flagrantly, returning strings for exceptional conditions. This should even go for null and undefined too, but since JS doesn’t provide you much else, you may have to reach for those as return values. If your API does exceptions, use those instead of nulls. With modern backend code, you’d complete a promise with failure, which is the same idea as an exception, but cleaner.

  3. Never use the == operator, always use ===. Double-equals is broken in some very basic ways: if you need the type conversion, do it yourself then keep using ===.

  4. Alternatively, just use TypeScript or Flow.


#8

Yesterday I got into another confusing thing: one of the challenges (‘stand in line’) seems to set this as the actual solution:
var removed = arr.shift();

but my solution was this:
var removed = arr.shift(ITEM);

when googling arguments, it seems that javascript doesn’t care quite often if you actually pass an argument or not, so confusing!!!


#9

It’s not confusing when you understand what shift() or pop() does. Shift will return the first item (index 0) from an array, so it doesn’t need an argument. On the other hand, Pop will return the last index from an array. For anything else you’d use slice() with proper arguments.


#10

I don’t understand your explanation. Do you mean to say that shift() only returns the content of an item and that pop() will only return the array’s position?
And could you give axample of what you mean that ‘anything else’ might be in regards to slice()?


#11

Sure, here are some examples :

Array.shift

var myArray = ["cat","dog","parrot","fish"];

var onlyFirst = myArray.shift(); // This will return the first index only, so onlyFirst ="cat"
//notice that shift() transforms the target, so after this, myArray =  ["dog","parrot","fish"];

Array.pop

var myArray = ["cat","dog","parrot","fish"];

var lastOnly = myArray.pop(); // This will return the last index only, so lastOnly ="fish"
//notice that pop() transforms the target, so after this, myArray =  ["cat","dog","parrot"];

Array.slice

var myArray = ["cat","dog","parrot","fish"];

var newArray= myArray.slice(0,2); // This will return ["cat","dog"] slice is not inclusive, so althougth I specified 0,2, I don't get "parrot" returned.
//notice that slice() doesn't transforms the target, so after this, myArray still is =  ["cat","dog","parrot","fish"];

#12

Apart from the excellent explanations above, initializing variables when you declare them is also about setting expectations for runtime.
If you declare a variable without immediately giving it any value, and you only use it sometime later, the risk that the value is undefined or something else that is completely unexpected, is just too great. Check Stackoverflow for errors with “is undefined” and you’ll find thousands of people who have no idea why some variable is undefined or null.
If you set it to a base value, like 0 in the case of a number, that’s far easier to test for, and to track down.

Like @bnoden says: It tells both you and the rest of your code, that this is a number and nothing else.

P.S.:
It’s easy to make such mistakes even after initialization,as variables get re-assigned, the more complex your code gets. For example you will inevitably run into errors like “cannot read property [blabla] of undefined”. This is when you tried to use an object property somewhere, when the object itself was never created, say, because a prior API call didn’t return that object as expected…
Fun :wink:


#13

it’s actually under “find the length of a string” challenge


#14
var firstName = "Ada";
var firstNameLength = firstName.length;