Without using .join()?

Without using .join()?
0

#1

Almost there, but not understanding why my code won’t work. Looking for guidance as always.

Instructions:

Write a function named toSentence that takes an array and returns a string with the elements joined by commas, with a trailing 'and'

Example:

If you pass ["Sue", "Will"] it should return "Sue and Will"
If you pass ["Sue", "Will", "Rachel"] it should return "Sue, Will and Rachel"
function toSentence(arr) {
  let str = '';
  
  for (let i = 0; i < arr.length; i++) {
      if (!arr[i].length - 1){
          str += arr[i] + ', '
      } else if (arr[i].length - 1){
          str += ' and ' + arr[i]
      } else {
          return str;
      }
      
     }
    return str;  
};

#2

What are you trying to check there?


#3

If it is the last element in the array. Should I do if (arr[i] === arr.pop())?


#4

No need. You already have the basic skeleton. You just have to compare what you already have with something.

For your if statement you need this.

if (arr[i].length - 1 === ",")

What about your else if statement?


#5

I don’t understand why you are checking if the last element is a comma? I just don’t understand how that goes through. Doesn’t .length return a number?


#6

yes .length returns a number. You are trying to add commas to your ‘str’ string in your if statement. But how does
if (!arr[i].length - 1) check your array to see if you need to add commas to your string?

It basically means grab the last letter index of each word and negate it. so It’s something like

if (!3) which will always evaluate to false.


#7

Okay, that still doesn’t help in the slightest lol


#8

No prob bro. I actually gave you a wrong condition in the if statement too lol. To explain my point, take a look at this if statement.

if (arr[i][arr[i].length - 1] === ",")

You are running a forloop to check every word in the array. If a word meets certain conditions, you want to add its word and comma to the string. So what should I be looking for in each word to add its word and comma to the string?

You can simply check if each the last element of each word is a comma and that is what the above code does.


#9

It’s so confusing that your code runs that. What the above tells me in perfect English is this…

“If (Array’s index & the last element in the array are EXACTLY EQUAL to comma)”?

How in the hell does that make sense? How does " , " === “Rachel”


#10

Also, hadn’t noticed they don’t want an Oxford comma…psh…plebs.


#11

Ok, actually forget everything about what I said above. I thought words in the arrays contained commas, I need to have my eyes checked…


#13

No, it’s okay. This one has been tricky for me.


#14

Are you not allowed to solve this with the join method? I can use the join method and create a one-liner for the solution.


#15

Yes, I cannot use it. I mean, if I could, I definitely wouldn’t have to make this thread.


#16

You can check to see if array contains only 2 elements then concatenate the first element and the 2nd element with and in between.

If not run your for loop and keep adding words after commas until you reach the last element. Don’t add the comma then add “and” before the last word.


#17

It can still be solved with a one-liner using the String.prototype.replace method using a regular expression.


#18

Sure, let me just grab my wizard hat and wand right over here… :stuck_out_tongue:

Let’s not fool ourselves. We both know I’m not in the least bit good enough to attempt it.


#19

I’m sure I could solve it this way, but that makes even more code, right? Ideally, I would like to keep it the same length or just a bit shorter if possible. Certainly there is a way without checking if there are only 2 in the array?


#20

Keep following @shimphillip’s suggestions. He is guiding you to a solution, so I will step aside. Once you figure out a solution, I will show you my replace method solution.


#21

I have this but it still isn’t passing. Unfortunately made my code even longer… ugh

function toSentence(arr) {
  let str = '';
  
  if(arr.length === 2){
      return arr[0] + ', ' + arr[1];
  }
  
  
  for (let i = 0; i < arr.length; i++) {
      if (i < arr.length - 2){
          str += arr[i] + ', '
      } else if (i === arr.length - 2){
          str += arr[i] + ' '
      } else {
          str += "and " + arr[i]
      }
      
     }
    return str;  
};

Had a typo and fixed it.