Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

added 332 characters in body
Source Link
OPTIMUS PRIME
  • 1.4k
  • 12
  • 22
let names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"];
let spent = [];
let gifts = [];
let totals = [];
for (let i = 0; i < names.length; i++) {
 spent.push( getAmountSpent() ); 
 gifts.push( getGift() ); 
 totals.push( getSumTotals(i) );
}
console.log( totals );
function getAmountSpent() {
 return rand(1, 500, 2);
}
function getGift() {
 return rand(1, 50, 2);
}
function getSumTotals(i) {
 return +(spent[i] +* ( spent[i]1 *+ gifts[i] )).toFixed(2);
}
function rand(from, to, fixed = 0){
 return +(Math.random()*( to - from ) + from).toFixed(fixed);
}
var names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"].reduce( (prev, elem, index) => {
 let spent = rand(1, 500, 2);
 let gift = rand(1, 50, 2);
 prev[elem] = {
  new spent:UserData( spent,
 gift: gift,
 total: +(spent * ( 1 + gift )).toFixed(2),
 };
 return prev;
}, {});
console.log( "Jeremy spent: " + names.Jeremy.spent );
console.log( names );
function rand(from, to, fixed = 0){
 return +(Math.random()*( to - from ) + from).toFixed(fixed);
}
function UserData(spent, gift){
 this.spent = spent;
 this.gift = gift;
 this.total = +(spent * ( 1 + gift )).toFixed(2);
}
/* Google → Array reduce, Constructor functions */
let names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"];
let spent = [];
let gifts = [];
let totals = [];
for (let i = 0; i < names.length; i++) {
 spent.push( getAmountSpent() ); 
 gifts.push( getGift() ); 
 totals.push( getSumTotals(i) );
}
console.log( totals );
function getAmountSpent() {
 return rand(1, 500, 2);
}
function getGift() {
 return rand(1, 50, 2);
}
function getSumTotals(i) {
 return +(spent[i] + ( spent[i] * gifts[i] )).toFixed(2);
}
function rand(from, to, fixed = 0){
 return +(Math.random()*( to - from ) + from).toFixed(fixed);
}
var names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"].reduce( (prev, elem, index) => {
 let spent = rand(1, 500, 2);
 let gift = rand(1, 50, 2)
 prev[elem] = {
  spent: spent,
 gift: gift,
 total: +(spent * ( 1 + gift )).toFixed(2),
 }
 return prev;
}, {});
console.log( "Jeremy spent: " + names.Jeremy.spent );
console.log( names );
function rand(from, to, fixed = 0){
 return +(Math.random()*( to - from ) + from).toFixed(fixed);
}
let names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"];
let spent = [];
let gifts = [];
let totals = [];
for (let i = 0; i < names.length; i++) {
 spent.push( getAmountSpent() ); 
 gifts.push( getGift() ); 
 totals.push( getSumTotals(i) );
}
console.log( totals );
function getAmountSpent() {
 return rand(1, 500, 2);
}
function getGift() {
 return rand(1, 50, 2);
}
function getSumTotals(i) {
 return +(spent[i] * ( 1 + gifts[i] )).toFixed(2);
}
function rand(from, to, fixed = 0){
 return +(Math.random()*( to - from ) + from).toFixed(fixed);
}
var names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"].reduce( (prev, elem) => {
 let spent = rand(1, 500, 2);
 let gift = rand(1, 50, 2);
 prev[elem] = new UserData( spent, gift );
 return prev;
}, {});
console.log( "Jeremy spent: " + names.Jeremy.spent );
console.log( names );
function rand(from, to, fixed = 0){
 return +(Math.random()*( to - from ) + from).toFixed(fixed);
}
function UserData(spent, gift){
 this.spent = spent;
 this.gift = gift;
 this.total = +(spent * ( 1 + gift )).toFixed(2);
}
/* Google → Array reduce, Constructor functions */
added 332 characters in body
Source Link
OPTIMUS PRIME
  • 1.4k
  • 12
  • 22
