简体   繁体   中英

find words more than x words (print how many words inputted)

how to find words more than x words.

i have states.js with code like this

exports.states= [
    {
        "id": "1101",
        "states_id": "11",
        "name": "Kentucky",
        "alt_name": "Kentucky",
        "latitude": 2.61667,
        "longitude": 96.08333
    },
    {
        "id": "1102",
        "state_id": "13",
        "name": "Ohio City",
        "alt_name": "Ohio City",
        "latitude": 2.41667,
        "longitude": 97.91667
    },
    {
        "id": "1103",
        "states_id": "12",
        "name": "West Los Angeles",
        "alt_name": "West Los Angeles",
        "latitude": 3.16667,
        "longitude": 97.41667
    },

and i have index.js code like this

function findCityMoreThanNWords(n) {

    for(let state of states) {
        if(state.name == stateName) {
            statesId = state.id;
            break;
        }
    }

my question is, if i put 1 the console will print name with 1 word

stateName: [Kentucky,etc,etc]

if i put 2 the console will print print name with 2 words like this

stateName: [Ohio City,etc City,etc City]

thank you in advance. any help or advise is appreciate it

State name with 1 word has 0 space " " symbol, with 2 words has 1 space sympol. You can try something like this:

function findCityMoreThanNWords(n) {
let result = []
for(let state of states) {
    if(state.name.split(" ").length == n) {
        result.push(state.name);
    }
}
return result;}

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