Home>

https://codepen.io/Tester2004-07/pen/MWwvoja

let d15= [0, 2, 5, -4, 6, 22, -9, -12, 8, 12, 13, 78];
function f15() {
    let inp= +document.querySelector('.i-15').value;
    if( d15.indexOf(inp != -1) ) {
        console log(1);
    }
    else {
        d15.push(inp);
    }
}
document.querySelector('.b-15').onclick= f15;

The parenthesis is not there in the condition. Well, in general, it's easier to use includes instead of indexOf.

Yaant2022-02-12 11:59:37
  • Answer # 1
    let d15= [0, 2, 5, -4, 6, 22, -9, -12, 8, 12, 13, 78];
    function f15() {
    let inp= +document.querySelector('.i-15').value;
            if (d15.indexOf(inp)== -1) {
                d15.push(inp);
            }
        showArr('.out-15', d15);
    }
    document.querySelector('.b-15').onclick= f15;
    

    As it stands, your answer is not clear. Please click edit below the post to add more details to help others understand how he answers the question asked. You can find more information on how to write good answers in the Help.

    Дух сообщества2022-02-12 00:22:07
  • Answer # 2
    let d15= [0, 2, 5, -4, 6, 22, -9, -12, 8, 12, 13, 78];
        function f15() {
            let inp= document.querySelector('.i-15').value;
            if(d15.indexOf(+inp) != -1){
                console. log('There is an element in the array');
            }
            else{
                d15.push(+inp);
                console log(d15);
            }
        }
        document.querySelector('.b-15').onclick= f15;
    <input class="i-15"><button class="b-15">Tyk</button>