I was writing a function to extract the index of the word which is included in a word, For example : 4of Fo1r pe6ople g3ood th5e the2 This should align order wise, Below is the code,
function order(words){
var result = []
var sorting = words.split(' ').sort()
console.log(sorting);
for(let i=0; i<sorting.length;i++)
{
var temp = sorting[i].split('').sort()//By this the words in the sentence get sorted and i will get the index(number) of the corresponding word at first position.
console.log(temp);
var valueHolder = parseInt(temp) //I will take the above obtained value and convert it into integer
console.log(valueHolder);
result.splice((valueHolder-1),0,sorting[i]) //will pass the index here
}
console.log(result);
}
But i am getting the wrong output : [ 'Fo1r', 'the2', '4of', 'g3ood', 'pe6ople', 'th5e' ]
Did i miss anything, Can anyone help me with this Thanks.
Above array.splice(index,0,item) will just insert the element at the specified position, and with 0
Expected output is : [ 'Fo1r', 'the2', 'g3ood', '4of', 'th5e','pe6ople' ]
sorting[i].split('').sort()produces an array of characters if i dont get it wrong, which is stored intemp. How can that be used as an argument toparseInt??