-4

I am just at javascript level 1 and would like to get your help in my case.

I create an array with the array name "buildings". But when I run code, it says "TypeError: buildings is not a function". I tried to replace "buildings" with "buildingsTypes" and other names like "housing" or "building" or even "x" but it does not work.

May you point out what is wrong with it? and how should I correct it? Thank you.

const buildings = ["Apartment", "Studio", "Condo"];
console.log(buildings);

buildings(0) = ["Villa"];
console.log(buildings);

buildings.push("Hotel");
console.log(buildings);
  • 2
    Array access is with square brackets, not parens. Just like when you declared and initialized the array. – Dave Newton Aug 14 '21 at 13:51
  • . The error you get is pretty clear ' buildings ' is not a function . You yourself are saying that buildings is an array. So how can it be a function ? `name()` is a function invocation. `buildings` is an array. That's why it says ' buildings ' is not a function. Getting a value at a specific index of an array is made by using square brackets `arrayName[index]` . Debugging is 50% ( at least ) of the job. Learn how to read and understand errors ( which in most cases are pretty clear ) and how to find the solutions by yourself ( not by asking on SO ) – Mihai T Aug 14 '21 at 14:09

2 Answers2

1

don't use buildings(0) but use buildings[0] because the buildings is not a function but an array. try the code below

const buildings = ["Apartment", "Studio", "Condo"]; 
console.log(buildings);

buildings[0] = "Villa";
console.log(buildings);

buildings.push("Hotel");
console.log(buildings);
  • 2
    Why use `[]` and not `()` ? Please include an explanation to help the OP understand why your solution works. – Mihai T Aug 14 '21 at 14:11
0

you should add only avalue and not an array.
And you dont write the variable correct (there is an s missing)

buildings[0] = "Villa";

also you should check the meaning of using a const for an array:
What do we mean by defining a const array in js?

A.Santos
  • 34
  • 3
  • 1
    What does ' const ' declaration have to do with the error the OP is getting ? – Mihai T Aug 14 '21 at 14:10
  • @Mihai T you right it has nothing to do. He only had mentioned that he is on level 1 so i thought it might be helpful. – A.Santos Aug 14 '21 at 14:23