Help with Cuboid problem in objects. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Help with Cuboid problem in objects.

Hi, I think this should be pretty simple but I can't figure this out. I have just studied functions and now this is the first problem about objects. Is there a method that will help with this calculation which i supposed to use? I have tried to implement a function as learnt in the last module but it's not working. I cant tell what I've done wrong. In code pen it is saying width is not defined. https://codepen.io/Leanne251/pen/KKaMqER function main(){ var cuboid = { length: 25, width: 50, height: 200 } }; //your code goes here function volume (length, width, height){ let c = length*width*height; return c; }; console.log(volume (length, width, height));

28th Mar 2021, 12:15 PM
Leanne Smith
Leanne Smith - avatar
10 Answers
+ 5
Thanks!! Getting head around things actually work is tough! I feel like the tutorials dont always teach you everything you meed to answer the questions either 😫
28th Mar 2021, 4:23 PM
Leanne Smith
Leanne Smith - avatar
+ 5
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int length = Convert.ToInt32(Console.ReadLine()); int width = Convert.ToInt32(Console.ReadLine()); int height = Convert.ToInt32(Console.ReadLine()); Cuboid cuboid = new Cuboid(length, width, height); Console.WriteLine("Volume: " + cuboid.Volume()); Console.WriteLine("Perimeter: " + cuboid.Perimeter()); } } struct Cuboid { public int length; public int width; public int height; public Cuboid(int length, int width, int height) { this.length = length; this.width = width; this.height = height; } public int Volume() { return length * width * height; } public int Perimeter() { return 4 * (length + width + height); } } }
21st May 2022, 1:30 PM
Gelbert Millones
+ 2
console.log(volume (cuboid.length, cuboid.width, cuboid.height)); edit: as the properties are stored as an object, you have to point to the properties through the variable where they are stored.
28th Mar 2021, 12:23 PM
Kashyap Kumar
Kashyap Kumar - avatar
+ 1
Thank you for your reply, thats really helpful! So, just to confirm an object is a variable with a sort of list in it. The things in the list are called properties. Always denoted a:b. Because this is an object to draw upon the properties in it i have to us a.b . So i changed the function to be Function volume( length, width, height){ Let c = cuboid.length*cuboid.width* cuboid.height; return c; } Console.log(volume(cuboard.length, cuboid.width,cuboid.height)); WHY do you NOT have to put cuboid.length in the function when it is declared?? How do i know when i need to put cuboid. And when i can just use the property name?? Thanks!
28th Mar 2021, 1:03 PM
Leanne Smith
Leanne Smith - avatar
+ 1
Leanne Smith inside the function volume, the parameters work as variable names. Whenever you call the function you need to provide the actual values inside the parenthesis. function a(v){ // v is local variable here } When you call the function a(cuboid.length); Here all the v inside the function will be replaced by cuboid.length
28th Mar 2021, 5:37 PM
Kashyap Kumar
Kashyap Kumar - avatar
+ 1
var cuboid = { length: 25, width: 50, height: 200 }; //your code goes here var volume = cuboid.length * cuboid.width * cuboid.height; console.log(volume)
6th Sep 2022, 10:34 AM
Alexander OKUNRINKOYA
+ 1
Var volume =cuboid. Length *cuboid. Width *cuboid. Height * Console. log(volume)
7th Nov 2023, 8:06 AM
Amitabha Gupta
0
Leanne Smith yes, an object is a data type which has a list of keys and values, as you say. It's not a variable by default, but without assigning it to a variable, we can't refer to it. That's why we assign things to variables and then use the variable names (e.g. var cuboid, so that we can say cuboid.someProperty). For your question about function declaration, look into the difference between declaration (defining stage) and invocation (calling stage): Basically, when you declare a function, those things that go in brackets are parameters, and they can be called anything because they're kind of like temporary variable names which can only be referred to within the function. This is different from when you call the function. At that stage, you have to pass in actual values which are called arguments. You know when you have to put cuboid.length when you need to provide an actual value (argument) to a function. I.e. when you call it.
28th Mar 2021, 2:27 PM
CamelBeatsSnake
CamelBeatsSnake - avatar
0
I totally agree with you about the tutorials, and that it's tough to get these concepts right when you're starting out. Hang in there though. Spend time playing around with the code. Changing smalls bits at a time and then logging it out. Don't be shy to break things, Google, and ask questions. The fundamentals will definitely get easier if you keep learning and practising.
28th Mar 2021, 4:45 PM
CamelBeatsSnake
CamelBeatsSnake - avatar
0
Hey, i found a different way to solve this. var cuboid = { length: 25, width: 50, height: 200, ope: function () { return this.length*this.width*this.height } }; //your code goes here console.log(cuboid.ope());
11th Jan 2023, 8:01 PM
Juan Rodriguez