On the function below, I'm trying to reshape a string into W-like shape, and made a new output with new arrangement based on its totalLevel
, check the test case at the end of the script for more clarity. I'm using nodejs
, and came up with idea using objects. But I have no clue on how to take the next str[i]
after it reach the totalLevel
. Thanks for any helps!
function shapeOfW (str, totalLevel) {
var obj = {}
var position = 0
for (var i = 0; i < str.length; i++) {
obj[position+1] = (str[i])
position++
if (posisi == totalLevel) {
break
}
}
return obj
}
//TEST CASE
console.log (shapeOfW('ABCDEFG', 3))//AEBDFCG
/*
Input : ABCDE
Transformed into: 1| A E
2| B D F
3| C G
OUTPUT : AEBDFCG
*/