JavaScript Exercises Basic#8 - Who's lying?

JavaScript Exercises Basic#8 - Who's lying?

Snow White was living peacefully with the seven dwarfs. One day, when the dwarfs came back from the work, she realised that NINE dwarfs has come back, not seven. All of them insisted that they are the real dwarfs but obviously two of them are lying. Fortunately, she remembered that the sum of the height of real dwarfs was exactly 100cm.

To help Snow White, write a program which finds the seven real dwarfs when given the height of nine dwarfs.

Result Example

Screenshot 2022-04-14 at 22.06.18.png

Solution

1) The sum of the height of 7 real dwarfs are 100. Which means if we subtract the sum of 2 liar dwarfs' height from the sum of all 9 dwarfs', it should be exactly 100. Declare variable named sum and store sum of all 9 dwarfs' height using Array.prototype.reduce() method.

Array.prototype.reduce() This method executes a reducer callback function on each element of the array. It returns a single value: the function's accumulated result.

function solution(arr){
                let realDwarfs = arr;
                let sum = arr.reduce((a, b) => a + b, 0); //The second parameter 0 means the initial value should be 0
            }

2) Iterate the array of dwarfs' height and make all the possible pair of liar dwarfs. Let's suppose the array looks like this-> [a, b, c, d]. The possible pairs should be -> (A, b), (A, c), (A, d), (B, c), (B, d), (C, d), which means for i(first element of the pair), we have to iterate only till the second last element of the array. For j(second element of the pair), we should start iterating from index 1 to the last element. So the for loop to iterate the array will look like the code below.

function solution(arr){
                let realDwarfs = arr;
                let sum = arr.reduce((a, b) => a + b, 0); 
                for (let i = 0; i < arr.length - 1; i++){
                    for (let j = 1; j < arr.length; j++){

                    }
                }
            }

3) Now add if statement to find out which one is the pair of liar dwarfs. The sum of the height of all nine dwarfs minus the sum of the height of two liar dwarfs should be 100. Using splice() method, get rid of the height of liar dwarfs from the array and return it.

Array.prototype.splice() The splice(start, [deleteCount], [item1], [item2], [itemN]) method removes or replace existing elements and/or add new elements in place.

function solution(arr){
                let realDwarfs = arr;
                let sum = arr.reduce((a, b) => a + b, 0); 
                for (let i = 0; i < arr.length - 1; i++){
                    for (let j = 1; j < arr.length; j++){
                        if((sum - (arr[i] + arr[j])) === 100){
                            arr.splice(j, 1);
                            arr.splice(i, 1);
                            /*
                            We should get rid of the second element of the pair(j) first
                            because otherwise it will change the index of each elements and affect i.
                            */
                        }
                    }
                }

                return realDwarfs;
            }