NodeJS 如何在指定的字母中找到中间的字母?

wd2eg0qa  于 2022-11-03  发布在  Node.js
关注(0)|答案(3)|浏览(192)

我创建了一个函数,它接受两个参数,以便在Alphabet变量中找到这两个参数之间的中间值。
示例:

the middle part between Q and U is S,
the middle part between R and U is ST,
the middle part between T and Z is W,

我所困惑的是,如何从Alphabet变量的索引1开始逐个取值?

function letterMiddleValue(a, b) {
    let alpha1 = Alphabet.indexOf(a);
    let alpha2 = Alphabet.indexOf(b);
    let center = (alpha1 + alpha2) / 2;
    let letterLength;

    if (center % 2 == 1) {
        letterLength = 1;
    } else {
        letterLength = 2;
    }
    return Alphabet.substring(center, center + letterLength);
}

var Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

console.log(letterMiddleValue("Q", "U"));
console.log(letterMiddleValue("R", "U"));
console.log(letterMiddleValue("T", "Z"));
7ajki6be

7ajki6be1#

我认为还有一种替代的方法,那就是仅仅计算两者之间的距离,然后决定是否提取一个或两个字符。
这也可以处理两者之间没有任何内容的情况,或者b值在字母表中的位置较低(或者不在定义的字母表中)的情况。

function letterMiddleValue(a, b) {
    const aPos = Alphabet.indexOf(a);
    const bPos = Alphabet.indexOf(b);
    const len = bPos - aPos

    if (len < 2) {
        return '[none]'
    }

    const start = aPos + (len / 2)
    const end = start + 1 + (len % 2)
    return Alphabet.slice(Math.floor(start), end)
}

const Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

console.log(letterMiddleValue("Q", "U"));   // S
console.log(letterMiddleValue("R", "U"));   // ST
console.log(letterMiddleValue("T", "Z"));   // W
console.log(letterMiddleValue("A", "B"));   // none
console.log(letterMiddleValue("A", "C"));   // B
console.log(letterMiddleValue("A", "D"));   // BC
console.log(letterMiddleValue("A", "A"));   // none
console.log(letterMiddleValue("Z", "A"));   // none
console.log(letterMiddleValue("z", "A"));   // none
console.log(letterMiddleValue("Z", "a"));   // none
k5ifujac

k5ifujac2#

中心为r & u为18.5.18.5%2为0. 5所以需要检查为0. 5的条件:

function letterMiddleValue(a, b) {
    let alpha1 = Alphabet.indexOf(a);
    let alpha2 = Alphabet.indexOf(b);
    let center = (alpha1 + alpha2) / 2;
    let letterLength;

    if (center % 2 == 0.5) {
        letterLength = 2;
    } else {
        letterLength = 1;
    }

    return Alphabet.substring(center, center + letterLength);
}

var Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

console.log(letterMiddleValue("Q", "U"));
console.log(letterMiddleValue("R", "U"));
console.log(letterMiddleValue("T", "Z"));

这是因为你计算中心的方式。(7 + 5)/ 2 = 6.5(6 + 10)/ 2 = 8,模运算返回的是不能被2整除的值

r7xajy2e

r7xajy2e3#

我希望这对你有帮助

const findBetween = (first, last) => {
    const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    const splited = alphabet.split("");
    const firstIdx = splited.findIndex((char) => char == first);
    const lastIdx = splited.findIndex((char) => char == last);
    const result = [];
    splited.forEach((char, i) => {
      i > firstIdx && i < lastIdx && result.push(char);
    });
    return result.join("");
  };

  console.log("7-Find Middle Alphabet");
  console.log("QU", findBetween("Q", "U"));
  console.log("RU", findBetween("R", "U"));
  console.log("TZ", findBetween("T", "Z"));
  console.log("QZ", findBetween("Q", "Z"));

相关问题