I am new to javascript, please help me with this code, where did I get it wrong?
function magix2(arrangment, figures) {
arrangment.push(figures);
return arrangment.shift();
}
var bum = [26,27,28,29,30,31,32];
console.log("Before: " + JSON.stringify(arrangment));
console.log(magix2(bum, 33));
console.log("After: " + JSON.stringify(arrangment));
function magix2(arrangment, figures) {
arrangment.push(figures);
return arrangment.shift();
}
var bum = [26,27,28,29,30,31,32];
//console.log("Before: " + JSON.stringify(arrangment));
console.log(magix2(bum, 33));
//console.log("After: " + JSON.stringify(arrangment));
arrangment is the argument of function, you can't access that argument outside the function, that's I commented out these console lines.
arrangment is an argument of magix2 function you can not access it outside. Try accessing bum instead.
function magix2(arrangment, figures) {
arrangment.push(figures);
return arrangment.shift();
}
var bum = [26,27,28,29,30,31,32];
console.log("Before: " + JSON.stringify(bum));
console.log(magix2(bum, 33));
console.log("After: " + JSON.stringify(bum));
The correct way to perform your operation. Console Return 26, as it is first element, also 33 is added to list
Related
I just started using Javascript and I have a small beginner question.
I have created 2 functions, only when I place an argument for a function my 2 parameters are seen as a string and they are not added properly.
par1 = 5 , par2 = 2
function sum(par1,par2){
return document.write("Sum : " + par1+par2 + "<br>");
}
function multi(par1,par2){
return document.write(" Multi : "+par1*par2);
}
If I remove the "Sum:" + function from the sum function, it adds up well, but if I leave it in, my parameters are seen as string and I get result 52 instead of 7.
In multi function it just works well.
Thank you in advance for answering my question!
The reason is that when you write:
"some string"+5
javascript interprets the + operator as concatenate, because that's the only thing that's meaningful for a string, and it assumes that you are concatenating two strings.
Since ES6 javascript has string interpolation, you could write:
document.write(`Sum: ${par1 + par2} <br>`)
Just wrap it with ().. example below
<!DOCTYPE html>
<head>
</head>
<body>
</body>
<script type="text/javascript">
function sum(par1,par2){
//Wrap "par1" and "par2" with ()
return document.write("Sum : " + (par1+par2) + "<br>");
}
function multi(par1,par2){
//Wrap "par1" and "par2" with ()
return document.write(" Multi : "+ (par1*par2));
}
sum(5,2);
multi(5,2);
</script>
</html>
Hope this helps
From my experience it does that because you are adding a string to an integer. If you do something like:
return ("Sum:", (par1 + par2))
That should work.
In JavaScript the '+' with a string just concatenates. For example:
const a = 1 + 1 // returns 2
const b = "2" + 2 // returns 22
const c = 3 + 3 + "3" + 3 // 633
const d = "Hello" + "World" + 4 // returns HelloWorld4
I'm getting undefined instead of a String on return of the answer - though it is the right correct characters being logged. How do I get it to output a definite string?
var greet = function(name) {
let first = String(name. charAt(0). toUpperCase());
let second = String(name.slice(1));
console.log('Hello ' + first + second + '!');
}
You forgot to return a value, so the return-value is undefined.
var greet = function(name) {
let first = String(name. charAt(0). toUpperCase());
let second = String(name.slice(1));
return 'Hello ' + first + second + '!';
}
console.log(greet('Gemma'));
The console will print the result of evaluating an expression. You can notice if you set
let name = 'john'
it will print undefined in the very next line.
That is also happening here. First it is printing your value, then it print undefined.
Your function is working fine - you just need to invoke it and feed its argument with a name...
[for the result look in the console]
var greet = function(name) {
let first = String(name.charAt(0).toUpperCase());
let second = String(name.slice(1));
console.log('Hello ' + first + second + '!');
}
;
greet("henky");
Below is my (not working) code. I'm trying to write a function that allows me to enter in any name into the console.log and then have that name attached to the message. Please see below for clarification:
function checkAge(name, age) {
name = {};
if (age >= 21) {
return ("Welcome," + {} + "!");
}
else return ("Go home," + {} + "!");
}
console.log(checkAge('Adrian', 22)); //Welcome,[object Object]!
Expected result should be 'Welcome, Adrian!' (and not Welcome, [object, Object]!). BUT I don't want to hard code names so I can't just write
name === 'Adrian' since it needs to work for any name. Any advice? Thank you! :)
Remove the name = {}; you are reassigning parameter name and not passing the parameter to the return of string.
function checkAge(name, age) {
if (age >= 21) {
return ("Welcome," + name + "!");
}
else return ("Go home," + name + "!");
}
console.log(checkAge('Adrian', 22));
output >> Welcome,Adrian!
I'm using Oracle JDK 1.8.0_65 with nashorn to run some test cases and I found a very strange behavior when parsing an empty JSON Array.
Here's the code i'm running in nashorn:
var testCase = {
start:function() {
// Case 1: a initialized from JavaScript Array
var a = [];
this.log.debug("a before:" + JSON.stringify(a) + " (length:" + a.length + ")");
a.push(15);
this.log.debug("a after:" + JSON.stringify(a) + " (length:" + a.length + ")");
// Case 2: b initialized parsing a JSON Array
var b = JSON.parse("[]");
this.log.debug("b before:" + JSON.stringify(b) + " (length:" + b.length + ")");
b.push(15);
this.log.debug("b after:" + JSON.stringify(b) + " (length:" + b.length + ")");
}
};
and the output is:
a before:[] (length:0)
a after:[15] (length:1)
b before:[] (length:0)
b after:[0,15] (length:2)
I'ts look like a bug in nashorn JSON parser. Returned Array is not really an empty Array, but it's look like that before pushing the first element. There's a hidden "0" that appears after the first push.
Can't find any bug report about this behavior.
Am I using JSON.parse in a wrong way?
Thanks.
J
Your usage is correct. It seems to be a bug and it appears to have been fixed. I just tried 1.8.0_112. It works as expected.
Can anyone tell me why this is not working?
var fieldsValid = {
registerUserName: false,
registerEmail: false,
registerPassword: false,
registerConfirmPassword: false
};
function showState () {
var str = "<p>registerUserName: " + fieldsValid[registerEmail] + "</p>" ;
document.getElementById('showstate').innerHTML = str;
}
showState();
There is no output into my div.
Use quotes around the property name because otherwise, registerEmail is treated as a variable containing the property name, not a property name:
var str = "<p>registerUserName: " + fieldsValid['registerEmail'] + "</p>" ;
Or use the . syntax without quotes:
var str = "<p>registerUserName: " + fieldsValid.registerEmail + "</p>" ;
MDN Working With Objects is a good resource, relevant to this.
For future debugging, observe the console (F12) in your browser.
Let's say you have someObject.
someObject[johndoe] Returns the item in someObject that has johndoe's value (since here it is a variable) as index.