0
What is wrong?
6 Respuestas
+ 3
https://www.sololearn.com/learn/JavaScript/1206/
+ 2
Sina navigator.getBattery return YET a Promise...
if(navigator.getBattery)
navigator.getBattery().then((battery) =>{
console.log(battery.level)
}).catch((error) =>{
alert(error)
})
else alert('navigator has not .getBattery')
+ 1
Hi Sina,
Your HTML doesn't call your JavaScript function.
+ 1
Yes KrOw your code is true but I want to write it as a function
+ 1
What do you mean Agent
+ 1
Hi Sina,
Maybe I lack imagination on this one, but I'm not sure why you would only want to check to presence of the battery API without actually doing anything with the information other than make an alert, so I was looking for clues in your HTML file, but found that you didn't call the function there, only at the end of the JavaScript file and I didn't see anything that indicated that you wanted this to happen when the window loaded (for example).
While SoloLearn does automatically tie the JavaScript file to the HTML file for you and automatically runs whatever you put in it, it just doesn't make sense under normal circumstances.
If I were you, I would mark Kr0W's answer as best because you can just wrap his answer in a function (like the one you wanted, "function a()") and because it deviates the least from whatever you were trying to do (not sure, can't guess).
For something a bit more objective-oriented, please see:
https://code.sololearn.com/WP4xPvHQCD9y/?ref=app
I hope this helps! ☺