JavaScript Question | SoloLearn: Learn to code for FREE!

0

JavaScript Question

i have been given an object, with length width and height, asked to finish a program to calculate the cube. i keep trying and the attempts are incorrect but i dont know why so i cant learn from my mistakes. var x = cuboid.length var y = cuboid.width var z = cuboid.height function vol = (x*y*z); return(vol);

11/11/2020 6:57:04 PM

Matthew Marchand

19 Answers

New Answer

+3

Just a guess, You need to create an object to use its methods unless it's a static then you can just call it using the class name.

+3

The declaration of "vol" is wrong, did you mean to define a function or a variable? For a function, you could write function vol() { return x * y * z; } You also need to display the result using console.log(). Furthermore, I think you need to wrap the code meant to execute at runtime, such as input and output, in a main() function, because you are writing NodeJS, not exactly pure JS: function main() { // input // calculations // output }

+3

Yeah, I looked at the code coach and solved it before giving my answer. You can use a method, but you don't have to. What is necessary is for the correct output to appear on the screen. How you achieve this is up to you. Regarding main(): The function encapsulates everything that is supposed to happen at runtime, i.e. when the program is running. The skeleton above is just an example of what could happen inside, not a fixed structure you always have to follow. For example, input is given at runtime. This challenge doesn't have any, so you don't need to worry about it right now. If you need to call other functions for e.g. calculations, you'd do it here. And lastly, you need to put the output in main(), i.e. all calls to console.log().

+3

I think you should follow the syntax explained by shadow

+3

Matthew Marchand No problem, try taking it one step at a time. First, focus on writing a piece of code that yields the result of length * width * height for the given cuboid. Maybe a function that returns this, or you could store it in a variable. Next, output the result with a call to console.log(). Finally, wrap the call to console.log() in a function called main(). Try using the Code Playground instead of solving it directly in the code coach. You can try around more and will get better feedback. If you still fail the test case, post the updated code here and I'll take a look. If you want to, I can also give you a number of different solutions, in case you want to review them.

+3

Nice, good job! As I said earlier, I think main() is necessary because to be able to run JS outside the browser, they make you write NodeJS, which differs a little from vanilla JS and seems to require that entry function, similar to languages like C. I'm not sure if something can be done about that, but you could send a mail or use the in-app feedback tool to send your suggestion to SL.

+2

Mirielle It's one of the new coach code problems inside the lessons. You can find it here, in the third lesson. It seems to be PRO though: https://www.sololearn.com/learn/JavaScript/1151/ Basically, you are given an object containing three fields that represent a cuboid, and you are supposed to output its volume.

+2

Shadow I solved it finally thanks to your guidance (and a couple others with that function main() trick really seems like a blind spot in the code coach challenges that should be fixed somehow

+2

Matthew Marchand avoid using var. Better use const and let to declare variables const cuboid = { length: x width: y height:z }; or wrap all in an IIFE 'use strict'; (funcion (){ //all code goes here })();

+1

its a code coach about objects and methods are very briefly mentioned. So i gathered they wanted a method because the object was supplied... var cuboid = { length: x width: y height:z }; im working under the assumption that the method goes here followed by console.log()

+1

Isn't there something wrong with the cuboid object, mathematically? I mean, having the keyword `length` to specify the x value when length could just be the measure of the spatial dimension x, y and z. X value has a length Y value has a length Z value has a length Who made that object by having length specifially to represent x? the proper definition could be, width, height and depth tho

+1

Also, the question isn't clear. I'll suggest you add a link to the actual problem if it's accessible for free users. "A program to calculate the cube" You can't calculate a cube because it's a shape, you can only calculate some properties of the cube. And you only provided a single vertex data from your cube

+1

i will follow those steps. I wish I would have thought about using code playground earlier. That seems sensible. Shadow Mirielle thanks for responding I appreciate it .

+1

I can’t be the only newbie who has run into the problem

+1

U got the function definition wrong

0

want to make sure im reading your answer correctly function main(){ object (input) method (calcultions) console.log (output) };

0

and thank you, i really appreciate you taking the time

0

yeah...im still not getting it sigh

0

function main(a,b,c){ var cube = { height: a, length: b, width: c, vol: function() { return this.height* this.length* this.width } }; var x=cube.vol(); console.log (x) } main(20,10,10); hope this solves;