+ 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);
28 Respostas
+ 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)
}
+ 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
}
+ 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
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.
+ 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
+ 3
var l=cuboid.length;
var w=cuboid.width;
var h=cuboid.height;
var x=l*w*h;
{
console.log(x)
}
+ 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.
+ 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.
+ 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
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
+ 1
I canāt be the only newbie who has run into the problem
+ 1
U got the function definition wrong
+ 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)
}
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;
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);