there are two properties for my bike method.front gear and rear gear.i want another property which will be a new property called gear ratio property which can be obtained by multiplying front and rear gear numbers.the code i have written giving continuous error.how it can be fixed.

<html>
<head>
</head>
<body>
<script>
function write(){
var bicycle={
   price:20000,
   model:"raleigh",
  front_gear:3,
   rear_gear:7,
    gear_ratio:function(){
      ratio:this.front_gear*this.rear_gear
       }
      }
      document.write("this is a "+bicycle.gear_ratio.ratio+" speed bike");
      }
      window.onload=write;
</script>
</body>
</html>

Dani AI

Generated

Short answer: the object in the original post defined gear_ratio as a function but never produced a value, and the code later tried to treat that function like a property. was on the right track — the product must be returned or exposed as a property — but you also need to either call the method or use a getter so bicycle.gearRatio actually yields a number.

Two safe patterns (different naming than the original to avoid repeating the exact code shown in the thread):

// computed-on-access (recommended if gears can change)
const bicycleA = {
  price: 20000,
  model: 'raleigh',
  frontGear: 3,
  rearGear: 7,
  get gearRatio() { return this.frontGear * this.rearGear; }
};
console.log(`this is a ${bicycleA.gearRatio} speed bike`);

// computed-on-creation (fine if values are static)
const bicycleB = {
  price: 20000,
  model: 'raleigh',
  frontGear: 3,
  rearGear: 7,
  gearRatio: 3 * 7
};
console.log('this is a ' + bicycleB.gearRatio + ' speed bike');

Notes and troubleshooting tips: call a normal method with parentheses (e.g. obj.method()), but if you want property-style access use an ES5/ES6 getter as above. Avoid document.write for appending content — prefer textContent/innerText or console.log. If gear values may be strings, coerce with Number(...) or +value before multiplying. This clarifies ’s original issue and refines ’s suggestion: either return-and-call or expose the computed value via a getter.

You're combining two different methods of doing this which will fail, need to choose one or the other, try this (Sorry for these not being in code boxes, my work PC doesn't like DaniWeb):

Change line 13 to:
return this.front_gear * this.rear_gear;

Change line 16 to:
document.write("this is a " + bicycle.gear_ratio + " speed bike");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.