Small coding problem for a possible job

HEllo,

I have a possible job interview and I saw that last year the employers asked so question like this coding, I can’t answer it and I’m pretty sure it would be almost the same thing, can you help me ?

What have you tried? This is a sanity check to make sure you can do very, very basic stuff, btw

We just learn how to do loop in JS last week, I know how to generate 1 2 3 4 …until 100

I’m in a business school, but they said it was ok even if I just started learn coding this month, even if I asked on internet what they want is someone who can find a solution…if somenone could help me it would be great

The worst that i can think would be like this:

let arr = [];
for(let i=1;i<=100;i++){
	arr.push(i);
}
let arr1 = arr.map(e => e % 3 === 0 && e % 5 === 0?"FizzBuzz":e);
let arr2 = arr1.map(e => e % 3 === 0?"Fizz":e);
let arr3 = arr2.map(e => e % 5 === 0?"Buzz":e);
console.log(arr3.join(", "));

Hope it can help with your interview :smile:

1 Like

Waaw thank you Minggas for taking the time to anwser me it’s so nice of you I really don’t know what to say !! I’ll keep you update for sure, thank you again !!

Break it down into small chunks.

First just make sure you can iterate through the sequence of numbers having each number prints to the console.

Then just do a simple script that can test if number is divisible by 3 with no remainder. (that’s what % is for). If there is no remainder then console.log("Fizz"); otherwise print the number.

Then just do a simple script just like like the one above for 5.

Then put it all together checking each number if it can be divided by 3 or 5 without a remainder and prints the outcome then moves on to next number until you reach 100.

Hopefully this helps you figure out the problem without me outright giving you the script…

2 Likes

Note that code is terrible in everyway you can imagine. I don’t have ideia you are starting in JS yet. So use that code as what not to do. It will work but has a badly implementation.

1 Like

Actually minggas I think you can write something way worse… something more verbose that take up 30 lines of code. :crazy_face:

2 Likes

hehehe You’re right, i’m think i’m on lazy mode today :stuck_out_tongue:

2 Likes

So you can check in the loop what the current number is

if (i == 1) {
  console.log(1);
} else if (i == 2) {
  console.log(2);
} else if (i == 3) {
  console.log('Fizz');
} else if (i == 4) {
  console.log(4);
} ....

... else if (i == 90) {
  console.log('FizzBuzz');
} else if (i == 91) {
  console.log('91') {
} .....

btw, why did you delete the question?

That is pretty bad. But if the OP only just learned loops, this is going to be way, way way beyond his or her understanding - if they actually get to interview with a copy-paste of that and they get asked about it they’ll be in trouble, because there will be no chance of them being able to explain how it works.

2 Likes

This is my solution:

var a = [];
function numbers () {
  for (var i = 1; i < 101; i++) {
    if (i % 3 === 0 && i % 5 === 0) {
      a.push("fizzBuzz, ");
    } else if (i % 3 === 0) {
      a.push("Fizz, ");
    } else if (i % 5 === 0) {
      a.push("Buzz, ");
    }  else {
      a.push(i + ", ");
    }
  }
  $("#numbers").html(a);//or a return statement
}

Well DanCouper I guess 99 else if statements is about as ugly as it gets… But it works.

My mistake think in a solution without ask the level of acknowledgment, was a really bad assumption that he was not a beginner

1 Like

Hey to everyone replying here: I’m debating whether to unlist this topic but in the meantime, I think posting full solutions is actively harmful to the OP:

  1. they have only learnt how loops work, this is their level of knowledge.
  2. they have a task for a job interview to implement FizzBuzz
  3. If you post a full solution, the danger is that they will copy-paste that, which will cause them to be asked about it in the interview
  4. They will be asked to explain it, which will not be possible.

Please bear this in mind. If you are posting, please post something that will help them understand, not a solution.

1 Like

I agree that complete solutions should not be posted here because this is a learning site… However, if you unlist this topic that will be 18 minutes of my life I will never get back… And I’ll lose a bunch of badges :stuck_out_tongue_winking_eye:

1 Like

My solution is to have a function that uses a for loop to create numbers from one to one hundred. For each number created i check if the remainder divided by 3 or 5 = 0. If the remainder doesn’t equal to 0 push the number to the array. If the remainder does equal to 0 push “Fizz”, “Buzz” or “FizzBuzz” to the array. That’s the explanation that DanCouper requested. Hope this helps…

1 Like