<reponame>konstantinterminatorovich/shooter<filename>JavaScript/Prep-Challenges/code-challenges/challenges-18.js<gh_stars>0'use strict';

// -------------------------------------------------------------------------------------------------------
// Challenge 01:
// Required:
//
// Given a string find the length of the middle word in the sentence
//
// Ex:
// Input: "You can't handle the truth!"
// Output: 6
//
// Input: "You're gonna need a bigger boat"
// Output: 1
//

const wordLength = str => {
    const arr = str.split(" ");
    return arr[parseInt(arr.length / 2)].length;
}
// -------------------------------------------------------------------------------------------------------


// -------------------------------------------------------------------------------------------------------
// Challenge 02:
// Required:
//
// Write a function that takes two strings and checks if the two strings contain the same combination of letters
//
// Ex:
// Input: "dad", "add"
// Output: true
//
// Input: "dad", "aadd"
// Output: false
//
// Input: "table", "label"
// Output: false
//
// Input: "cat", "rat"
// Output: false

const wordLetters = (str1, str2) => {
    let answer = true;

    const strA = str1.split("").sort();
    const strB = str2.split("").sort();

    if (strA.length === strB.length) {
        strA.forEach((letter, i) => {
            letter === strB[i] ? "" : answer = false;
        })
    } else {
        answer = false;
    }

    return answer;
}
// -------------------------------------------------------------------------------------------------------

// -------------------------------------------------------------------------------------------------------
// Challenge 03:
// Optional:
//
// Write a function that takes an integer and a sorted array
// The function returns the index of the integer in the array
// If the integer was not there, the function returns where it could have been
//
// Ex:
// Input: [1,3,5,6], 5
// Output: 2
//
// Input: [1,3,5,6], 2
// Output: 1
//
// Input: [1,3,5,6], 7
// Output: 4
//

const targetIndex = (arr, int) => arr.indexOf(int) >= 0 ? arr.indexOf(int) : [...arr, int].sort().indexOf(int);

// -------------------------------------------------------------------------------------------------------

module.exports = { wordLength, wordLetters, targetIndex };