Need explanation to understand this coding problem

I am solving a coding problem on codewars site.
But I don’t understand the problem and What should I return in this challenge??
So I need explanation to understand this coding problem
link to challenge:

Hi @abdulrahman.mhd.anas !

My first step would be to figure out what 11 is in binary.
Binary is a number system made up of 0’s and 1’s.

If you want to convert the first example of 11 into binary then it would be 1011.

I found a helpful tool that will convert the integer into binary for you.
https://decimal.info/Decimal-to-Binary/how-to-convert-11-to-binary.html

Now let’s take a close look at the example they gave us.

H e l l o   w o r l d !
1 0 1 1 1 x 0 1 1 1 0 x

The directions say, if the bit is 1, then we need to swap the case.

Your task is to take the original string and return the newly converted string.

Hello world! ==> heLLO wORLd!

The directions also say, once you are finished with the last bit of n, then you start again from the beginning.

It might be easier to see it if we break up the example this way.

H e l l  o   w o r  l d !
1 0 1 1  1 x 0 1 1  1 0 x

When we reach the end of 1011, then we start over again.

Lastly, the challenge tells us that we don’t have to check for bits for non-alphabetic characters(spaces, !@#, etc)
So that is why the example uses x’s because we don’t have to check for bits when it comes to the space and ! point.

I haven’t done this particular codewars challenge but I would solve a couple of the examples first with just pen and paper.
Then I would come up with a game plan to convert it to code.

Hope that helps!

2 Likes

I read the challenge description. but I don’t understand this sentence:

because 11 is 1011 in binary, so the 1st, 3rd, 4th, 5th, 7th, 8th and 9th alphabetical characters have to be swapped:

How do we know that so the 1st, 3rd, 4th, 5th, 7th, 8th and 9th alphabetical characters have to be swapped ?

Look very carefully at the visual again.

If there is a 1 underneath a letter then you have to swap the case.

Yes I understand how to do that.
Thank you @jwilkins.oboe for HELPING!! :slight_smile:

Recently, I started solving the challenge and I’m trying to solve and pass the challenge.
But I got stuck and I want help.

My code so far:

function swap(s, n){
  let str = s.split('');
  let bit = (n >>> 0).toString(2).split('');
  let bitLength = bit.length;
  let strWithOnlyLetter = s.replace(/([a-zA-Z]{4})/g, bit);
  console.log(...chunkArrayInGroups(str, bitLength)); // returns [ 'H', 'e', 'l', 'l' ] [ 'o', ' ', 'w', 'o' ] [ 'r', 'l', 'd', '!' ]
  return bit; 
}

console.log(swap('Hello world!', 11))



function chunkArrayInGroups(arr, size) {
  let newArr = [];
  for (let i = 0; i < arr.length; i += size) {
    newArr.push(arr.slice(i, i + size));
  }
  return newArr;
}

So I want to fill 1011 in the array items but When I find a non-alphabetic, How to skip this item and give the number to the next array item?

The results would be:

H e l l o   w o r l d !
1 0 1 1 1 x 0 1 1 1 0 x

So I found it difficult to apply that in code!

LINK TO THE CHALLENGE:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.