简体   繁体   中英

`my 'return' keyword seems not to be working in visual studio code while writing javascript..any help will be appriciated `

i have been writting javascript on visual studio code and the 'return keyword seems not to be working.the code is correct and should display sumOfCount on the console but instead it onlt displays 1..this is code for displaying the number of times an element appears in an array.

const numbers=[1,2,,2,3,4,5,1,];

const sumOfCount=countOccurence(numbers,2);
console.log (sumOfCount);

function countOccurence(array,searchElement){
    let count=0;
    for (let number of array)
        if (number===searchElement);
            count++;
    return count;
}

hi remove semicolon from end of if statement. please refer below code

const numbers=[1,2,,2,3,4,5,1,];
const sumOfCount=countOccurence(numbers,2);
console.log (sumOfCount);

function countOccurence(array,searchElement){
let count=0;
for (let number of array)
    if (number===searchElement)
        count++;
return count;
}

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