Uncaught ReferenceError: copiedElement is not defined?

  • 0
    Help me figure it out :(
    <button class="game-panel-item__copy" onClick="copyElement(${group.id})">
            Copy
        </button>


    const copyElement = (elementId, elementObj) => {
            if (elementObj){
                const copiedElement = elementObj
            }
            else{
                const copiedElement = selectedElements.filter(element => element.id === elementId)
            }
        
            let element;
        
            element = {
                ...copiedElement[0],
                id: generateElementId()
            }
        
            if (selectedElements === winds || selectedElements === areas) {
                element.params = copiedElement[0].params
            }
        
            if (selectedElements === groups) {
                copiedElement.elements.forEach((el)=>{
                    elements.elements.push(copyElement(el.id, el))
                })
        
            }
        
            if (elementObj) {
                if (elementObj.params) {
                    if ('forse' in el.params) {
                        winds.push(element)
                    } else if ('coup' in el.params) {
                        areas.push(element)
                    }
                } else {
                    winds.push(element)
                }
                return element
            } else {
                selectedElements.push(element)
                renderAll()
            }
        }
    JavaScript Anonymous, Jul 14, 2020

  • 1 Answers
  • 0
    dear, are you aware that variables / constants declared in blocks are invisible outside of them?

    let example = 3;
    if (example > 1) {
    alert("Yes!"); // Yes! - работает.
    }


    if (condition) {
    let example = 3;
    }
    if (example > 1) { // Uncaught ReferenceError: example is not defined
    alert("Yes!");
    }
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!