Question

In: Computer Science

Mini JAVASCRIPT Algorithm Exercise "Sum All In Array" Objectives Create a function that adds all numbers...

Mini JAVASCRIPT Algorithm Exercise

"Sum All In Array"

Objectives

  • Create a function that adds all numbers of a provided array (arr), accounting for non-integer values in arr.
  • The output should return an integer.

Notes

Remember your data types? If the element is an integer (8), it should be added. If the element is a string with a number value ("8"), it should be added. If the element is not a number, or if it is a string with a non-number value, it should be ignored.

Use the following starter code below:

const sumAllInArray = arr => {

}

console.log(sumAllInArray(["-1", "a", 4, "-32", 8, "94"]))

//expected output: 73

Solutions

Expert Solution

# If you have a query/issue with respect to the answer, please drop a comment. I will surely try to address your query ASAP and resolve the issue

# # Please consider providing a thumbs up to this question if it helps you. by doing that, you will help other students who are facing a similar issue.

//---------------OUTPUT-------------------------

73

//-------------------------------------------------------

const sumAllInArray = arr => {

    let output=0;

    //loops over the array length

    for(let i=0;i<arr.length;i++){

        // isNaN return false if a string can be converted to number

        if(!isNaN(arr[i])){

            //adding to the output

            output=output+Number(arr[i])

        }

    }

    return output;

}


console.log(sumAllInArray(["-1", "a", 4, "-32", 8, "94"]))


Related Solutions

Define a function in Javascript named secondHighest which accepts an array of numbers, and returns the...
Define a function in Javascript named secondHighest which accepts an array of numbers, and returns the second highest number from the array. If the highest value occurs more than once, then it is also the second highest (see example). Assume the input array will always have length at least two (so there is always a second highest value). The original array must NOT be modified by this function. Example secondHighest([5,3,8,6,2,7,4]) must return 7, and secondHighest([5,3,8,6,2,7,8]) must return 8. I have...
(JAVA) ExceptionSum The objective of this exercise is to create a method that adds all the...
(JAVA) ExceptionSum The objective of this exercise is to create a method that adds all the numbers within an array with an exception you cant sum the 6, and the 7, and the numbers between them cannot be added too, taking into account that whenever a 6 appears will be followed by a 7. If we have an array such that [1,2,3,6,10,9,8,7,5] when we pass it through the method it will give us an int such that the sum will...
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., 12321 This is the code i have, but it doesnt work. PLEASE MAKE SURE IT WORK BEFORE POST ANSWER, I GOT @ CODE THAT DID WORK BEFORE HTML JavaScript Palindrome Exercise rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" /> Palindrome Enter a positive number: Is this a palindrome? No...
WITH using methods create an array with numbers
IN JAVA  Prompt 3:WITH using methods create an array with numbersint [] number = { 35, 68, 80, 34, 45, 79, 80};Display the numbers in the array using for loopDisplay the sumFind biggest element in the arrayDisplay the numers in the array with index numbersDisplay the numers using for loop in ascending order (sort)
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through...
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through the array elements and accumulate the sum of ASCII value of each character in element and return the total. For example: function([‘A’, ‘bc’, 12]); // returns 361 which is the sum of 65 + 98 + 99 + 49 + 50 Use of any built in string functions or built in array functions is not allowed, Any help would be much appreciated
Project: Given an array numbers. We define a running sum of an array as runningSum[i] =...
Project: Given an array numbers. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of numbers. Example: Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]. You need to do: Create a class called RunningSumArray to perform the main method. Create a class called ArrayOperationto hold the actions for array. In this project, you will need 4methods: 1. Public static Int[] getArray() --- inside ArrayOperationclass,...
JAVASCRIPT Create an array of 5 objects named "movies" Each object in the movies array, should...
JAVASCRIPT Create an array of 5 objects named "movies" Each object in the movies array, should have the following properties: Movie Name Director Name Year Released WasSuccessful (this should be a boolean and at least 2 should be false) Genre Loop through all of the objects in Array If the movie is successful, display all the movie information on the page. These movies were a success: Title: Forrest Gump Year Realeased: 1994 Director: Robert Zemeckis Genre: Comedy
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
Write a python program to sum the prime numbers existing in an array . For instance...
Write a python program to sum the prime numbers existing in an array . For instance , if A = [4, 7, 12, 3, 9] the output should be 10
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT