create a function (in javascript) that will take those two strings and transpose
ID: 3814826 • Letter: C
Question
create a function (in javascript) that will take those two strings and transpose them, so that the strings go from top to bottom instead of left to right. There should be one spacein between the 2 characters, and if one string is longer than other, there should be space where the character would be. Having some problems getting my code to work correctly, here's what I have so far:
function transposeTwoStrings(arr){
var arrayOne = [];
for(var i = 0; i < arr.length; i++) {
arrayOne.push([]);
};
for(var i = 0; i < arr.length; i++){
for(var j = 0; j < arr.length; j++) {
arrayOne[j].push(array[i][j]);
};
};
return(arayOne);
}
transposeTwoStrings(['hello', 'world']);
Explanation / Answer
here i have written code to do above task please refer that,
let testArr = ['Hello','World'];
function transposeTwoStrings(arr) {
let result = '';
let counter = 0;
while(arr[0][counter] || arr[1][counter]) {
result += arr[0][counter] || ' ';
result += ' '; // if you want a space in between
result += arr[1][counter] || ' ';
result += ' '
counter++
}
return result.slice(0, -1)
}
transposeTwoStrings(testArr)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.