Programming Questions

• Popular Tags
• App Academy Practice Questions help
Write a function lucky_sevens? (numbers), which takes in an array of integers and returns true if any three consecutive elements sum to 7. Examples lucky_sevens?([2,1,5,1,0]) == true # => 1 + 5 + 1 == 7 lucky_sevens?([3,4,3,4]) == false I have this so far and I am stuck. ``` function lucky_sevens (numbers) { var sum = 0; for (var i = 1; i < numbers.length; i++) { if (numbers[0] + numbers[1] + numbers [2] === 7); return "true" i += 1; } return numbers } lucky_sevens ([3,4,3,4]); ```
restoration101 posted this question on 10/6/15 |
• +
• 1
• -
• You want your program to sum each 3 consecutive elements in an array from beginning to end and see if any of the sums equal 7. Your program has a couple problems, the most prominent of which is your for loop. It's adding the first 3 elements of the array (numbers) over and over again. In order for you to loop through the array, you need to use numbers[i] so that its accessing the different elements in the array. You also don't need to include i+=1 since you have i++ in the for loop already. Your return statement is also simply returning the array because no changes are being made to it. This is what your program should look like: ``` function lucky_sevens(arr) { var runningCount = 0; //This will be updated each time a new element is introduced and an an old one is removed for (var i = 0; i < arr.length; i++) { runningCount += arr[i]; //Each array element is being added into runningCount if (i >= 3) { runningCount -= arr[i-3]; /*This if statement is saying: once the loop reaches the third element in the array, subtract the value three elements earlier. This ensures that runningCount is only tracking three consecutive elements at a time.*/ } if (i >= 2 && runningCount === 7) { //If at any time runningCount has the sum of 3 elements and equals 7, return true. return true; } } return false; //The program defaults to returning false unless it fulfills the conditions above } ```
• +
• 1
• -
• To display code include the following tags: ```function luckyseven(arr){ for(i=0;i<arr.length-2;i++){ var sum=arr[i]+arr[i+1]+arr[i+2]; console.log(sum) if(sum==7){return true} } return false; } luckyseven([1,6,3,5,7,5,7,5,-1,2]) ```
• +
• 1
• -
• I posted a solution to this challenge in the app academy prep course everyone! coderbyte.com/course/prepare-app-academy
• +
• 0
• -
• I'm applying to FullStack academy myself in NYC. Good luck! ```function lucky_sevens (numbers) { var result = 0; // Loop through argument array, checking to see if sum is equal to 7 for (var i = 0; i < numbers.length; i++) { result = (numbers[i] + numbers[i+1] + numbers [i+2]); // If sum is equal to 7, immedialtly break out of loop & return boolean true. if (result === 7){ return true;} } // Otherwise, return false. Did not find sum equal to 7 from any 3 consecutive elements. return false; } ```
• +
• 0
• -
• To display code include the following tags: ```public class Practise { static int [] arr={1,2,2,0,5}; static int number; public static void main(String[] args) { System.out.println(num()); } public static boolean num() { for (int i=0; i<= arr.length;i++) if (i<arr.length) { number = arr[i]+arr[i+1]+arr[i+2]; if (number ==7) { return true; } } return false; } }```
• +
• 0
• -
• To display code include the following tags: ```public class Practise { static int [] arr={1,2,2,0,5}; static int number; public static void main(String[] args) { System.out.println(num()); } public static boolean num() { for (int i=0; i<= arr.length;i++) if (i<arr.length) { number = arr[i]+arr[i+1]+arr[i+2]; if (number ==7) { return true; } } return false; } }```
• +
• 0
• -
• This function is bloated but I wanted to create something that could make visulalisation of the process easier. If you run this then it will show you each step of the process. Again, the function doesn't need to be this bloated and it's not very eloquant but for those struggling to see how iterating through functions works, it may help you ``` //Creates the Array to be passed into the function "luckySeven". Choose any numbers you want. var arrayOfNumbers = [1,4,3,5,1,5,7,8,10,0,1,2,4,6,7,8,9]; function luckySeven(arrayOfNumbers) { // The variable that will hold our sums var sumHolder = 0; // The variable that will track how many times we've looped through the array. var iCycleCounter = 0; // This will loop through our array and then add "1" to the count of variable i for(var i = 0; i < arrayOfNumbers.length; i++) { //Adds the current element to the sumHolder. For example If the sumHolder is 2 and the currrent element "arrayOfNumber[i]" is 7 then sumHolder becomes the sum "+=" of 2 + 7 sumHolder += arrayOfNumbers[i]; //We add 1 "++" to the iCycleCounter so later when it gets to a total of 3, we can reset all the variables or return true. iCycleCounter++; // This is unnecessary but lets us visualise how the process works. Feel free to delete. console.log(sumHolder + " sumHolder Logged"); // This is unnecessary but lets us visualise how the process works. Feel free to delete. console.log( "************ i Iteration Cycle = " + iCycleCounter); //Checks to see if the value of the variable "sumHolder is equal to 7 and the loop iteration is equal to 3" if(sumHolder === 7 && iCycleCounter === 3) { //if both the conditions of the if statement are true then end here and log "You got your seven" return true; console.log("You got your seven"); } //Checks to see if the value of the variable "sumHolder is NOT equal to 7 and the loop iteration is equal to 3" if(sumHolder !==7 && iCycleCounter === 3) { //if both the conditions of the if statement are true then we need to reset the variables. sumHolder = 0; iCycleCounter = 0; //We've moved up 3 places in the array index so we need to step back 2 places so as to ensure we check all array elements consecutively by 3's i -= 2; // This is unnecessary but lets us visualise when the variables reset. Feel free to delete. console.log("SUM HOLDER && ICYCLECOUNTER RESET"); } } //If we've made it to here then it means that we never returned true so we must return false. The console.log can be deleted as it just let's us visualise the retrun of false. console.log("We're not lukcy this time!") return false; } ```