Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialFrancesco Paolini
9,910 Pointsturning array's items from strings to numbers
I would like to turn the items stored in the numbersToSum array from strings to numbers, but I don't know how or where I should apply the parseInt
let numbersToSum = [];
displayField.value = '';
//digit numbers in display
digit.addEventListener('click', (e) => { if(e.target.className === 'number-btn'){ displayField.value += e.target.innerHTML; }
//storing numbers in the array
if(e.target.className === 'sum-btn'){ numbersToSum.push(displayField.value); displayField.value = ''; }})
1 Answer
Steven Parker
231,153 PointsIf you want the array to contain only numbers, you'd want to convert before (or as) you push the values into the array:
numbersToSum.push(parseInt(displayField.value));
Francesco Paolini
9,910 PointsFrancesco Paolini
9,910 Pointsthank you Steven, always on point :)