0

Write a program that will fill the guest list. Put the guest to the admit if they are invited; if not, put it on refuse.

My code:

const admit = document.getElementById("admit");
const refuse = document.getElementById("refuse");
admit.textContent = "Admit: ";
refuse.textContent = "Refuse: ";
const people = [
  "Chris",
  "Anne",
  "Colin",
  "Terri",
  "Phil",
  "Lola",
  "Sam",
  "Kay",
  "Bruce",
];

for (let i = 0; i < people.length; i++) {
  if (people[i] === "Phil" || people[i] === "Lola") {
    refuse.textContent += people[i] + ",";
  } else {
    admit.textContent += people[i] + ", ";
  }
}

I got the program worked.

Admit:  Chris, Anne, Collin,Terri,Sam,Kay,
Refuse:  Phil,Lola,

But I'm trying to solve how to add and and replace the , with ..

Here is the output that I want:

Admit:  Chris, Anne, Collin,Terri, Sam, Kay, and Bruce.
Refuse:  Phil and Lola.

3 Answers 3

2

const people = ['Chris', 'Anne', 'Colin', 'Terri', 'Phil', 'Lola', 'Sam', 'Kay', 'Bruce'];
const admit = [];
const refuse = [];

for (let person of people) {
  (person === 'Phil' || person === 'Lola' ? refuse : admit).push(person);
}

const toText = (array) => {
  let multipleNames = array.length > 1;
  return `${array.slice(0, -1).join(", ")}${multipleNames ? " and " : ""}${array.slice(-1)[0]}.`;
};

admit.textContent = `Admit: ${toText(admit)}`;
refuse.textContent = `Refuse: ${toText(refuse)}`; 
console.log(admit.textContent);
console.log(refuse.textContent);

Sign up to request clarification or add additional context in comments.

3 Comments

There is small issue with this code. Say one of name is not present then it will response like Refuse: and Bruce
Also it is doing ` and ${admit.slice(-1)[0]}`` which should be array.slice i guess
const toText = (array) => { let multipleNames = array.length > 1; return ${array.slice(0, -1).join(", ")}${multipleNames ? " and " : ""}${array.slice(-1)[0]}.; };
0

You can try below code. Add and and comma conditionally.

const admit = document.getElementById('admit');
const refuse = document.getElementById('refuse');
admit.textContent = 'Admit: ';
refuse.textContent = 'Refuse: ';
const people = ['Chris', 'Anne', 'Colin', 'Terri', 'Phil', 'Lola', 'Sam', 'Kay', 'Bruce'];

for (let i = 0; i <= people.length; i++) {
if (people[i] === 'Phil' || people[i] === 'Lola') {
      refuse.textContent += (refuse.length == 1) ' and ' + people[i] : people[i];
  }else{
      admit.textContent += (people.length == i) ' and ' + people[i] : people[i] + ',';
  }
}

Comments

0

Try this out:

const admit = document.getElementById("admit");
const refuse = document.getElementById("refuse");
admit.textContent = "Admit: ";
refuse.textContent = "Refuse: ";
const people = ["Chris", "Anne", "Colin", "Terri", "Phil", "Lola", "Sam", "Kay", "Bruce"];
let refusedPeople = [];
let admitedPeople = [];
people.forEach((person) => {
  person === "Phil" || person === "Lola" ? refusedPeople.push(person) : admitedPeople.push(person);
});

const genrateConcatString = (arr, person, index) => {
  let text = "";
  if (index === arr.length - 1) {
    arr.length === 1 ? (text += person + ".") : (text += " and " + person + ".");
  } else {
    index === arr.length - 2 ? (text += person) : (text += person + ", ");
  }
  return text;
};
refusedPeople.forEach((person, index) => {
  refuse.textContent += genrateConcatString(refusedPeople, person, index);
});

admitedPeople.forEach((person, index) => {
  admit.textContent += genrateConcatString(admitedPeople, person, index);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.