【发布时间】:2021-08-18 09:49:17
【问题描述】:
标题:确定数组的等边
问题:
我需要帮助编写一个 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。
谢谢!
【问题讨论】:
-
@Snow 感谢您的有效输入,我重新表述了我的问题,以便更有意义!如果您愿意,请随时回答。
标签: javascript arrays sum integer iterated-function