The Array Twins [Edabit]

The Array Twins

Create a function that given an array, it returns the index where if splited in two-subarrays (last element of the first array has index of (foundIndex-1)), the sum of them are equal.

Examples

twins([10, 20, 30, 5, 40, 50, 40, 15]) ➞ 5
// foundIndex 5 : [10+20+30+5+40]=[50+40+15]

twins([1, 2, 3, 4, 5, 5]) ➞ 4
// [1, 2, 3, 4] [5, 5]

twins([3, 3]) ➞ 1

Return only the foundIndex, not the divided arrays!

Notes

Solution:

function twins(arr) {
  for (var i = 1; i < arr.length; i++) {
    if (
      arr.slice(0, i).reduce((a, b) => a + b) ===
      arr.slice(i).reduce((a, b) => a + b)
    )
      return i;
  }
}

Average Word Length [Edabit]

Average Word Length

Create a function that takes in a sentence and returns the average length of each word in that sentence. Round your result to two decimal places.

Examples

averageWordLength("A B C.") ➞ 1.00

averageWordLength("What a gorgeous day.") ➞ 4.00

averageWordLength("Dude, this is so awesome!") ➞ 3.80

Notes

Ignore punctuation when counting the length of a word.

Solution:

function averageWordLength(str) {
  var strArr = str.replace(/[^\w\s]|_/g, "").split(" ");
  var len = strArr.length;
  var sol = strArr.map((word) => word.length).reduce((a, b) => a + b) / len;
  return sol.toFixed(2) == sol + "" ? sol : parseFloat(sol.toFixed(2));
}

Carrying the Digits [Edabit]

Write a function that returns the number of times you must carry a digit when you sum together two integers.

Examples

carryDigits(36, 135) ➞ 1
// You carry the 1 when you sum 6 and 5 together.

carryDigits(671, 329) ➞ 3

carryDigits(1111, 3333) ➞ 0

carryDigits(53214, 56905) ➞ 3

Notes

Count all carry operations (even those on leading digits).

Solution:

function carryDigits(n1, n2) {
  var sol = 0,
    carryOver = 0,
    sum = 0;
  while (n1 !== 0 && n2 !== 0) {
    sum = (n1 % 10) + (n2 % 10) + carryOver;
    console.log("n1: " + n1 + " n2:" + n2 + "sum:" + sum);

    if (sum >= 10) {
      carryOver = 1;
      sol++;
    } else carryOver = 0;
    n1 = Math.floor(n1 / 10);
    n2 = Math.floor(n2 / 10);
  }
  return sol;
}

What’s the Length of the Missing Array? [Edabit]

Create a function that takes an array of arrays and return the length of the missing array.

Examples

findMissing([[1], [1, 2], [4, 5, 1, 1], [5, 6, 7, 8, 9]]) ➞ 3

findMissing([[5, 6, 7, 8, 9], [1, 2], [4, 5, 1, 1], [1]]) ➞ 3

findMissing([[4, 4, 4, 4], [1], [3, 3, 3]]) ➞ 2

Notes

  • Test input arrays won’t always be sorted in order of length.
  • If the array of arrays is null or empty, your function should return false.
  • If an array within the parent array is null or empty, return false.
  • There will always be a missing element and its length will be between the given arrays.

Solution:

function findMissing(arr) {
	if(arr === null) return false;
	if(arr.length === 0 ) return false;
	var lenArr = arr.map(a => a.length).sort()
	console.log(lenArr);
	for(var i = 0; i<= lenArr.length; i++){
		if(lenArr[i] == 0) return false;
		if(i+lenArr[0] !== lenArr[i]){
			return i+lenArr[0];
		}
	}
}

Creating beautiful Image Picker component in React Native.

In this post I we are going to see how easily we can create a beautiful ImagePicker component in React Native using `expo-mage-picker`.

First create a new React Native Project, and install required packages using yarn or npm:

With Yarn:

yarn add expo-image-picker

yarn add @expo/vector-icons

With npm:

npm install expo-image-picker

npm install @expo/vector-icons

Full Source Code:

import React, { useState, useEffect } from "react";
import * as ImagePicker from "expo-image-picker";
import { Image, TouchableOpacity, View } from "react-native";
import { MaterialCommunityIcons } from "@expo/vector-icons";

import Screen from "./app/components/Screen";

export default function App() {
  // WE are using hooks to store image url
  const [imageURI, setImageURI] = useState(null);

  /* showing alert message for getting required permissions 
  for accessing image gallery */

  const requestPermission = async () => {
    const { granted } = await ImagePicker.requestCameraPermissionsAsync();
    if (!granted) {
      alert("You need to enable permission to access Image");
    }
  };

  /* function for selecting the image and setting imageURI 
  for us to use
  */

  const selectImage = async () => {
    try {
      const result = await ImagePicker.launchImageLibraryAsync();
      if (!result.cancelled) {
        setImageURI(result.uri);
      }
      console.log(result.uri.toString());
    } catch (e) {
      console.log("Error Reading image.");
    }
  };

  /* when our component runs for the first time then useEffet runs the
  requestPermission() function to get needed permissions
  
  */
  useEffect(() => {
    requestPermission();
  }, []);

  return (
    <Screen>
      {
        /*
        we check if the imageURI is null or not, if null then we render
        View block showing us add icon else if imageURI is having the 
        uri of image then it will render Image block and show the image. 
        Image component is wrapped with TouchableOpacity which is calling the 
        hook setImageURI and setting the imageURI to null i.e. delete it.
        
        
        */
        imageURI ? (
          <TouchableOpacity onPress={() => setImageURI(null)}>
            <Image
              source={{ uri: imageURI }}
              style={{
                width: 300,
                height: 300,
                marginTop: 100,
                alignSelf: "center",
                borderWidth: 10,
                borderColor: "lightgreen",
                borderRadius: 15,
              }}
            />
          </TouchableOpacity>
        ) : (
          <TouchableOpacity onPress={selectImage}>
            <View
              style={{
                width: 300,
                height: 300,
                marginTop: 100,
                alignSelf: "center",
                justifyContent: "center",
                borderWidth: 10,
                borderColor: "lightgreen",
                borderRadius: 15,
              }}
            >
              <MaterialCommunityIcons
                name={"plus"}
                size={200}
                color="lightgrey"
                style={{ alignSelf: "center" }}
              />
            </View>
          </TouchableOpacity>
        )
      }
    </Screen>
  );
}