英文:
how to write a program that prints the largest value that occurs simultaneously in both arrays?
问题
我必须创建两个包含1到20之间随机值的10元素数组,并编写一个程序,打印同时出现在这两个数组中的最大值。
我创建了两个如下所示的数组。程序应该打印同时出现在这两个数组中的最大值。这里应该是11。我只知道如何从数组中获取最大值。感谢帮助。
<script>
var max = 0;
var tab = [1, 2, 5, 8, 9, 11, 15, 16, 17, 20];
var tab2 = [3, 4, 6, 7, 10, 11, 12, 13, 14, 18];
for (var i = 0; i < tab.length; i++) {
if (max <= tab[i]) {
max = tab[i];
}
}
console.log(max);
</script>
英文:
I have to create two 10-elements arrays with random values from 1 to 20 and write a program that prints the largest value that occurs simultaneously in both arrays.
I created two tabs like below. The program should prints the largest value that occurs simultaneously in both arrays. Here it should be 11. I know just how to catch the max value from the array. I appreciate help.
<script>
var max = 0;
var tab = [1, 2, 5, 8, 9, 11, 15, 16, 17, 20];
var tab2 = [3, 4, 6, 7, 10, 11, 12, 13, 14, 18];
for (var i = 0; i < tab.length; i++) {
if (max <= tab[i]) {
max = tab[i];
}
}
console.log(max);
</script>
答案1
得分: 1
使用嵌套循环来查找最大值,比较两个数组的每个元素如下所示:
var tab = [1, 2, 5, 8, 9, 11, 15, 16, 17, 20];
var tab2 = [3, 4, 6, 7, 10, 11, 12, 13, 14, 18];
var max = 0;
for (var i = 0; i < tab.length; i++) {
for (var j = 0; j < tab2.length; j++) {
if (tab[i] === tab2[j] && tab[i] > max) {
max = tab[i];
}
}
}
console.log(max);
英文:
to find the largest value use nested loops to compare each element of both arrays as follow
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var tab = [1, 2, 5, 8, 9, 11, 15, 16, 17, 20];
var tab2 = [3, 4, 6, 7, 10, 11, 12, 13, 14, 18];
var max = 0;
for (var i = 0; i < tab.length; i++) {
for (var j = 0; j < tab2.length; j++) {
if (tab[i] === tab2[j] && tab[i] > max) {
max = tab[i];
}
}
}
console.log(max);
<!-- end snippet -->
答案2
得分: 0
Sure, here's the translated JavaScript code:
// const {intersection,max} require('lodash/fp'}
const { intersection, max } = _;
const tab = [1, 2, 5, 8, 9, 11, 15, 16, 17, 20];
const tab2 = [3, 4, 6, 7, 10, 11, 12, 13, 14, 18];
const res = max(intersection(tab, tab2))
console.log({
intersection: intersection(tab, tab2),
max: max(intersection(tab, tab2)),
});
The HTML part does not need translation as it contains only HTML tags and a link to the lodash library.
英文:
I'd do this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// const {intersection,max} require('lodash/fp'}
const { intersection, max } = _;
const tab = [1, 2, 5, 8, 9, 11, 15, 16, 17, 20];
const tab2 = [3, 4, 6, 7, 10, 11, 12, 13, 14, 18];
const res = max(intersection(tab, tab2))
console.log({
intersection: intersection(tab, tab2),
max: max(intersection(tab, tab2)),
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
<!-- end snippet -->
You can translate that into straight javascript, especially if it's for a homework assignment :).
答案3
得分: 0
以下是翻译好的内容:
... 然后要么自己实现一个交集功能,要么使用第三方库/辅助功能。
下面的示例代码包含了getIntersection
的一个可能实现,该实现使用...
Array.from
以始终确保处理数组参数,Array.prototype.sort
以帮助最佳分配参与的数组以获得最佳的计算性能,Array.prototype.reduce
和一个Map
实例,用于创建查找表以实现更高性能的过滤过程,用于实际计算交集时,Array.prototype.filter
和Map.prototype.has
以最终计算交集结果。
function getIntersection(firstIterable = [], secondIterable = []) {
const [
comparisonBase, // ... 与较短的数组进行比较。
comparisonList, // ... 从较长的数组中筛选。
] = [Array.from(firstIterable), Array from(secondIterable)]
// - 确保两个数组(上一行)
// - 并根据每个数组的`length`对它们进行排序。
.sort((a, b) => a.length - b.length);
// 从较短的数组创建一个基于`Set`的查找。
const itemLookup = new Set(comparisonBase);
// 交集是以下过滤任务的结果。
return comparisonList.filter(item => itemLookup.has(item));
}
const tab = [1, 2, 4, 5, 8, 9, 11, 15, 16, 17, 20, 21, 0];
const tab2 = [3, 4, 6, 7, 9, 10, 11, 12, 13, 14, 18, 19, 0];
console.log(
'tab`和`tab2`的交集...',
getIntersection(tab, tab2)
);
console.log(
'tab`和`tab2`的交集的最大值...',
Math.max(...getIntersection(tab, tab2))
);
英文:
The approach is as follows ...
-
get the intersection of both arrays
-
get the maximum value of the just computed intersection
... which then boils down either to coming up with an own implementation of an intersection functionality or to making use of a third party library / helper functionality.
The below example code features one possible implementation of getIntersection
which makes use of ...
-
Array.from
in order to always ensure the processing of array parameters, -
Array.prototype.sort
in order to help optimally assign the participating arrays for the best computation performance, -
Array.prototype.reduce
and aMap
instance for creating a lookup-table in order to achieve a more performant filter process when it comes to the actual computation of the intersection, -
Array.prototype.filter
andMap.prototype.has
for finally computing the intersection result.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function getIntersection(firstIterable = [], secondIterable = []) {
const [
comparisonBase, // ... compare to the shorter array.
comparisonList, // ... filter from the longer array.
] = [Array.from(firstIterable), Array.from(secondIterable)]
// - ensure two arrays (line above)
// - and sort them according to each array's `length`.
.sort((a, b) => a.length - b.length);
// create a `Set` based lookup from the shorter array.
const itemLookup = new Set(comparisonBase);
// the intersection is the result of following filter task.
return comparisonList.filter(item => itemLookup.has(item));
}
const tab = [1, 2, 4, 5, 8, 9, 11, 15, 16, 17, 20, 21, 0];
const tab2 = [3, 4, 6, 7, 9, 10, 11, 12, 13, 14, 18, 19, 0];
console.log(
'intersection of `tab` and `tab2` ...',
getIntersection(tab, tab2)
);
console.log(
'max value of the intersection of `tab` and `tab2` ...',
Math.max(...getIntersection(tab, tab2))
);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论