Scope of variables in JS/nodejs

1823 views javascript
-3

(And Guys I am new to Javascript(ES6), so please be kind) .content and noiseWords are the arrays which consist of strings. My aim is to remove noiseWords from Content. Both content and noiseWords are not global variables. So, can I access noiseWords in the word()? Is the line 4 correct? If not then how can I access noiseWord inside word()? I have mentioned "line 4" in the code.

word(content){
// this function remove noiseWords from the Content 
//and then return the content

noiseWords.split(/\s+/).map((w) => 
normalize(w));//line4    

 }

addNoiseWords(noiseWords){
//this function consists noisewords
}

answered question

Welcome to stackoverflow! Please take a look at How to Ask a Good Question. In this case, providing the entire code fragment you are trying, with an example of the input data and your desired output would help people provide you with a better answer.

1 Answer

10

Can I access noiseWords in the word()?

No

Is the line 4 correct? If not then how can I access noiseWord inside word()? I have mentioned "line 4" in the code.

You need to pass that array as parameter in function word(), for example:

function word(content, noiseWords){...}

or pass an object with those values:

function word(obj){
    var {content, noiseWords} = obj;
    .
    .
    .
}

or pass an array with those values:

function word(array){
    var [content, noiseWords] = array;
    .
    .
    .
}

posted this

Have an answer?

JD

Please login first before posting an answer.