I could write this code that I share with you here bellow. Now I want to turn it into a function. What should I do? All the console.log that are written here pass perfectly. But as you see it in second part of the array “thisArea” you have the property “houses” being undefined. I wanted to push values in this array and to pop values from the array “houses” that is index zero of the parent array. When I use .push() or if I use .pop() at pressing the run button I’m told that it cannot pass because it is not a function. So how can I turn this complete set of variables into a function in JavaScript?
var thisArea = [
{
shops: "few",
houses: [
"The old ones",
"The small ones",
"The expensive ones",
"The clayish ones"
],
roads: 5
},
{
shops: "big",
houses: [],
roads: 10,
cars: 420
},
{
shops: "None",
houses: [
"Manchon",
"Chalet",
"Département shop"
],
roads: [],
cars: 75
}
]
thisArea[0].cars = 54;
var thePlace = thisArea[0];
var thatArea = thisArea[1];
var theHouse = thisArea[0].houses[3];
console.log(thePlace);
console.log(thatArea);
console.log(theHouse);
var theNewHouse = thisArea[1].houses[0];
console.log(theNewHouse);
It is not a challenge. I was watching this video Learn JavaScript - Full Course for Beginners - YouTube of freeCodeCamp on YouTube and from it I could write this code. Then, I wanted to pass forward to make it look different from what I saw in the video. Then this is my question : can variables that are made up of arrays be turned to a function? This is what I want.
But if you cannot make it become a function tell me how can I push values in the undefined arrays that are there and how I can pop out values from the other arrays there.
On this screenshot you can see that all the console.log pass correctly. Then there is no mistake in the set of the code. I just want to make it become a function.
Also, what exactly do you want the function to do?
What will be the inputs to the function?
What will be the desired output from the function (i.e. return value)?
By the questions here above you woke me up to a reality that I didn’t reflect on. Well, let me leave it to your appreciation. Make a function for example to tell how many houses are there. Or just to check the number of car in each array. Or to give the total of the houses in the parent array.
I wanted to push elements in the array that is empty and undefined at index 1 of the parent array. You see an array called houses there that is empty. Well, I want to fill it.
No I don’t understand it at all. I’m totally new in this field. I can learn by seeing what others do.
I am giving it to you right away. But can you just try to turn this object with all the arrays that are in it into one of the functions that i suggested got me to see how you do?
I was sure you would react this way. . But it is fine! I can do that. I love learning and I rarely fail to get what I’m looking for. So having some hints and guidance is what I often need.
With this you asked me to show you what I did to try to push elements in the empty array that I’ve there. Here is what I did and that was a gross failure.
var thisArea = [
{
shops: "few",
houses: [
"The old ones",
"The small ones",
"The expensive ones",
"The clayish ones"
],
roads: 5
},
{
shops: "big",
houses: [],
roads: 10,
cars: 420
},
{
shops: "None",
houses: [
"Manchon",
"Chalet",
"Département shop"
],
roads: [],
cars: 75
}
]
thisArea[0].cars = 54;
var thePlace = thisArea[0];
var thatArea = thisArea[1];
var theHouse = thisArea[0].houses[3];
console.log(thePlace);
console.log(thatArea);
console.log(theHouse);
var theNewHouse = thisArea[1].houses[0];
for (var e = 0; e < 5; e++) {
theNewHouse.push(e);
}
console.log(theNewHouse);
theNewHouse.push(4);
var theHouses = thisArea[0].houses[3].pop();
console.log(theHouses);
You can copy the code and past it in a code editor and you’ll see where I got stuck.
You are methodically right when you say that I’ve a lot to learn in coding methods. But I thank you for telling me that I cannot turn objects into functions. Yet, I’d be the happiest boy of this lost planet if you could tell me why.
As for learning let me tell you that i speak very fluently 4 languages. I know quite well two others. And I’m enrolled in learning others more right now. So I’m a neophyte in coding but I’m far from being empty headed. I Know a good amount of things and I love learning. Just put up with me for a little while. I could not get it from the written courses so I decided to turn to watching the videos and trying what was explained there for me to get roots on the matter. Well, I hope that you could get me.
I’m not randomly guessing at all, dear Randell. I use to seek, dig, ponder, search and examine deeply to get what I want. I do not know how many languages you speak. But if you have crossed the number of two you can get what I mean. The learning of a language, mostly when you do not live in the country that uses it daily, requires of you to be a real researcher. I attached a picture here to show you how far I could go in the curriculum of freeCodeCamp. I have a certification in web design that I understand quite well and that I got from freeCodeCamp. You can see how true I’m by just clicking here bellow .
So what do I try to tell you? Stop seeing me as a random guesser. I would never reach so far if I didn’t know how and where to seek. I use videos to practice what I previously learnt in the courses. Then when I still do not get it you should just take me from where I’m and lift me up.
In any case I thank you for your support. I was somehow helpful. I could learn for example that an object cannot be turned into a function in JavaScript. But you did not tell me if there is a way the push elements in an empty array that is in an object and how to pop out elements of an array that is in an object. Let me move ahead at my pace. I’ll will get there even faster than what is expected.
Hi Randell! I hope you got a good weekend. I spent mine dedicating time to share biblical thoughts with friends. Are you a believer? I’m a Christian. Well, I come back to you to show you that as I told you I often find what I’m going for. Here I the synthetic proof of my sayings.
Just run this code.
var thisArea = [
{
shops: "few",
houses: [
"The old ones",
"The small ones",
"The expensive ones",
"The clayish ones"
],
roads: 5
},
{
shops: "big",
houses: [],
roads: 10,
cars: 420
},
{
shops: "None",
houses: [
"Manchon",
"Chalet",
"Département shop"
],
roads: [],
cars: 75
}
]
thisArea[0].cars = 54;
var thePlace = thisArea[0];
var thatArea = thisArea[1];
var theHouse = thisArea[0].houses[3];
/*console.log(thePlace);
console.log(thatArea);
console.log(theHouse);*/
var theNewHouse = thisArea[1].houses;
theNewHouse[0] = ["a boy", 5];
console.log(theNewHouse);
console.log(thisArea[1]);
theNewHouse[1] = "a chalet";
console.log(theNewHouse);
console.log(thisArea[1]);
By running the code I give you here and that I struggled with last time, you will notice that I did not just push an element in the empty array “houses” as I wanted to do I pushed an array and a number. In German we say : “Es ist wirklich toll!” So never call me again random guesser.
Randell, I can see that you took time to read attentively all that I gave and to understand what I was aiming. It touches me. I can see that I reached at least one goal. I proved you that I’m not what you thought of me at first.
I looked at it again and I declare you right. You could see these points that I presented wrongly. But what I wanted was to push elements in an empty array and I can tell you that now I know to do it very well now. I can pass by nested arrays or by .push() or by .unshift(). But no matter what the method that would be asked to me I know how to push elements in an empty array.
The road is till long so I do not boast at all. I just wanted to show you that I am on my way to reach where I wanted to reach.