-2

i am a noob to javascript. I how have tried to access my url variable in another block but it either says Uncaught ReferenceError: value is not defined or i can't access it when i define it. thanks in advance. #i have also defined it in another scope.

const myJson = { "images":[
    {"scissors": {"url":"images/icon-scissors.svg"}},
    {"paper": {"url":"images/icon-paper.svg"}},
    {"rock": {"url":"images/icon-rock.svg"}}
]};
for(let key in e){
        value = e[key];
         url = value.url;   
    }
    if(randChoice.scissors){
        let url = value.url;
        console.log(url[0])     
    }

//the other place i defined it

const myJson = { "images":[
    {"scissors": {"url":"images/icon-scissors.svg"}},
    {"paper": {"url":"images/icon-paper.svg"}},
    {"rock": {"url":"images/icon-rock.svg"}}
]};
const selected = myJson.images.find((elem)=> {
    for(let key in elem){
     if(triggerType === key) {
         const value = elem[key]
          url =  value.url
        selectedChoiceImg.src = url
     }else{
        return
     }
    }
})

// json file

{ "images":[
    {"scissors": {"url":"images/icon-scissors.svg"}},
    {"paper": {"url":"images/icon-paper.svg"}},
    {"rock": {"url":"images/icon-rock.svg"}}
]}
jabaa
  • 5,844
  • 3
  • 9
  • 30

1 Answers1

-1

sorry but i think found the solution. i had to define the url in each function scope as null.

     const randChoiceImg = myJson.images.find((e)=> {
    let url;
    for (let key in e) {
        value = e[key]
        url = value.url 
    }
    if (randChoice.scissors){
        url = value.url
        console.log(url[0])     
    }
})

}

and the other function scope i also defined it to null.

const trigger = (e)=> {
const triggerType = e.currentTarget.classList[1]
let url = ''

const selected = myJson.images.find((elem)=> {
    for(let key in elem){
     if(triggerType === key) {
         const value = elem[key]
          url =  value.url
        selectedChoiceImg.src = url
     }else{
        return
     }
    }
})

 
triggerMain.classList.add('active')
choiceScreen.classList.add('active')
cpuChoice(triggerType)

}