标题:确定数组的等边
问题:
我需要帮助编写一个 JS 函数,该函数将递增索引两侧的左右整数相加,直到它返回左右整数和相等的索引和整数。
摘要示例:
注意:“|x|” 是分隔左右整数和的索引整数。
//ignore "|x|" when adding each side, and Sum both "Left|x|Right" sides until the Right and Left side are determined "===" (eg.equivalent)
SUM LEFT <|x|> RIGHT SUM CHECK
1 = [|1|2,3,4,3,2,1 ] = 15 //!==
1 = [ 1|2|3,4,3,2,1 ] = 13 //!==
3 = [ 1,2|3|4,3,2,1 ] = 10 //!==
6 = [ 1,2,3|4|3,2,1 ] = 6 //===
10 = [ 1,2,3,4|3|2,1 ] = 3 //!==
13 = [ 1,2,3,4,3|2|1 ] = 1 //!==
15 = [ 1,2,3,4,3,2|1|] = 1 //!==
回答:
在此示例中,整数 4 上的索引 3 拆分数组,其中左侧和右侧的和相等。
返回:
当函数确定左右两边相等时,它应该返回索引整数(例如4)。如果在所有迭代中两边都不等价,那么它应该返回 -1。
谢谢!