varlet names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"];
varlet spent = [];
varlet gifts = [];
varlet totals = [];
for (let i = 0; i < names.length; i++) {
 spent.push( getAmountSpent() ); 
 gifts.push( getGift() ); 
 totals.push( getSumTotals(i) );
}
console.log( totals );
function getAmountSpent() {
 return rand(1, 500, 2);
}
function getGift() {
 return rand(1, 50, 2);
}
function getSumTotals(i) {
 return +(spent[i] + ( spent[i] * gifts[i] )).toFixed(2);
}
function rand(from, to, fixed = 0){
 return +(Math.random()*( to - from ) + from).toFixed(fixed);
}

P.s. Math.random() returns a number between 0 (included) and 1 (not included). If you need a random number between (example) 20 and 100, Math.random()*(100-20) will give a number between 0 and 80. After adding +20 to the result, you get a number from 20 to 100. That's what does this formula Math.random()*( to - from ) + from

P.P.s. Another way, to get the same thing:

var names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"].reduce( (prev, elem, index) => {
 let spent = rand(1, 500, 2);
 let gift = rand(1, 50, 2)
 prev[elem] = {
 spent: spent,
 gift: gift,
 total: +(spent * ( 1 + gift )).toFixed(2),
 }
 return prev;
}, {});
console.log( "Jeremy spent: " + names.Jeremy.spent );
console.log( names );
function rand(from, to, fixed = 0){
 return +(Math.random()*( to - from ) + from).toFixed(fixed);
}

var names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"];
var spent = [];
var gifts = [];
var totals = [];
for (let i = 0; i < names.length; i++) {
 spent.push( getAmountSpent() ); 
 gifts.push( getGift() ); 
 totals.push( getSumTotals(i) );
}
console.log( totals );
function getAmountSpent() {
 return rand(1, 500, 2);
}
function getGift() {
 return rand(1, 50, 2);
}
function getSumTotals(i) {
 return +(spent[i] + ( spent[i] * gifts[i] )).toFixed(2);
}
function rand(from, to, fixed = 0){
 return +(Math.random()*( to - from ) + from).toFixed(fixed);
}

P.s. Math.random() returns a number between 0 (included) and 1 (not included). If you need a random number between (example) 20 and 100, Math.random()*(100-20) will give a number between 0 and 80. After adding +20 to the result, you get a number from 20 to 100. That's what does this formula Math.random()*( to - from ) + from

let names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"];
let spent = [];
let gifts = [];
let totals = [];
for (let i = 0; i < names.length; i++) {
 spent.push( getAmountSpent() ); 
 gifts.push( getGift() ); 
 totals.push( getSumTotals(i) );
}
console.log( totals );
function getAmountSpent() {
 return rand(1, 500, 2);
}
function getGift() {
 return rand(1, 50, 2);
}
function getSumTotals(i) {
 return +(spent[i] + ( spent[i] * gifts[i] )).toFixed(2);
}
function rand(from, to, fixed = 0){
 return +(Math.random()*( to - from ) + from).toFixed(fixed);
}

P.s. Math.random() returns a number between 0 (included) and 1 (not included). If you need a random number between (example) 20 and 100, Math.random()*(100-20) will give a number between 0 and 80. After adding +20 to the result, you get a number from 20 to 100. That's what does this formula Math.random()*( to - from ) + from

P.P.s. Another way, to get the same thing:

var names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"].reduce( (prev, elem, index) => {
 let spent = rand(1, 500, 2);
 let gift = rand(1, 50, 2)
 prev[elem] = {
 spent: spent,
 gift: gift,
 total: +(spent * ( 1 + gift )).toFixed(2),
 }
 return prev;
}, {});
console.log( "Jeremy spent: " + names.Jeremy.spent );
console.log( names );
function rand(from, to, fixed = 0){
 return +(Math.random()*( to - from ) + from).toFixed(fixed);
}

added 332 characters in body
Source Link
OPTIMUS PRIME
  • 1.4k
  • 12
  • 22

