Output in same line - For Of Loop

1k Views Asked by At

How do I output it in same line? Currently, it's listing in vertical list;

But I wanted in line like this "lAbI..."

Code works. It's just matter of outputting the result. Please provide simple solution.

   var words ="kzahdjdnshdkjts";
   var letters = words.split("");

   var final = "";
   for(let letter of letters){

    if(letter === "z"){
        letter = "`";
    }

  final = letter.charCodeAt();
  final += 1;

    if(final === 97){
            final = 65;
       }else if(final === 101){
            final = 69;
       }else if(final === 105){
            final = 73;
       }else if(final === 111){
            final = 79;
       }else if(final === 117){
            final = 85;
                } 

      final = String.fromCharCode(final);
      console.log(final);
       }
5

There are 5 best solutions below

0
On

You could take an output as HTML text by adding it to a node.

var words = "kzahdjdnshdkjts";
var letters = words.split("");

var final = "";
for (let letter of letters) {

  if (letter === "z") {
    letter = "`";
  }

  final = letter.charCodeAt();
  final += 1;

  if (final === 97) {
    final = 65;
  } else if (final === 101) {
    final = 69;
  } else if (final === 105) {
    final = 73;
  } else if (final === 111) {
    final = 79;
  } else if (final === 117) {
    final = 85;
  }

  final = String.fromCharCode(final);
  document.body.innerHTML += final;
}

0
On

Create a second string (in this case finalString), which is initially empty, and append to it instead of overwriting it (with +=).

You also need to make sure that you run the console.log() outside of the for loop:

var words = "kzahdjdnshdkjts";
var letters = words.split("");

var final = "";
var finalString = "";
for (let letter of letters) {

  if (letter === "z") {
    letter = "`";
  }

  final = letter.charCodeAt();
  final += 1;

  if (final === 97) {
    final = 65;
  } else if (final === 101) {
    final = 69;
  } else if (final === 105) {
    final = 73;
  } else if (final === 111) {
    final = 79;
  } else if (final === 117) {
    final = 85;
  }

  finalString += String.fromCharCode(final);
}

console.log(finalString);

Hope this helps! :)

0
On

You can append the letters first and the print the very final string to the console.

   var words ="kzahdjdnshdkjts";
   var letters = words.split("");

   var veryfinal = "";
   var final = "";
   for(let letter of letters){

    if(letter === "z"){
        letter = "`";
    }

  final = letter.charCodeAt();
  final += 1;

    if(final === 97){
            final = 65;
       }else if(final === 101){
            final = 69;
       }else if(final === 105){
            final = 73;
       }else if(final === 111){
            final = 79;
       }else if(final === 117){
            final = 85;
                } 

      final = String.fromCharCode(final);
      veryfinal=veryfinal + final;

       }
             console.log(veryfinal);

0
On

You can .map() the array that you split to a new array of char codes that gets passed to String.fromCharCode().

var words = "kzahdjdnshdkjts";

console.log( 
  String.fromCharCode(...words.split("").map(c => {
    const final = (c === "z" ? "`" : c).charCodeAt() + 1;

    switch (final) {
    case 97:  return 65;
    case 101: return 69;
    case 105: return 73;
    case 111: return 79;
    case 117: return 85;
    default:  return final;
    }
  }))
)

So now you have only a single call to String.fromCharCode, which gets an array of all the replaced char codes using spread syntax.

Much faster and clearer this way.

0
On

I haven't tried this code but this might work: (replace this with last 3 lines of your code.)

var finalString = "";
final = String.fromCharCode(final);
    finalString.concat(final)
}
console.log(finalString);

You need to concatenate string inside loop and print it outside loop.