I'm confused! :(((


#1

There is something I don’t really get in programming languages… for example… in Python why do we use .lower() and .upper() to change the text and make it all capital or small, why not just doing that in the string itself… I mean instead of London.upper() why not “LONDON” in the first place???

also, in JavaScript and many other languages, they use " + " to separate words, why cannot we simply include a space in our strings.

Variables also confuse me, for example: var myName = "Codey"
var myAge = "19"
console.log("myName " + “myAge”) why cant we just print “Codey 19” string instead…

Thanks in advance!


#2

Maybe someone could correct me if I’m wrong but this has to do with making your code dynamic suppose we wanted codey’s age to update on a website when he turns 20 we cant go in manually and enter his new age. This will take ages suppose we have codey and 1000 other people ages to change.
The ‘+’ which is used to join strings of data this also helps with dynamic content. Suppose we have a login and registration system each time we want someone to log in and see there name and so it logs the user in and writes "Hello " + personsName. And each person will require there name to show when logging in.

This is my first answer so forgive me if you find be puzzling you more.


#3

Every language has its differences. If they were all the same, there wouldn’t be different languages would there? I strongly suggest you read up the basics of JavaScript by starting here. It’ll show you some nice basics of the language, including the reasons why concatenation is used with the + operator.

For your London example, you simply assign your string to a variable, and use that variable with the toLowerCase() and toUpperCase() methods like this:

var x = "london";
x.toUpperCase();  // Result:  LONDON

pretty easy


#4

Great first answer @ChrisKissoon! Better than mine.


#5

The others have given you good advice. I just want to comment on the question related to “+”. The + operator is used to combine strings and not specifcally to put space in strings. However, if you have multiple strings as the code below has, you could use the + operator with a " " string to put a space between each string.

var str1 = "Hello";
var str2 = "Mr."
var str3 = "Jones"
var combined = str1 + " " + str2 + " " + str3;
console.log(combined); // Result: "Hello Mr. Jones"

#6

I’m not quite sure how to articulate this, but I’ll try,

Variables have many many uses. Some basics points are that variables are called variables because they can change. Lets say you have a calculator app with a variable called runningTotal. every time something is added or subtracted etc. runningTotal changes. This is one reason why you cant just manually type things in every time, in programming there’s automation changing your variables.

variables are also used when you dont know what the value will be at all, like for user input. in your example you yourself could usually type “LONDON” but if its user input that sets the variable, the user could type “LonDon” or any other variation, and if you need that to be all capital no matter what at the end, you have a function that can make sure of that.

‘+’ isn’t to separate words. When you use + between 2 strings (or 2 variables representing strings) you get the 2 strings combined into 1 string. If you use it between 2 numbers you get the two numbers added together.

hope that helps,
theres already been a couple decent responses it seems, by the time I finished typing.

good luck


#7

One of the reasons for using string manipulation like that is because people are not robots; they enter data in different ways and accidentally hit the shift or caps lock keys. If you need to process and return user input it is better that it is standardised in some way before storing or returning it.

Take your London, london, LonDon, LONDon or LONDON user input example as a case in point. If London was the correct answer to a question, LonDon would be incorrect, so you need a way to standardise the input.

The + in JavaScript CONCATENATES strings (displays them one after the other), it does not separate them, and never adds any spaces between them. If spaces are required, they need to be input manually, even if the string is stored in a variable.


#8

All of your answers were very helpful and clear, now I understand things better than before, thank you all for your helpful replies , much respect brothers and sisters!