I'm new and I need help to find the right solution for my function in java
function concatenateMatrix(matrix) {}
var stringMatrix = [
['H', 'e', 'l', 'l', 'o'],
[' ', 'J', 'a', 'v', 'a'],
['W', 'o', 'r', 'l', 'd', '!']
];
var matrix = '';
newArray = [];
for (var i = 0; i < stringMatrix.length; i++) {
for (var j = 0; j < stringMatrix[i].length; j++);
newArray.push(stringMatrix);
stringMatrix = '';
}
console.log(concatenateMatrix(stringMatrix))
It should be Hello , Java World! displayed.
What do I wrong? Thanks a lot
I have solved it, thanks a lot
for(var x in stringMatrix) {
if(stringMatrix[x] instanceof Array) {
stringMatrix[x] = stringMatrix[x].join("");
}
} var concatenateMatrix = stringMatrix.join("");
return concatenateMatrix;
You can first flatten/merge the array elements and then join
then with ""
to show the full word like
[].concat.apply([], stringMatrix).join("")
var stringMatrix = [ ['H','e','l','l','o'], [' ','J','a','v','a'], ['W','o','r','l','d','!'] ]; console.log([].concat.apply([], stringMatrix).join(""))
function matrixMapper(matrix){
var str = ''
matrix.map(function(arr){
str += arr.join('') + ' '
})
console.log(str)
}
Basically iterate over the arrays in the matrix, join the subarrays and add them to your string
You can also use reduce()
, concat()
and join()
method of array.
DEMO
var arr= [ ['H','e','l','l','o'], [' ','J','a','v','a'], ['W','o','r','l','d','!'] ]; console.log(arr.reduce((r,v)=>r.concat(v),[]).join(''));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Using map()
var arr= [ ['H','e','l','l','o'], [' ','J','a','v','a'], ['W','o','r','l','d','!'] ]; console.log(arr.map(v=>v.join('')).join(''));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Buddy, I think you are new to javaScript you can achieve your goal using single array only, don't need to use two loops and two array. I have checked your code write little bit code for you try to
var stringMatrix = ['H','e','l','l','o',' ','J','a','v','a','W','o','r','l','d','!'];
for(var i=0; i<stringMatrix.length; i++){
document.write(stringMatrix[i]);
}
since everybody pointed out easier ways of doing that, but you have very concerning flaws in your code writing, here is a simple correction of your code :
function concatenateMatrix(matrix) {
var stringMatrix = [
['H','e','l','l','o'],
[' ','J','a','v','a'],
['W','o','r','l','d','!']
];
var matrix = '';
newArray = [];
for(var i=0; i<stringMatrix.length; i++){
for(var j=0; j<stringMatrix[i].length; j++){
newArray.push(stringMatrix[i][j]);
}
}
return newArray;
}
console.log(concatenateMatrix(stringMatrix));
you closed too early your curly braces, code with indentation so you can see theese problems, you missed the return value, you were adding stringMatrix
instead of stringMatrix[i][j]
, and you were clearing your stringMatrix
every time in your i
loop (maybe you wanted to clear stringMatrix[i]
instead ?)
You can concat the arrays and then Join the final array with Join function of arrays.
var str=[];
for(var i=0;i<stringMatrix.length;i++){
str = str.concat(stringMatrix[i]);
}
console.log(str.join(" "));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.