How do I get the console to output my number? [duplicate] - javascript

This question already has answers here:
How to call a function in Javascript [duplicate]
(4 answers)
Closed 2 years ago.
I'm just a beginner in JavaScript and I can't solve a problem with a function. I need the code to cycle through an array of let's say the numbers ["3", "5", "7", "4", "9"] and console.log only the highest number in the array. This is the code I'm working with and I had tried many variations to get the function to console.log the number 9 in this case. What is the proper way to insert an array here and I need to switch the return max to console.log(max). Until now I just had an error or the console wont show anything, I need an answer at this point to get me through it.
function findMax(arr){
let max = arr[0];
for(let i = 1; i < arr.length; i++){
if(arr[i] > max){
max = arr[i];
}
}
return max;
}

function findMax(arr){
let max = arr[0];
for(let i = 1; i < arr.length; i++){
if(arr[i] > max){
max = arr[i];
}
}
return max;
}
console.log(findMax(['1','2','3','4','5']))
console.log(findMax([1,2,3,4,5]))

Related

How to get all possible 3 element combinations from an array of n elements in JavaScript? [duplicate]

This question already has an answer here:
Permutation on arrays without duplicate and fixed length
(1 answer)
Closed 10 months ago.
Suppose I have the below array of integers in JavaScript:
[5,7,6,1,7,5,4,8,2,4]
And I want to create a function that extracts all possible 3 element combinations from it, like below:
[[5,7,6],[5,7,1],[5,7,7],etc]
What is the most performant and shortest way to do it?
Is there a better way to do it than for loops?
I think the simplest and most performant way to do it is for loops like this
const data = [5,7,6,1,7,5,4,8,2,4]
const combinations = []
for(let i = 0; i < data.length -2; i++){
for(let j = i + 1; j < data.length -1; j++){
for(let k = j + 1; k < data.length; k++){
combinations.push([data[i],data[j],data[k]])
}
}
}
console.log(combinations)
There are more elegant ways to do it but less performant

ways to check for empty array in a conditional in JS [duplicate]

This question already has answers here:
Empty array does not equal itself in Javascript?
(4 answers)
Closed 1 year ago.
I'm wondering why my conditional statement where I'm trying to sort out the edge case of the input array being empty doesn't seem to work. With my current test case, the result I get is NaN (no idea why). If I change the conditional to if (array.length === 0), then my code functions the way I want it to. Isn't [] the correct representation of an empty array though?
function getAvg(array) {
if (array === []) {
return 0;
}
var sum = 0;
var average = 0;
var length = array.length;
for (var i = 0; i < length; i++) {
sum += array[i];
}
average = sum / length;
return average
}
var input = [];
var result = getAvg(input);
console.log('should be 0:', result);
You can check if length of array is greater than 0\
if(arr.length>0){}

string conversion to numbers to avoid dupes [duplicate]

This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
How to convert a string to an integer in JavaScript
(32 answers)
Closed 1 year ago.
function removeDups(arr) {
let x = {};
for (let i=0; i<arr.length; i++) {
if(!x[arr[i]]) {
x[arr[i]] = true;
}
}
let result = Object.keys(x);
for(let i=0; i<result.length; i++) {
if(typeof result[i] == Number) {
result[i] = parseInt(result[i]);
}
}
return result;
}
removeDups([1, 0, 1, 0]);
//➞ [1, 0]
Hey guys trying to return [1,0] like the problem states but I keep returning ['1','0']. I'm trying to convert these values to numbers and not having much luck. Any help?

How can I check if a string contains repeated characters or not? [duplicate]

This question already has answers here:
Check for repeated characters in a string Javascript
(14 answers)
Closed 2 years ago.
I'm wondering what is the best way to determine if a string contains repeated characters even when they are not adjacent together? I have tried changing the string into an array and then using nested for loops to compare each item in the array with the other one but for some reason it doesn't work.
function hasRpeatedCharacters(str) {
let array = [];
for (let i = 0; i < str.length; i++) {
array.push(str[i]);
}
for (let j = 0; j < array.length; j++) {
for (let k = j + 1; k < array.length; k++) {
if (array[j] != array[k]) {
return true;
} else {
return false;
}
}
}
}
hasRepeatedCharacters("abadan");
Create a Set from the string, and check if the Set's size is less than the string's length. A Set can only hold unique values, so if there are repeated characters the Set's size would be less than the string's length.
const hasRepeatedCharacters = str => new Set(str).size < str.length;
console.log(hasRepeatedCharacters("abadan"));
console.log(hasRepeatedCharacters("abc"));

calculate the sum of a list of lists [duplicate]

This question already has answers here:
Javascript unlimited nested array handling
(6 answers)
Closed 8 years ago.
I found this code on http://games.usvsth3m.com/javascript-under-pressure/ and I reach the 5th problem which requires to estimate the sum of all the integer in the list. The list may contains nested lists. So for a list [1,2,3,4,5] or [[1,2,3],4,5] the code below works but for a list [[[[[1]]]],2,3,4] does not. I try a lot of hours and I do not know to solve it. I need a hit plz.
function arraySum(i) {
var sum =0;
for (var id =0; id<i.length;id++){
if(typeof i[id]==='object'){
var ar = i[id];
for (var dd =0; dd<ar.length;dd++ ){
if(typeof ar[dd]==='number'){
sum+=parseInt(ar[dd]);
}
}
}
else
if(typeof i[id]==='number'){
sum+=parseInt(i[id]);
}
}
return sum;
}
use recursion.
var arr = [1, 2, [3, 4, [[[5]]]]];
num = 0;
function loop (arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] instanceof Array) {
loop(arr[i]);
} else {
num += parseInt(arr[i]);
}
}
}
loop(arr);
console.log(num);
fiddle - http://jsfiddle.net/9j1hcx4x/

Categories

Resources