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.
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;
}
}
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));
}
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;
}
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";
exportdefaultfunction App() {
// WE are using hooks to store image urlconst [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>
);
}