-3

I am working on a task for the coding bootcamp I am enrolled on, this task is to have an array with 10 words and all with a boolean value, if the words contain 6 or more letters they should be pushed to a new array and the boolean should be converted as part of a callback function.

I have set all the values to false, and I am trying to switch them to true.

I have successfully pushed all the 6+ letter words into an array, but I cannot seem to convert the boolean value.

You can see in my code here what I have tried, I have made comments within the function.

Any pointers would be appreciated.

 let words = [
     {name:"Lion", sixOrMore: false},
     {name: "Jaguar", sixOrMore: false},
     {name: "Cheetah", sixOrMore: false},
     {name: "Tiger", sixOrMore: false},
     {name: "Leopard", sixOrMore: false},
     {name: "Bobcat", sixOrMore: false},
     {name: "Puma", sixOrMore: false},
     {name: "Ocelot", sixOrMore: false},
     {name: "Lynx", sixOrMore: false},
     {name: "Caracal", sixOrMore: false},
 ]

let sixLetterWords = []

let names = words.map(item => item.name);

const myFilterFunction = () => {

     for (i=0; i < names.length; i++){
        if (names[i].length >= 6) {
            sixLetterWords.push(words[i])
        }
    } 
} 

let sixLetterNames = sixLetterWords.map(item => item.name);

const trueVal = () => {
    for (j=0; j < sixLetterNames.length; j++){
        if (sixLetterNames[j].length >= 6) {
            //words[j] = JSON.parse(JSON.stringify(words).replaceAll("false","true"));
            //return words[j].forEach((item) => item.sixOrMore = true);
            //words[j].sixOrMore = true
        }
    }
}

myFilterFunction(words)
console.log(JSON.stringify(sixLetterWords))

trueVal(words)
console.log(JSON.stringify(words))
Laura
  • 9
  • 3
  • Do you have a specific question? [Why is "Can someone help me?" not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – jabaa Jan 20 '23 at 22:15
  • 1
    This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Jan 20 '23 at 22:16
  • `const result = words.filter(w => w.name.length >= 6).map(w => { w.sixOrMore = true; return w; });` – Parzh from Ukraine Jan 20 '23 at 22:17
  • Thank you all for your responses, they have helped me greatly, I apologise that my question was not very clear. – Laura Jan 20 '23 at 22:50

1 Answers1

0

There's no need for the names array. Just loop over words and do everything you want.

words.forEach(word => {
    if (word.name.length >= 6) {
        word.sixOrMore = true;
        sixLetterWords.push(word);
    }
});

You can also do it with filter(). While filter callback functions normally just test the value to determine whether it should be in the result, it can also modify the object while it's running.

let sixLetterWords = words.filter(word => 
    if (word.name.length >= 6) {
        word.sixOrMore = true;
        return true;
    }
    return false;
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you, I did end up using the names array because "words.name.length" was not working for me, I have noticed you have wrote "word.name.length", how are we able to use the object array without the correct spelling of the name we give it? – Laura Jan 20 '23 at 22:47
  • `words` is the array, `word` is the current element of the array inside the `foreach` or `filter` callback. – Barmar Jan 21 '23 at 00:15