I'm writing a function without the use of any JS built-in functions. This function accepts an array ['z','C',2,115,30] and then creates a new array with ascii values of chars in that first array plus the normal numbers that were there previously, so: [127, 67, 2, 115,30]. The problem I'm having is that I cannot properly get the numbers from the 1st array into the second array.
So far I can get the new array to [127,67,2,1,1,5,3,0]. So the chars ascii value is being inputted into the new array properly, but while iterating through the numbers it puts each digit of a number and then a comma into the array when i just want the whole number to be inputted instead of each digit.
function func(x){
var str = '';
var arr = [];
for(var i=0; i<x.length;i++){
str += x[i]
}
for(var j=0; j<str.length;j++){
if(isNaN(str[j])){
arr += str.charCodeAt(j) + ', ';
}
else if(!isNaN(str[j])){
arr += str[j] + ', ';
}
} print(arr)
}
func(['z','C',2,115,30])
I expect output of [122, 67, 2, 115, 30], current output is [122, 67, 2, 1, 1, 5, 3, 0,]
typeofnotisNaN()becauseisNaN()doesn't mean what one might think it means.if (something) else if (!something)is pointless. Ifsomethingis not true then!somethingis true, by definition.