Message from JavaScript discussions
September 2017
— Hello guys i need your help to translate this code in python to javascript
#python code
def new_list(n):
n_dict = {}
n_dict['words'] = []
n_dict['numbers'] = []
for i in n:
if i == str(i):
n_dict['words'].append(i)
else:
n_dict['numbers'].append(i)
return n_dict
// javaScript
function new_list(n){
var dict = {};
dict.words = [];
dict.numbers = [];
for(var i in n){
if (i == str(i)){
dict.words.push(i);
}
else{
dict.numbers.push(i);
}
}
return dict;
}
var m = ["book", 4, "hook", 4.5]
console.log(new_list(m))
m = ["book", 4, "hook", 4.5]
print(new_list(m))
— My problem is the str function is not valid in javascript, what do should i use. i want the result to be
dict = { 'words' : ["books", "hooks"], 'numbers': [4, 4.5]
— If you want to check if something is a string you can check if(typeof i === "string"){}
— Okay thanks
— Its saying it needs an operator
— Disgusting type check
— Yea. i edited the answer
— Check types in python with isinstance(object, type)
— Lols but it worked
— Okay
— Huh