Interview Questions

  • View all interview questions
  • blank
  • Oddball sum
    The practice coding question created by App Academy:
    Write a function called oddball_sum which takes in a list of numbers and returns the sum of all the odd elements.
    To solve this challenge we'll simply loop through the array while maintaining a final count, and every time an odd number is encountered we'll add it to the count.

    Sources

    http://prepwork.appacademy.io/coding-test-1/practice-coding-exercise/


    You need to be a premium member to see the rest of this question and code.

    Become a Premium Member

    With our large collection of challengs, tutorials, and solutions, we make it easy for you to become a better coder, prepare for interviews, and learn new skills from more experienced coders.

    “Also important to try coding in nonstructured or semistructured environments to see if you like it. Coderbyte really helped me.“ ― Jeff Ruder
    • 200+ Coding Challenges
    • Mock Interview Questions
    • 500,000+ Code Solutions
    • Algorithm Tutorials
    • Interview Prep Courses
    mrdaniel published this on 1/31/16 | javascript, bootcamp, array, App Academy
    Comments
  • +
  • 4
  • -
  • @thedragon That's actually more abstract, which makes it more dificult for someone learning JavaScript to grasp the concepts of iteration. In your example you're introducing: built-in methods, higher-order functions/callbacks, scoping, and the introduction of a call stack (via reduce). So, no. That's not simpler for a beginner. It's probably the route I'd take in solving the problem, but it's not how I'd teach a beginner about iteration and conditional checking. Also, it's not any faster as both solutions run in a linear time complexity.
  • +
  • 1
  • -
  • Why don't you use reduce? Isn't this simpler?
    function oddball_sum(nums) {
      return nums.reduce(function(total, item){
      	if (item % 2 === 1) {
      		return total += item;
      	}
      	return total;
      });
    }
    
  • +
  • -1
  • -
  • I agree with @richardjboothe. @thedragon - your code doesn't seem to take into account if the first element in the array is not an odd number. Will work with [1, 2, 3, 4, 5], but not with [2, 3, 4, 5].
    Login to submit a comment