My goal was to find the biggest number using the Math object in the console. However, How could I use the FOR loop to PROMPT the number 1, number 2 so that I don't have to write the whole object? I just wanted to return the value each time I use for loop but could not think that way so later I had to write an object.
var num = {
x: parseInt(prompt("Enter your number1")),
Y: parseInt(prompt("Enter your number2")),
W: parseInt(prompt("Enter your number1")),
Z: parseInt(prompt("Enter your number1")),
}
console.log(Math.max(num.x, num.Y,num.Z,num.W));
You can loop over the Object's keys
var num = {
x: 0,
Y: 0,
W: 0,
Z: 0,
}
Object.keys(num).forEach(key => num[key] = +prompt(`Enter your ${key}`));
// Object.keys(num).forEach(function(key) {
// num[key] = +prompt('Enter your ' + key);
// });
console.log(num);
for loop and an array
var nums = [];
for (var i=0; i<4; i++) {
nums.push(+prompt('Enter your ' + i));
}
console.log(nums);
console.log(Math.max(...nums));