Note, that toString returns a type of "String", but not "Number".
When you try to sum a number with string, you get a concatenated string "1" + 2 = "12"
To turn a string into Number, you must use a Number("str") function, or just a bunary + before the string:

console.log( "1" + 2 );
console.log( Number("1") + 2 );
console.log( +"1" + 2 );

Also, you use the same loop 3 times, but can use just one loop instead, and call all functions inside the one loop. And use your array.length instead of fixed number 5:

var names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"];
var spent = [];
var gifts = [];
var totals = [];
for (let i = 0; i < names.length; i++) {
 spent.push( getAmountSpent() ); 
 gifts.push( getGift() ); 
 totals.push( getSumTotals(i) );
}
console.log( totals );
function getAmountSpent() {
 return rand(1, 500, 2);
}
function getGift() {
 return rand(1, 50, 2);
}
function getSumTotals(i) {
 return +(spent[i] + ( spent[i] * gifts[i] )).toFixed(2);
}
function rand(from, to, fixed = 0){
 return +(Math.random()*( to - from ) + from).toFixed(fixed);
}

P.s. Math.random() returns a number between 0 (included) and 1 (not included). If you need a random number between (example) 20 and 100, Math.random()*(100-20) will give a number between 0 and 80. After adding +20 to the result, you get a number from 20 to 100. That's what does this formula Math.random()*( to - from ) + from

Note, that toString returns a type of "String", but not "Number".
When you try to sum a number with string, you get a concatenated string "1" + 2 = "12"
To turn a string into Number, you must use a Number("str") function, or just a bunary + before the string:

console.log( "1" + 2 );
console.log( Number("1") + 2 );
console.log( +"1" + 2 );

Also, you use the same loop 3 times, but can use just one loop instead, and call all functions inside the one loop. And use your array.length instead of fixed number 5:

var names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"];
var spent = [];
var gifts = [];
var totals = [];
for (let i = 0; i < names.length; i++) {
 spent.push( getAmountSpent() ); 
 gifts.push( getGift() ); 
 totals.push( getSumTotals(i) );
}
console.log( totals );
function getAmountSpent() {
 return rand(1, 500, 2);
}
function getGift() {
 return rand(1, 50, 2);
}
function getSumTotals(i) {
 return +(spent[i] + ( spent[i] * gifts[i] )).toFixed(2);
}
function rand(from, to, fixed = 0){
 return +(Math.random()*( to - from ) + from).toFixed(fixed);
}

Note, that toString returns a type of "String", but not "Number".
When you try to sum a number with string, you get a concatenated string "1" + 2 = "12"
To turn a string into Number, you must use a Number("str") function, or just a bunary + before the string:

console.log( "1" + 2 );
console.log( Number("1") + 2 );
console.log( +"1" + 2 );

Also, you use the same loop 3 times, but can use just one loop instead, and call all functions inside the one loop. And use your array.length instead of fixed number 5:

var names = ["Jeremy", "Arun", "Alisa", "Rohan", "Dana"];
var spent = [];
var gifts = [];
var totals = [];
for (let i = 0; i < names.length; i++) {
 spent.push( getAmountSpent() ); 
 gifts.push( getGift() ); 
 totals.push( getSumTotals(i) );
}
console.log( totals );
function getAmountSpent() {
 return rand(1, 500, 2);
}
function getGift() {
 return rand(1, 50, 2);
}
function getSumTotals(i) {
 return +(spent[i] + ( spent[i] * gifts[i] )).toFixed(2);
}
function rand(from, to, fixed = 0){
 return +(Math.random()*( to - from ) + from).toFixed(fixed);
}

P.s. Math.random() returns a number between 0 (included) and 1 (not included). If you need a random number between (example) 20 and 100, Math.random()*(100-20) will give a number between 0 and 80. After adding +20 to the result, you get a number from 20 to 100. That's what does this formula Math.random()*( to - from ) + from

Source Link
OPTIMUS PRIME
  • 1.4k
  • 12
  • 22
Loading
default

AltStyle によって変換されたページ (->オリジナル) /