I'm working through an application to a coding bootcamp and am stuck. I could use some help seeing what I'm doing wrong. I'll post the question, the code and the output. Any help is greatly appreciated.
Question:
Jim plans to buy new pack of assorted light bulbs.
He has heard that light bulbs are the most efficient when their serial number is an odd number and has exactly six digits. He would appreciate you writing some code that figured out which serial numbers met the criteria.
When this function runs, it will receive an array of serialNumbers - something like
[32438, 34193, 149143, 4329429, 98537, 238791, 23492, 298342]
You need to add code that will ensure Jim collects the serial numbers he wants to keep in the provided variable, efficientSerialNumbers.
For the above array, this would mean that efficientSerialNumbers should end up containing
[149143, 238791]
We've added a log of serial numbers so you can see in the console what numbers are in the array when we try out your function. You are welcome to remove it, change it, do whatever you find useful!
function findEfficientBulbs(serialNumbers) {
console.log(serialNumbers)
const efficientSerialNumbers = [];
// Write your code here
for (let i= 0 ; i < serialNumbers.length ; i++)
if (serialNumbers[i].length <=5 || serialNumbers[i].length >=7) {
serialNumbers[i].shift()
} else if (serialNumbers[i] % 2 === 1 && serialNumbers[i].length === 6 ) {
serialNumbers[i].shift()
efficientSerialNumbers.unshift(serialNumbers[i])
}
console.log(efficientSerialNumbers)
console.log(serialNumbers)
return efficientSerialNumbers;
}
Output
4 Passing 1 Failing
should return an array
✓ Well done!
logs
[ 123123 ]
[]
[ 123123 ]
should discard serial numbers with five or fewer figures
✓ Well done!
logs
[ 1, 123, 12345 ]
[]
[ 1, 123, 12345 ]
should discard serial numbers with seven or more figures
✓ Well done!
logs
[ 1234567, 123456789 ]
[]
[ 1234567, 123456789 ]
should discard even numbers
✓ Well done!
logs
[ 1234, 123456, 12345678 ]
[]
[ 1234, 123456, 12345678 ]
should keep all efficient numbers - those that are odd and have six digits in
✕ AssertionError: expected [] to deeply equal [ 234567, 456789 ]
logs
[ 123456, 234567, 345678, 456789 ]
[]
[ 123456, 234567, 345678, 456789 ]
forloop.lengthproperty, you could filter by number range and remainder.Btw in real life you don't need to push filtered values to a second array. An array already has
Array::filter()method to make the job for you.If the serials are strings: