I have the following code:
function transposeTwoStrings(arr){
var stringOne = arr[0];
var stringTwo = arr[1];
for (var i = 0; i < arr.length; i++) {
return (stringOne.charAt(i) + " " + stringTwo.charAt(i));
}
I am expecting output as follows:
e.g. transposeTwoStrings(['Hello','World']);
should return
H W
e o
l r
l l
o d
However it is only returning the first character at the first index of each string. I am trying to return all characters for both strings.
I initially had a much more verbose solution using if/else and decided to refactor using for loop.
I am stuck as output currently being shown when executed is
H W
I have the following code:
function transposeTwoStrings(arr){
var stringOne = arr[0];
var stringTwo = arr[1];
for (var i = 0; i < arr.length; i++) {
return (stringOne.charAt(i) + " " + stringTwo.charAt(i));
}
I am expecting output as follows:
e.g. transposeTwoStrings(['Hello','World']);
should return
H W
e o
l r
l l
o d
However it is only returning the first character at the first index of each string. I am trying to return all characters for both strings.
I initially had a much more verbose solution using if/else and decided to refactor using for loop.
I am stuck as output currently being shown when executed is
H W
Share Improve this question asked Apr 3, 2017 at 19:41 g01di3l0ck5g01di3l0ck5 114 bronze badges 06 Answers
Reset to default 4You are retun
ing in the middle of your loop, which will cause it to only loop once. Also, you're using the wrong object to determine the length
.
Try this instead:
function transposeTwoStrings(arr){
var stringOne = arr[0];
var stringTwo = arr[1];
var ret = '';
for (var i = 0; i < arr[0].length; i++) {
ret += stringOne.charAt(i) + " " + stringTwo.charAt(i) + '\n\r';
}
return ret;
}
The return in your loop makes it stop during its first iteration. Once you fix that, you should also change the end condition of your loop as otherwise it will only iterate twice.
But you can do it also using split
, map
and join
like this:
function transposeTwoStrings(arr){
return arr[0].split('')
.map( (a, i) => a + ' ' + arr[1][i] )
.join('\n');
}
var result = transposeTwoStrings(['Hello','World']);
console.log(result);
Alternative with replace
callback
function transposeTwoStrings(arr){
var i = 0;
return arr[0].replace(/./g, c => c + ' ' + arr[1][i++] + '\n')
}
var result = transposeTwoStrings(['Hello','World']);
console.log(result);
Use one of the strings length as a condition in the for loop:
function transposeTwoStrings(arr){
var stringOne = arr[0];
var stringTwo = arr[1];
var calculatedStr = '';
var loopCount = 0;
// CALCULATE LOOP COUNT
if(stringOne.length > stringTwo.length){
loopCount = stringTwo.length;
}else{
loopCount = stringOne.length;
}
for (var i = 0; i < loopCount; i++) {
calculatedStr += stringOne.charAt(i) + " " + stringTwo.charAt(i));
}
// ADD REMAINING CHARACTERS TO STRING RESULT IF SIZE IS DIFFERENT
if(stringOne.length > loopCount){
calculatedStr += stringOne.substring(loopCount);
}
if(stringTwo.length > loopCount){
calculatedStr += stringTwo.substring(loopCount);
}
return calculatedStr;
}
this will do the trick:
var strgs = ['Hello','World'];
var res = strgs[0].split('').map((l, ix)=>[l, strgs[1][ix] || '']);
console.log(res);
if you prefer concatenated strings:
var strgs = ['Hello','World'];
var res = strgs[0].split('').map((l, ix)=>l + ' ' + strgs[1][ix] || '')
console.log(res);
printing string to console:
var strgs = ['Hello','World'];
var res = strgs[0].split('').map((l, ix)=>l + ' ' + strgs[1][ix] || '')
res.forEach((e)=>console.log(e));
In case of different size strings the first will define the length and the second element will be '' if the first element don't exist.
You could use the maximal length of the strings and respect empty places.
function transposeTwoStrings(array){
var i, length = array.reduce(function (r, a) { return Math.max(r, a.length); }, 0),
result = [];
for (i = 0; i < length; i++) {
result.push(array.map(function (a) { return a[i] || ' '; }).join(' '));
}
return result;
}
console.log(transposeTwoStrings(['Hello', 'World']).join('\n'));
console.log(transposeTwoStrings(['This', 'is', 'a', 'test']).join('\n'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Give this a shot:
function transposeTwoStrings(arr) {
var array = [];
for (var i = 0; i < arr[0].length; i++) {
array.push(arr[0][i] + " "+ arr[1][i]);
}
return array.join('\n');
}
console.log(transposeTwoStrings(['Hello','World']));