+ 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));
10 Respostas
+ 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 đ«
+ 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);
}
}
}
+ 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.
+ 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!
+ 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
+ 1
var cuboid = {
length: 25,
width: 50,
height: 200
};
//your code goes here
var volume = cuboid.length * cuboid.width * cuboid.height;
console.log(volume)
+ 1
Var volume =cuboid. Length *cuboid. Width *cuboid. Height *
Console. log(volume)
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.
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.
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());