Use variable value as JS parameter

Asked

Viewed 32 times

1

I was doing some tests to train JS with HTML. Then I made a newsletter system, where a newsletter Object has the notes of some subjects, and by a select chose the subject and which bimonth wanted:

var boletim = {matematica: [5.5, 10, 10, undefined], fisica: [5.1, 9.5, 10, undefined], ingles: [10, 10, 8.9, 10]}

function nota() {
    let res = document.querySelector("#res")
    let bim = Number(document.querySelector("#bim").value)
    let materia = document.querySelector("#materia").value
    res.innerHTML = `A nota no ${bim}º bimestre foi ${boletim.materia[bim - 1]}` 
}

The materia variable has the value of the option chosen by the user, I wanted to use this value to take an element of the bulletin object, how to solve this?

  • William, please put your HTML too.

  • try to access the bulletin object this way bulletin[materia][Bim-1]

  • Thanks Gabriel, it worked. Thanks for trying too, Daniel Mendes

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.