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 5 years ago.
Improve this question
screenshot
Can someone verify where I went wrong?
addTodos parameter todos will override the global todos so just rename it to something else. see below example
var todos = ['1','2','3'];
function display() {
console.log(todos)
}
function addTodos(todo) {
todos.push(todo)
display()
}
addTodos('4')
The problem you are having with your code is, you created a global variable var todos and at the same time in your addTodos function you added an argument todos, the JavaScript is accessing your function argument not the global variable,
Now change the name of the global variable, to something like var myTodos to avoid conflicting
Hope this helps 😉
todos is a string not an array where error is occurring.
string type doesn't have push method. Also, local todos is found first in the local scope so it masks the array version.
Not sure whether you have realised, you are sending in an argument called todos. You are effectively overwriting the todos array that has been set previously. Replacing it with a new variable name would fix the problem
function addTodos(newTodo) {
todos.push(newTodo);
}
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
Does anyone have any idea how to stop an interval that is situated inside of a function once it's done doing its thing?
Here is what I mean:
function renderMessage(message) {
const renderInterval = setInterval(() => {
characterIndex++;
dealerMessage.innerHTML = `
${messages[message].slice(0, characterIndex)}
`;
if (characterIndex === messages[message].length) {
clearInterval(renderInterval)
}
}, 100);
}
As you can see, I'm trying to render out a message using this function. It does its job fine, but if I don't stop it, subsequent messages keep overriding themselves...
I've tested the if check and it is actually functioning inside the function, yet for some reason the clearInterval doesn't work.
Is there any way I can fix this, or do you recommend me to start from scratch?
Note: this method would be very handy for me, so, if possible, I would like to keep it.
I think your 'if' statement of clearInterval should be
if (characterIndex===message[message.length]){}
Also, I cannot see any initialization of the characterIndex variable. Please do inform if this worked or not.
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 5 months ago.
Improve this question
Good day.
I was testing stuff in Javascript with Proxies.
Currently I have this proxy
My question was, how do I iterate over this? I've tried several methods, including object.keys and forEach, which yielded nothing.
Thanks in advance
You need to specify an ownKeys method in the handler you're using to create the proxy or you won't be able to enumerate the keys of the proxy object.
const obj = { test: 'a' };
const handler1 = {
ownKeys(target) {
return Reflect.ownKeys(target);
}
};
const proxy1 = new Proxy(obj, handler1);
console.log(Object.keys(proxy1)) // ['test']
Edit
Actually, you can use Reflect.ownKeys directly also, but you'll want to make sure the behavior is what you expect. For example, it might return length as a key as well.
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.
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 7 years ago.
Improve this question
So I asked a similar question before... I wanted to know how to argue for the css property in a document.element.style.property=value. The solution was simple, and almost made sense --but clearly I didn't understand it entirely or I'd know trying the same solution for .element doesn't work.
Here is my code:
function appendElement(handle){
element=document[handle]('div');
document.body.appendChild(element);
}
This way I could choose to create a new element or shift an existing one based on id or class or index appearance or whatever. Of course even without knowing the correct way to do this, the code I have above looks wrong to me, but it's the best I can do without some assistance.
EDIT: Test case
/* The core instructions */
element=document.createElement('div');
document.body.appendChild(element);
/* the choosy version */
function appendElement(handle){
element=document[handle]('div');
document.body.appendChild(element);
}
appendElement(createElement);
element.innerHTML="third text";
/* SHOULD move the 'text' div under the 'third text' div*/
appendElement(getElementById('first'));
<div id="first">text</div>
<div>second text</div>
Edit
In this line appendElement(getElementById('first'));you are not passing a function as you want, you are passing the result of call undefined with the param 'first', because it can't find a function in that context or a global function called getElementById (so it will be undefined), furthermore your are trying to execute undefined passing it a string... this is going to raise an error, and in the case in which function existed (i.e you pass document.getElementById('first')), then you will be passing the returned value of executing that function instead of the function.
If you want to pass a function you should pass a function, thats is appendElement(document.getElementById), without calling it with an argument, but I think you are going to need to pass a selector to that function to accomplish what you are trying to do. So
the code will be something like this:
function appendElement(handle, selector){
element=handle.call(document, selector);
document.body.appendChild(element);
}
appendElement(document.getElementById, 'first');
<div id="first">text</div>
<div>second text</div>
call allows you to execute a function as a method and specify which object will be the receptor of that calling. Here is more info
I didn't understand your need.
But, if handle is equals to 'createElement', which is a property of document, your code will run.