I'm trying to compare all the values in a set. I read about using Set.values() to get an iterator object with all set values. However my loop isn't getting the values as wanted. Why isn't this working and what can I do to properly iterate over the values in a set? Thanks.
function greatestVal(arr) {
let answer = 0;
const numbers = new Set()
//store all arr nums in a set to remove duplicates
for(let i=0; i<arr.length; i++) {
numbers.add(arr[i])
}
const iterator = numbers.values()
for(var num of iterator){
if(iterator[num]>answer){
answer=iterator[num];
}
}
return answer;
};