Unable to pass an array as function argument in Javascript [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I am trying to pass an array as a function argument. When I run the HTML the browser displays a blank page. Please help:) (Obviously a coding noob)
<html>
<head>
<script>
var list = [7, 8, 9, 4];
document.write(list);
pass_array(array) {
let sum = 0;
for (let i = 0; i < list.length; i++) {
sum = +list[i];
}
document.write(sum);
}
pass_array(this, list);
</script>
</head>
</html>

You should replace pass_array(array) with function pass_array(array) (as for function declaration), and then use array values inside pass_array function instead
pass_array(this, list); should have only 1 param list like pass_array(list);. In this context, this is not referred to your list data
<html>
<head>
<script>
var list = [7, 8, 9, 4];
document.write(list);
function pass_array(array) {
let sum = 0;
for (let i = 0; i < array.length; i++) {
sum += array[i];
}
document.write(sum);
}
pass_array(list);
</script>
</head>
</html>

Related

tried reverse array but reverse last item instead [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I tried to reverse array items, but it reverse the last item not array. I also posted the expected output. Hope it will make easy to understand.
var color = ("red", "green", "blue");
function printReverse(str) {
for (var i = str.length - 1; i >= 0; i--) {
console.log(str[i]);
}
}
printReverse(color);
/*
output
e
u
l
b
*/
You define arrays with this statement: ["red","green","blue"], not this: ("red","green","blue"):
var color = ["red","green","blue"];
function printReverse(str){
for ( var i = str.length-1; i>= 0; i-- ){
console.log(str[i]);
}
}
printReverse(color);
var color = ["red","green","blue"];
var rev_color = color.reverse()

Why is this for loop not logging sum? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to use this for loop within the function to get the sum of an array. However, when I call the function I receive NaN.
tips = [1,2,3,4,5]
function sumTip (tips){
for (i=0; i<tips.length; i++){
var total = total + tips[i];
console.log(total);
}
}
sumTip();
Error 1 - Wrong syntax. It should be tips.length and not length.tips.
Error 2 - You haven't given the parameter to the function when you
are calling it. It should be sumTip(tips);
Error 3 - i is not declared in the loop.
Error 4 - For your logic to work, the total should be declared and initialised to zero outside the for loop and then used inside the loop.
You are doing - var total = total + tips[i]; when total hasn't been defined yet so the in the expression total + tips[i] evaluates to NaN and that's why you are getting Nan as output.
The following code should work after correcting the above mentioned errors -
tips = [1,2,3,4,5]
function sumTip (tips){
var total = 0;
for (var i=0; i<tips.length; i++){
total = total + tips[i];
console.log(total);
}
}
sumTip(tips);
Couple problems you're running into here. First, in your last line, when you call sumTip, you never pass it the array, like: sumTip(tips). Second, you are resetting total everytime you loop, it should be set up like:
var total = 0;
for (var i=0; i<tips.length; i++){
total += tips[i];
console.log(total);
}
This all could just be reduced though: let sum = tips.reduce((a, b) => a + b, 0)

Javascript function causing error not defined [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a simple function that seems to be causing an 'Uncaught ReferenceError: arraySort is not defined' whenever the function is called, in this case by a button and i cant see why any help would be brilliant.
Javascript
<script language="javascript">
var unsorted = ["Printer","Tablet","Router"];
var alphaOrder = [" ","A","a","B","b","C","c","D","d","E","e","F","f","G","g", //15
"H","h","I","i","J","j","K","k","L","l","M","m","N","n","O", //30
"o","P","p","Q","q","R","r","S","s","T","t","U","u","V","v", //45
"W","w","X","x","Y","y","Z","z","0","1","2","3","4","5","6", //60
"7","8","9","'","?","!",".","\"","<",">","#",",","#","~","=", //75
"+","-","_","/","\\"];
function arraySort(array){
var sortedArray = [];
var letterNum = 0;
var numArray = [];
function letterToNum(){
for (var elementNum = 0; elementNum < array.length; elementNum++;){
for (var alphaNum = 0; alphaNum < alphaOrder.length; alphaNum++;){
numArray[elementNum] = alphaOrder.indexOf(array[elementNum][letterNum]);
document.getElementById('tester1').innerHTML = numArray;
}
}
}
}
</script>
HTML
<button type = "button" onclick = "arraySort(unsorted)">Sort</button>
Remove the semicolon from the end of your loops.
for (var elementNum = 0; elementNum < array.length; elementNum++) {
for (var alphaNum = 0; alphaNum < alphaOrder.length; alphaNum++) {
}
Few suggestions here
Wrap your logic inside script by window.onload
Don't mix your markup and javascript.
Try binding events at javascript end
Follow the above suggestions, your error will be fixed.

Append to an array made from ul [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi im trying to append a "s" to each element an array created from a unsorted list. Here is my code and I am not sure what I am doing wrong.
Html:
<ul>
<li class="fruit">Apple</li>
<li class="fruit">Banana</li>
<li class="fruit">Pineapple</li>
<li class="fruit">Orange</li>
</ul>
Javascript to append s to each element
var list = document.getElementByClassName('friut');
for(var i=0;i < list.length; i++) {
var arrValue = list[i];
list[i] = arrValue.innerHTML + 's';
}
First, you misspelled both getElementsByClassName and "fruit". If all you are trying to do is make an array of strings equal to the list elements' values + "s", that's your problem.
If you're trying to actually add 's's to the HTML, you want something like this:
for(var i=0;i < list.length; i++) {
list[i].innerHTML += 's';
}
It's getElementsbyClassName, not getElementByClassName. You also spelled 'fruit' as 'friut'
var list = document.getElementsByClassName('fruit');
for(var i=0;i < list.length; i++) {
var arrValue = list[i];
list[i] = arrValue.innerHTML + 's';
}

JS: Why FOR LOOP loops once? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Why I get only one alert after run this code:
var poly = function()
{
this.disp = function()
{
for(var i=0; i<6; i++); //And I already found my problem. It is this ';'
{
alert('The number is '+i); //I get one alert: 'The number is 6'
}
}
}
test = new poly();
test.disp();
Thanks for any help!
This code works. Probably, in your real code, it looks like this:
var poly = function()
{
this.disp = function()
{
for(var i=0; i<6; i++)
{
}
alert('The number is '+i); //I get one alert: 'The number is 6'
}
}
That will cause i to loop from 0 to 6, after which it is alerted once.

Categories