0

I want to output as a string from this code. I just checked its length but I could not output as a string again like-('joy', 'james';). I don't know where is my problem with the output string. please help to solve this problem. thank you.

function oddFriend(name) {
  let oddFr = [];
  for (let i = 0; i < name.length; i++) {
    let frName = name[i].length;
    console.log(frName);
    if (frName % 2 != 0) {
      oddFr.push(frName);
    }
  }
  return oddFr;
}

console.log(oddFriend(["jon", "james", "robert", "george", "Leo", "joy"]));
Andy
  • 61,948
  • 13
  • 68
  • 95
Jabed
  • 15
  • 5

4 Answers4

3

In the oddFriend function, you are pushing the length of the name to the array instead of the name itself. Trying pushing name[i] instead.

Sarah
  • 132
  • 3
1

You just need to check that the length of the name isn't an even number, and then push the element into the output array, not the length of the element.

function oddFriend(list) {
  let oddFr = [];
  for (let i = 0; i < list.length; i++) {
    const len = list[i].length;
    if (len % 2 !== 0) {
      oddFr.push(list[i]);
    }
  }
  return oddFr;
}

console.log(oddFriend(["jon", "james", "robert", "george", "Leo", "joy"]));

You could also use filter for this.

function oddFriend(list) {
  return list.filter(name => {
    return name.length % 2 !== 0;
  });
}

console.log(oddFriend(["jon", "james", "robert", "george", "Leo", "joy"]));
Andy
  • 61,948
  • 13
  • 68
  • 95
1

Your result is the number of chars (length of string) which is frName but you need each result to be the actual string which is name[i].

Corrected OP code

function oddFriend(name) {
  let oddFr = [];
  for (let i = 0; i < name.length; i++) {
    let frName = name[i].length;
    if (frName % 2 === 1) {
      oddFr.push(name[i]);
    }
  }
  return oddFr;
}

console.log(oddFriend(["jon", "james", "robert", "george", "Leo", "joy"]));

Fast and terse alternative

const array = ["jon", "james", "robert", "george", "Leo", "joy"];

let odd = array.flatMap(o => o.length % 2 === 1 ? [o] : []);

console.log(odd);
zer00ne
  • 41,936
  • 6
  • 41
  • 68
0

You properly use that name length to check if the length is odd using the modulus operator, but you should push the original name parameter passed into the function instead of that value which is set to the name length.

function filterOddLengths(stringsToFilter) {
  let oddLengths = [];
  for (let i = 0; i < stringsToFilter.length; i++) {

    if (stringsToFilter[i].length % 2 !== 0) {
      oddLengths.push(stringsToFilter[i]);
    }
  }

  return oddLengths;
}

console.log(filterOddLengths(["jon", "james", "robert", "george", "Leo", "joy"]));

Update: Please use more specific variable names. The parameter named name is very confusing because the function expects an array of names.

  • I'm guessing you haven't tested this? What do you think `oddFriends.push(name)` is doing? – Andy Feb 05 '22 at 04:21
  • i think it will be [ oddFr.push(list[i]); ] . I got it another comment. it was my mistake. i appreciate your dedication and helping mind. thank you. – Jabed Feb 05 '22 at 04:27
  • 1
    Updated! Yea I was way off. Thank you @Andy!| Sorry I didn't test it. Won't happen again. The variable names need to be improved here to make the code more understandable. – Brian Danowski Feb 05 '22 at 13:04