Ad
  • Custom User Avatar

    if you use function expression for that method you have to remember about hoisting

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    I created two solutions, both work and give correct results (I tested in browser console). But for some reason none of my solutions passed the tests. The first solution got "SyntaxError: Unexpected token", the second "Identifier 'Cat' has already been declared" - but it's not possible! The code works fine in the console. Here's my code, I wonder if anyone can spot the problem:

    class Cat {
    	static cumulWeight = 0; 
    	static catCount = 0;
    	   	constructor(name, weight) {
    		   	if (typeof name !== 'string') {
    			   throw 'Error: Input name!';
    			   }
    		   	if (typeof weight !== 'number') {
    			   throw 'Error: Input weight!';
    			   }
    		   	let _name = name;
    		   	let _weight = weight; 
    		   	Cat.cumulWeight += _weight;
    		   	Cat.catCount++;
    			Object.defineProperty(this, "name", {
    				get: function() {
    					return _name;   
    				},
    			});
    			Object.defineProperty(this, "weight", {
    				get: function() {   
    					return _weight;
    				},
    				set: function(value) {
    					if (typeof value !== 'number') {
    						throw 'Error: Input weight!';
    					}
    					else {
    						Cat.cumulWeight += value - _weight;
    						_weight = value;
    					}
    				}
    			});
    	   }
    	static averageWeight() {
    		return Math.round(this.cumulWeight / this.catCount);
    	}
    }
    
    
    const Cat = (function () {
    	let cumulWeight = 0; 
    	let catCount = 0;
    	   	return function construct(name, weight) {
    		   	if (typeof name !== 'string') {
    			   throw 'Error: Input name!';
    			   }
    		   	if (typeof weight !== 'number') {
    			   throw 'Error: Input weight!';
    			   }
    		   	let _name = name;
    		   	let _weight = weight; 
    		   	cumulWeight += _weight;
    		   	catCount++;
    			Object.defineProperty(this, "name", {
    				get: function() {
    					return _name;   
    				},
    			});
    			Object.defineProperty(this, "weight", {
    				get: function() {   
    					return _weight;
    				},
    				set: function(value) {
    					if (typeof value !== 'number') {
    						throw 'Error: Input weight!';
    					}
    					else {
    						cumulWeight += value - _weight;
    						_weight = value;	
    					}
    				}
    			});
    			Cat.averageWeight = function() {
    				return Math.round(cumulWeight / catCount);
    			};
    	   }
    }());
    
  • Custom User Avatar

    It won't work with 'class' syntax for some reason.
    You have to use the approach shown here: https://www.youtube.com/watch?v=gdjfsDlkH7A

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Can someone tell me what the error is?

    Uses Object.defineProperty to get and set instance weight
    Value is not what was expected

  • Default User Avatar

    Add one more test - set felix.weight second time and check average. This will surely show some broken solutions.

  • Custom User Avatar

    Use Object.defineProperty to write custom setters and getters

    This might have been the (only solution?) norm when this kata was created, but now that JavaScript has a proper class syntax with getters and setters, forbidding the use of modern language features is ridiculous.

  • Custom User Avatar

    If anybody's interested:

    Test.it("should have an averageWeight method", function () {
      Test.assertEquals(typeof Cat.averageWeight, 'function');
    });
    
  • Custom User Avatar
  • Custom User Avatar

    Nobody's going to change it now.

  • Custom User Avatar

    SyntaxError: Identifier 'Cat' has already been declared

    Why is there a Cat hidden in preloaded?

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    I've imlement this method (all test are passed). What does this test check? method typeof?? If yes, my code also passed sample tests, which are check method typeof too

  • Custom User Avatar

    seems pretty clear, doesn't it? :o
    you have to define that averageWeight method, where appropriate for the current task.

  • Loading more items...