Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I was taking a challenge and one of the questions seems like I got the right answer but it wouldn't pass. Need help understanding why it didn't.
Challenge: Add a method to the Person's prototype called "shoutName" that returns the person's name in all uppercase letters.
function Person(name) {
this.name = name;
this.shoutName = function() {
name.toUpperCase();
return '"' + name.toUpperCase()+'"'
}
}
/* Do not modify the code below this line */
const john = new Person('John');
console.log(john.shoutName(), '<-- should be "JOHN" ');
The question said to add a function to the constructor's prototype.
You didn't do that. You modified the constructor to dynamically add the function to the instance as the instance was created.
Person.prototype.shoutName = function () {
return this.name.toUpperCase();
}
Your function also wrapped the resulting value in quotes, which the question didn't ask you to do.
From your tiny picture, I noticed that your code was:
return '"' + name.toUpperCase() + '"';
Not sure why you added the quotes, just return this.name.toUpperCase(); and it should work fine. You should be referencing this object's property, rather than the input value of just name.
Also, having name.toUpperCase(); on a line by itself does nothing. Unnecessary calculations since that function returns a value that you're not assigning.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
Can i do this in one line without the intermitten variables.
const { state$, toggleState } = StateManagement.useToggleState$(initialState);
this.layoutState$ = state$;
this.toggleState = toggleState;
Something like this
{ state$: this.layoutState$, toggleState: this.toggleState } = StateManagement.useToggleState$(initialState);
When I wrap it in paranthesis like below, the IDE stops throwing errors, but they are not actually assigned.
({ state$: this.layoutState$, toggleState: this.toggleState } = StateManagement.useToggleState$(initialState));
Apparently my problem came from typescript not doing its thing when you assign a declared property in the constructor (Inferring its type). So, I just needed to manually write its type in the declaration.
toggleLayoutState: (key: keyof LayoutState) => void;
constructor() {
({ state$: this.layoutState$, toggleState: this.toggleLayoutState } = StateManagement.useToggleState$(initialState));
}
Normally it should understand the type, even if you don't explicitly state it. Something to fix by typescript contributors maybe, or I am missing something.
PS: Not deleting the question, since it would have helped me if it existed before. Mods can delete it If I am breaking any rules I guess.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
working on JS, What am I missing? Thank you
Modify the function below to greet only those with an even number of letters in their name
function helloYou(name)
numbers.filter (n => n % 2 =i= 1);{
}
/* Do not modify code below this line */
console.log(helloYou('Bob'), `<-- should return undefined`)
console.log(helloYou('Anna'), `<-- should return "Hello, Anna!"`)
To access the number of letters in the string, you can use the attribute .length.
Then to check if this number is even a modulus 2 should return 0, that's what we need to check. This condition goes in an if statement.
Finally, if this condition is met, return Hello concatenated with name.
Otherwise nothing is returned, so it's undefined (there is no need to explicitly write return undefined).
function helloYou(name) {
if (name.length % 2 === 0) {
return "Hello, " + name;
}
}
/* Do not modify code below this line */
console.log(helloYou('Bob'), `<-- should return undefined`)
console.log(helloYou('Anna'), `<-- should return "Hello, Anna!"`)
The variable numbers is actually undefined in this case.
Also, filter is not useful in this situation. Filter is mainly used to get the elements that match a condition from an array.
Your should use an if statement to check for even length. Better yet, you can use the ternary operator. Here is an example:
function helloYou(name) {
return name.length % 2 === 0 ? 'Hello, ' + name : undefined;
}
console.log(helloYou('Bob'));
console.log(helloYou('Anna'));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've got an object.
function Obj()
{
}
Obj.prototype.doSomething = function(thing)
{
this["do" + thing]();
}
Obj.prototype.doAlert = function()
{
alert("Alert!");
}
var obj = new Obj();
obj.doSomething("Alert");
This is just a shortened down version of my object, and is a lot bigger.
What I would like to do is that if you pass in 'Alert' it will run this.doAlert(); and if I pass in 'Homework' it will run this.doHomework();
Obviously, in this case, it is stupid to do it like this, but my final project is going to be completely different.
It works fine with window like this:
window["do" + thing]();
but I don't want it to be a global function, but to be part of obj.
Does anyone have an idea how I'd go about doing this?
Thanks in advance!
It turns out that when you get the function through this['functionName'], this is not bound to it.
This means you cannot use this.foo inside any function called like above.
To fix this, I used the following code:
this["do" + thing].bind(this)();
instead of this["do" + thing]();
JSFiddle: https://jsfiddle.net/auk1f8ua/
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am new to JavaScript and wanted to create a simple game of pong for practice.
I can easily draw the gaming area on the canvas but now there is some problem when I try to draw the bat.
I created a playerBat object with some properties.
var playerBat = {
batWidth:20,
batHeight:100,
x:10,
y:(height/2)-(batHeight/2),
spdY:10
};
Then in the function "drawPlayerBat" I simply drew a rectangle with these properties as parameters.
function drawPlayerBat() {
ctx.fillStyle = "white";
ctx.fillRect(playerBat.x, playerBat.y, playerBat.x+playerBat.batWidth, playerBat.y+playerBat.batHeight);
};
But its not working ! The console says "Unable to get property 'x' of undefined or null reference". Have I made any mistake in syntax or doing this is just not possible ?
Here is the entire code... http://codepen.io/anon/pen/YqgVog?editors=1010
Any help will be appreciated.
Thanks.
Easy fix:
There is really no reason to call
y:(height/2)-(batHeight/2),
since you are defining batHeight two lines above it. It won't behave dynamically - once you set it it won't change when batHeight changes, it will stay the same.
Knowing batHeight value you can simply say that
y:(height/2)-(50),
What answer you probably want:
If the question is about self-referencing in JavaScript, then the answer would be to set the values inside of a function inside of the object and initialize it after declaring it. For example:
var playerBat = {
batWidth: 20,
batHeight: 100,
x: 10,
spdY:10,
init: function(){
this.y = (height/2)-(this.batHeight/2);
return this;
}
}.init();
You need to remove batHeight. Do it like below.
var playerBat = {
batWidth:20,
batHeight:100,
x:10,
y:(height/2)-(100/2),
spdY:10
};
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I come across below statement in various JS code where first a function let's say "test" is declared thereafter used as mentioned below:
type: "test"
Please explain what does this statement mean where "test" is a function name.
Sample code below:
var MyFavoritesAjax = Class.create();
MyFavoritesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
ajaxFunction_getFavorites : function() {
var result = this.newItem("result");
result.setAttribute("message", "returning all favorites");
this._addFavorite("color", "blue");
this._addFavorite("beer", "lager");
this._addFavorite("pet", "dog");
},
_addFavorite : function(name, value) {
var favs = this.newItem("favorite");
favs.setAttribute("name", name);
favs.setAttribute("value", value);
},
type : "MyFavoritesAjax"
});
Now that you've shown the rest of the code:
type : "MyFavoritesAjax"
is a property declaration on an object being passed to Object.extendsObject(). It creates a property on that object named "type" and gives it a value of "MyFavoritesAjax".
This is similar to the other properties on that same object ajaxFunction_getFavorites and _addFavorite, though they have function references as their value instead of a string.
Given what you posted, you have a label statement (with the label type) including a simple expression statement consisting of the sole string literal "test". You could (and should) add a semicolon after it. However,
type: "text";
does not make much sense, it neither has any effects when executing nor does the label identify a loop.
Ah, your code example sheds some more light on this. As expected, you have an object literal that is passed to the Object.extendsObject function:
{
type : "MyFavoritesAjax"
}
Here, "type" is the property name and the "MyFavoritesAjax" string the property value. What this function does with it, and what it means in that contexts, should be documented with that function. I don't know it, it's not native.