简体   繁体   中英

Function doesn't return the value

I have this function `

let n = (index) => {
    if(index < 0){
        index = 0;  
    }
    return index;}

that must run inside of this function,

prev.addEventListener('click', function() {  
    index--;
    n(index);
    console.log(index);
});

but for some reasons it wouldn't work. It looks like it sets the value of index, but actually it doesn't

You are forgetting to set your index to the value returned by the function.

prev.addEventListener('click', function() {  
    index--;
    index = n(index);
    console.log(index);
});

In here you have called the function n(index) but it's value isn't assigned anywhere, you have to assign it's value like below

let n = (index) => {
    if(index < 0){
        index = 0;  
    }
    return index;}

prev.addEventListener('click', function() {  
    index--;
    index = n(index); // This is where you have Assign it's Value
    console.log(index);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM