-2

Write a program that prints all the numbers from 1 to 100. However, for

multiples of 3, instead of the number, print "Linio". For multiples of 5 print

"IT". For numbers which are multiples of both 3 and 5, print "Linianos".

But here's the catch: you can use only one if. No multiple branches, ternary

operators or else.

indhu
  • 91
  • 7
  • 3
    There is no question in this post. Please fix it – Tracer69 Feb 13 '20 at 11:46
  • 1
    This is FizzBuzz. You can do this. I believe in you (although without conditionals I can see it being particularly difficult) – evolutionxbox Feb 13 '20 at 11:47
  • Plenty of examples here: https://stackoverflow.com/questions/11764539/writing-fizzbuzz/11764613 – Ben Aston Feb 13 '20 at 11:52
  • yes but please note the conditions to write the code..... Thank you – indhu Feb 13 '20 at 11:55
  • 3
    This sounds like a task you were given to prove _your_ skills … so why do we have to do all the work for you then? – misorude Feb 13 '20 at 11:55
  • 1
    sorry, u mistaken me. just wanted to people know about these because i was stuck with these question. it may help someone. thats why i posted this question. – indhu Feb 13 '20 at 12:02

2 Answers2

1

var replacer = ["IT", "Linio", "Linianos"];
var accumulator = [];
for (i = 1; i <= 100; i++) {
  if (i % 3 == 0 || i % 5 == 0) {
    accumulator.push(replacer[Number(i % 3 == 0 && i % 5 >= 1) + (Number(i % 3 == 0 && i % 5 == 0) * 2)]);
    continue;
  }
  accumulator.push(i);
}
console.log(accumulator);

here is the solution for this question.

Mohammad Faisal
  • 2,144
  • 15
  • 26
indhu
  • 91
  • 7
1

The modified version of your code without a single if

var replacer = ["IT", "Linio", "Linianos"];
var accumulator = [];
for (i = 1; i <= 100; i++) {
  ((i % 3 == 0 || i % 5 == 0) 
     && accumulator.push(replacer[Number(i % 3 == 0 && i % 5 >= 1) + (Number(i % 3 == 0 && i % 5 == 0) * 2)])) 
     || accumulator.push(i);
}
console.log(accumulator);
Mohammad Faisal
  • 2,144
  • 15
  • 26