Loop works only for first value of i in : i=0 ; i<=4 ; i++

why this code returns a value of the i=0 only(see the terminal)?
when I use console.log in each if statement instead of the return keyword, I get all the values for each value of ‘i’ .

  • how to turn the returned value of test() into a string?
    thank you.

A return is going to exit a function.

The if(myPhrase[i] === myAlpha[2]) is executed and the value is returned there is no reason to keep looping.

why is this happening ?
the 3 dots under toString says this message:
property ‘toString’ does not exist on ‘void’.

You should console.log(myCode) to see what is being printed.

In your original code, it returned one number.
So the toString method would work.

It looks like you are trying to convert letters into binary.

If that is true, then I think an easier solution would be to loop through the arr, get the charCode and then use the toString method to convert it to binary.

const alphaArr = ["A", "B", "C"];

alphaArr.forEach((letter) => console.log(letter.charCodeAt(0).toString(2)));
1 Like

no that is not what i am trying to do.
i am trying to get all the numbers printed into one horizontal line that is easy to read, like an array for example. i think I asked you the wrong question.

So you want to the print to the console an array of binary numbers?

No,not some generated binary numbers that some javascript method can do, all i want is that the numbers on myBinary array be printed everytime the if statements are true, than take those printed results which are separate in the console and put them in one array.

Why not just create an array and push those results to it?

That was my first idea, i tryed it but the method didn’t push them in an empty array i declared before like this myArray = [ ];

It would be easier to see what you tried if you write your code directly into the forum.

Can I see your full code for your attempt?

here is the code :

var myPhrase = 'CADB';
var myAlpha = ['A','B','C','D'];
var myBiary = ['01','1000','1010','100'];
function test(){
  for (let i=0;i<=4;i++) {
    if(myPhrase[i] == myAlpha[0]){
        console.log(myBiary[0])
    }
    if(myPhrase[i] == myAlpha[1]){
        console.log(myBiary[1])
    }
    if(myPhrase[i] == myAlpha[2]){
        console.log(myBiary[2])
    }
    if(myPhrase[i] == myAlpha[3]){
        console.log(myBiary[3])
    }
  }
}
var myArray = [];
var myCode = test();
myArray.push(myCode);
console.log(myArray);

the goal of the code is to turn myPhrase into elements from myBinary.

I guess the quickest fix would be to add the push statements inside the conditions.
It is a little repetitive and there is probably a much better solution but it would give you the result you are looking for.

var myPhrase = 'CADB';
var myAlpha = ['A','B','C','D'];
var mybiary = ['01','1000','1010','100'];
var myArray = [];
function test(){
  for (let i=0;i<=4;i++) {
    if(myPhrase[i] == myAlpha[0]){
        console.log(mybiary[0])
      myArray.push(mybiary[0])
    }
    if(myPhrase[i] == myAlpha[1]){
        console.log(mybiary[1])
        myArray.push(mybiary[1])
    }
    if(myPhrase[i] == myAlpha[2]){
        console.log(mybiary[2])
        myArray.push(mybiary[2])
    }
    if(myPhrase[i] == myAlpha[3]){
        console.log(mybiary[3])
        myArray.push(mybiary[3])
    }
  }
}

test();
console.log(myArray); 

One way I would restructure this would be to create an object for the letters and binary numbers and then loop through that object and push results to an array.

const alphaObj = {
  C: "1010",
  A: "01",
  D: "100",
  B: "1000"
};

const binaryArr = [];

for (const letter in alphaObj) {
  console.log(alphaObj[letter]);
  binaryArr.push(alphaObj[letter]);
}

console.log(binaryArr);

thank you this has worked perfectly, I had this idea in my mind but was not complete.
thank you.

i didn’t study the const yet so I’ll wait.

You can use a string variable and do string concatenation and then return that string at the end of the function

function test() {
    let strVal = "";

    for (let i=0 ;i<=4; i++) {
        if (myPhrase[i] == myAlpha[0]) {
            strVal += myBiary[0];
        }

        if (myPhrase[i] == myAlpha[1]) {
            strVal += myBiary[1];
        }

        if (myPhrase[i] == myAlpha[2]) {
            strVal += myBiary[2];
        }

        if (myPhrase[i] == myAlpha[3]) {
            strVal += myBiary[3];
        }
    }

    return strVal;
}

Yeah that could work too. But for me personally I think the cleaner option would be to create an object with the values and then loop through it. It just looks cleaner than writing a whole bunch of if statements :grinning:

Let me reach the objects in the curriculum first wilkins :slight_smile: .

Oh no worries.
I was just replying to @Otumian-empire 's post.

1 Like