+ 2

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);

11th Nov 2020, 6:57 PM
Matthew Marchand
Matthew Marchand - avatar
28 Answers
+ 7
var cuboid = { length: 25, width: 50, height: 200 }; //your code goes var l=cuboid.length; var w=cuboid.width; var h=cuboid.height; var x=l*w*h; { console.log(x) }
10th May 2021, 12:26 PM
Anvarbek Uktamov
+ 4
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 }
11th Nov 2020, 8:07 PM
Shadow
Shadow - avatar
+ 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().
11th Nov 2020, 8:37 PM
Shadow
Shadow - avatar
+ 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.
11th Nov 2020, 9:25 PM
Shadow
Shadow - avatar
+ 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.
12th Nov 2020, 7:00 PM
Shadow
Shadow - avatar
+ 3
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 })(); https://code.sololearn.com/cTaY63vkeNOK/?ref=app https://code.sololearn.com/WuiDXTPYV46Z/?ref=app
13th Nov 2020, 1:48 PM
David Ordás
David Ordás - avatar
+ 3
var l=cuboid.length; var w=cuboid.width; var h=cuboid.height; var x=l*w*h; { console.log(x) }
9th Jun 2022, 4:20 AM
Zeei Developer
Zeei Developer - avatar
+ 2
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.
11th Nov 2020, 7:10 PM
D_Stark
D_Stark - avatar
+ 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.
11th Nov 2020, 9:10 PM
Shadow
Shadow - avatar
+ 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()
11th Nov 2020, 8:24 PM
Matthew Marchand
Matthew Marchand - avatar
+ 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 .
11th Nov 2020, 11:15 PM
Matthew Marchand
Matthew Marchand - avatar
+ 1
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
12th Nov 2020, 3:39 PM
Matthew Marchand
Matthew Marchand - avatar
+ 1
I can’t be the only newbie who has run into the problem
12th Nov 2020, 7:35 PM
Matthew Marchand
Matthew Marchand - avatar
+ 1
U got the function definition wrong
13th Nov 2020, 12:26 PM
eskayML 💣
eskayML 💣 - avatar
+ 1
var cuboid = { length: 25, width: 50, height: 200 }; //your code goes var len=cuboid.length; var wid=cuboid.width; var ht=cuboid.height; var x=len*wid*ht; { console.log(x) }
13th Jun 2021, 11:02 AM
Chinmay Anand
Chinmay Anand - avatar
0
want to make sure im reading your answer correctly function main(){ object (input) method (calcultions) console.log (output) };
11th Nov 2020, 8:26 PM
Matthew Marchand
Matthew Marchand - avatar
0
and thank you, i really appreciate you taking the time
11th Nov 2020, 8:27 PM
Matthew Marchand
Matthew Marchand - avatar
0
yeah...im still not getting it sigh
11th Nov 2020, 8:48 PM
Matthew Marchand
Matthew Marchand - avatar
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;
13th Apr 2021, 4:28 AM
ajay T
ajay T - avatar
0
what about this var cuboid = { length: 25, width: 50, height: 200 }; //your code goes here var vol1 = cuboid.length; var vol2 = cuboid.width; var vol3 = cuboid.height; var vol4 = vol1*vol2*vol3; function volume (vol4){ return(vol4) }; console.log(vol4);
15th Jun 2021, 6:56 AM
Issah breezy
Issah breezy - avatar